kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

diff.kak (5515B)


      1 hook global BufCreate .*\.(diff|patch) %{
      2     set-option buffer filetype diff
      3 }
      4 
      5 hook global WinSetOption filetype=diff %{
      6     require-module diff
      7     map buffer normal <ret> :diff-jump<ret>
      8 }
      9 
     10 hook -group diff-highlight global WinSetOption filetype=diff %{
     11     add-highlighter window/diff ref diff
     12     hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/diff }
     13 }
     14 
     15 provide-module diff %§
     16 
     17 set-face global DiffAdd green
     18 set-face global DiffRm red
     19 set-face global DiffMeta cyan
     20 
     21 add-highlighter shared/diff group
     22 add-highlighter shared/diff/ regex "^\+\N*\n" 0:DiffAdd
     23 add-highlighter shared/diff/ regex "^-\N*\n" 0:DiffRm
     24 add-highlighter shared/diff/ regex "^@@\N*@@" 0:DiffMeta
     25 # If any trailing whitespace was introduced in diff, show it with red background
     26 add-highlighter shared/diff/ regex "^\+\N*?(\h+)\n" 1:default,red
     27 
     28 define-command diff-jump -params .. -docstring %{
     29         diff-jump [<switches>] [<directory>]: edit the diff's source file at the cursor position.
     30         Paths are resolved relative to <directory>, or the current working directory if unspecified.
     31 
     32         Switches:
     33             -       jump to the old file instead of the new file
     34             -<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.
     35     } %{
     36     evaluate-commands -draft -save-regs c %{
     37         # Save the column because we will move the cursor.
     38         set-register c %val{cursor_column}
     39         # If there is a "diff" line, we don't need to look further back.
     40         try %{
     41             execute-keys %{,<a-l><semicolon><a-?>^(?:> )*diff\b<ret>x}
     42         } catch %{
     43             # A single file diff won't have a diff line. Start parsing from
     44             # the buffer start, so we can tell if +++/--- lines are headers
     45             # or content.
     46             execute-keys Gk
     47         }
     48         diff-parse BEGIN %{
     49             my $seen_ddash = 0;
     50             foreach (@ARGV) {
     51                 if ($seen_ddash or !m{^-}) {
     52                     $directory = $_;
     53                 } elsif ($_ eq "-") {
     54                     $version = "-", $other_version = "+";
     55                 } elsif (m{^-(\d+)$}) {
     56                     $strip = $1;
     57                 } elsif ($_ eq "--") {
     58                     $seen_ddash = 1;
     59                 } else {
     60                     fail "unknown option: $_";
     61                 }
     62             }
     63         } END %exp{
     64             my $file_column;
     65             if (not defined $file_line) {
     66                 $file_line = "";
     67                 $file_column = "";
     68             } else {
     69                 my $diff_column = %reg{c};
     70                 $file_column = $diff_column - 1; # Account for [ +-] diff prefix.
     71                 # If the cursor was on a hunk header, go to the section header if possible.
     72                 if ($diff_line_text =~ m{^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+) @@ )(\N*)}) {
     73                     my $hunk_header_prefix = $1;
     74                     my $hunk_header_from_userdiff = $2;
     75                     open FILE, "<", $file or fail "failed to open file: $!: $file";
     76                     my @lines = <FILE>;
     77                     for (my $i = $file_line - 1; $i >= 0 and $i < scalar @lines; $i--) {
     78                         if ($lines[$i] !~ m{\Q$hunk_header_from_userdiff}) {
     79                             next;
     80                         }
     81                         $file_line = $i + 1;
     82                         # Re-add 1 because the @@ line does not have a [ +-] diff prefix.
     83                         $file_column = $diff_column + 1 - length $hunk_header_prefix;
     84                         last;
     85                     }
     86                 }
     87             }
     88             printf "set-register c %%s $file_line $file_column\n", quote($file);
     89         } -- %arg{@}
     90         evaluate-commands -client %val{client} %{
     91             evaluate-commands -try-client %opt{jumpclient} %{
     92                 edit -existing -- %reg{c}
     93             }
     94         }
     95     }
     96 }
     97 complete-command diff-jump file
     98 
     99 define-command -hidden diff-parse -params 2.. %{
    100     evaluate-commands -save-regs ae| %{
    101         set-register a %arg{@}
    102         set-register e nop
    103         set-register | %{
    104             eval set -- "$kak_quoted_reg_a"
    105             if ! result=$(perl "${kak_runtime}/rc/filetype/diff-parse.pl" "$@"); then
    106                 printf 'set-register e %s\n' "fail $result"
    107             else
    108                 printf '%s\n' "$result"
    109             fi >"$kak_command_fifo"
    110         }
    111         execute-keys <a-|><ret>
    112         %reg{e}
    113     }
    114 }
    115 
    116 §
    117 
    118 define-command \
    119     -docstring %{diff-select-file: Select surrounding patch file} \
    120     -params 0 \
    121     diff-select-file %{
    122                 evaluate-commands -itersel -save-regs 'ose/' %{
    123         try %{
    124             execute-keys '"oZgl<a-?>^diff <ret>;"sZ' 'Ge"eZ'
    125             try %{ execute-keys '"sz?\n(?=diff )<ret>"e<a-Z><lt>' }
    126             execute-keys '"ez'
    127         } catch %{
    128             execute-keys '"oz'
    129             fail 'Not in a diff file'
    130         }
    131     }
    132 }
    133 
    134 define-command \
    135     -docstring %{diff-select-hunk: Select surrounding patch hunk} \
    136     -params 0 \
    137     diff-select-hunk %{
    138     evaluate-commands -itersel -save-regs 'ose/' %{
    139         try %{
    140             execute-keys '"oZgl<a-?>^@@ <ret>;"sZ' 'Ge"eZ'
    141             try %{ execute-keys '"sz?\n(?=diff )<ret>"e<a-Z><lt>' }
    142             try %{ execute-keys '"sz?\n(?=@@ )<ret>"e<a-Z><lt>' }
    143             execute-keys '"ez'
    144         } catch %{
    145             execute-keys '"oz'
    146             fail 'Not in a diff hunk'
    147         }
    148     }
    149 }