From c81266d4f6e5ddff861dc245d86600cb8f68b9e8 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 25 Feb 2024 11:12:58 +0100 Subject: [PATCH] jump{,-next,-previous} cmds to navigate any grep-like buffer `grep-next-match` works only on the `*grep*` buffer so it can't be used on buffers that were preserved by renaming or on other grep-flavored buffers created by 3rd party plugins kakoune-find and kakoune-lsp, like `*find*` and `*references*`. Let's generalize `grep-next-match` with a `jump-next` command that takes a buffer argument. This renames some options but I think they're not commonly used. kakoune-lsp is an exception that uses grep_current_line but it's no big deal, things will fail loud and early if options are missing. Since grep.kak and friends now depend on jump.kak, move the jumpclient declaration there as well. --- rc/tools/clang.kak | 2 +- rc/tools/grep.kak | 58 +++++++--------------------------------- rc/tools/jump.kak | 66 ++++++++++++++++++++++++++++++++++++++++++++++ rc/tools/lint.kak | 7 +++-- rc/tools/make.kak | 57 +++++++++++++++------------------------ 5 files changed, 101 insertions(+), 89 deletions(-) create mode 100644 rc/tools/jump.kak diff --git a/rc/tools/clang.kak b/rc/tools/clang.kak index cddf90037..bfa8160a7 100644 --- a/rc/tools/clang.kak +++ b/rc/tools/clang.kak @@ -27,7 +27,7 @@ define-command -params ..1 \ evaluate-commands -draft %{ edit! -fifo ${dir}/fifo -debug *clang-output* set-option buffer filetype make - set-option buffer make_current_error_line 0 + set-option buffer jump_current_line 0 hook -once -always buffer BufCloseFifo .* %{ nop %sh{ rm -r ${dir} } } }" diff --git a/rc/tools/grep.kak b/rc/tools/grep.kak index d97388427..faef6ee92 100644 --- a/rc/tools/grep.kak +++ b/rc/tools/grep.kak @@ -1,8 +1,7 @@ +# require-module jump + declare-option -docstring "shell command run to search for subtext in a file/directory" \ str grepcmd 'grep -RHn' -declare-option -docstring "name of the client in which utilities display information" \ - str toolsclient -declare-option -hidden int grep_current_line 0 define-command -params .. -docstring %{ grep []: grep utility wrapper @@ -30,7 +29,7 @@ define-command -params .. -docstring %{ printf %s\\n "evaluate-commands -try-client '$kak_opt_toolsclient' %{ edit! -fifo ${output} *grep* set-option buffer filetype grep - set-option buffer grep_current_line 0 + set-option buffer jump_current_line 0 hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } } }" }} @@ -39,60 +38,23 @@ complete-command grep file hook -group grep-highlight global WinSetOption filetype=grep %{ add-highlighter window/grep group add-highlighter window/grep/ regex "^([^:\n]+):(\d+):(\d+)?" 1:cyan 2:green 3:green - add-highlighter window/grep/ line %{%opt{grep_current_line}} default+b + add-highlighter window/grep/ line %{%opt{jump_current_line}} default+b hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/grep } } hook global WinSetOption filetype=grep %{ - hook buffer -group grep-hooks NormalKey grep-jump + hook buffer -group grep-hooks NormalKey jump hook -once -always window WinSetOption filetype=.* %{ remove-hooks buffer grep-hooks } } -declare-option -docstring "name of the client in which all source code jumps will be executed" \ - str jumpclient - define-command -hidden grep-jump %{ - evaluate-commands -save-regs a %{ # use evaluate-commands to ensure jumps are collapsed - try %{ - evaluate-commands -draft %{ - execute-keys ',xs^([^:\n]+):(\d+):(\d+)?' - set-register a %reg{1} %reg{2} %reg{3} - } - set-option buffer grep_current_line %val{cursor_line} - evaluate-commands -try-client %opt{jumpclient} -verbatim -- edit -existing -- %reg{a} - try %{ focus %opt{jumpclient} } - } - } + jump } -define-command grep-next-match -docstring 'Jump to the next grep match' %{ - evaluate-commands -try-client %opt{jumpclient} %{ - buffer '*grep*' - # First jump to end of buffer so that if grep_current_line == 0 - # 0g will be a no-op and we'll jump to the first result. - # Yeah, thats ugly... - execute-keys ge %opt{grep_current_line}g /^[^:\n]+:\d+: - grep-jump - } - try %{ - evaluate-commands -client %opt{toolsclient} %{ - buffer '*grep*' - execute-keys gg %opt{grep_current_line}g - } - } +define-command grep-next-match -docstring %{alias for "jump-next *grep*"} %{ + jump-next *grep* } -define-command grep-previous-match -docstring 'Jump to the previous grep match' %{ - evaluate-commands -try-client %opt{jumpclient} %{ - buffer '*grep*' - # See comment in grep-next-match - execute-keys ge %opt{grep_current_line}g ^[^:\n]+:\d+: - grep-jump - } - try %{ - evaluate-commands -client %opt{toolsclient} %{ - buffer '*grep*' - execute-keys gg %opt{grep_current_line}g - } - } +define-command grep-previous-match -docstring %{alias for "jump-previous *grep*"} %{ + jump-previous *grep* } diff --git a/rc/tools/jump.kak b/rc/tools/jump.kak new file mode 100644 index 000000000..a50958b4c --- /dev/null +++ b/rc/tools/jump.kak @@ -0,0 +1,66 @@ +provide-module jump %ยงยง + +declare-option -docstring "name of the client in which all source code jumps will be executed" \ + str jumpclient +declare-option -docstring "name of the client in which utilities display information" \ + str toolsclient + +declare-option -hidden int jump_current_line 0 + +define-command -hidden jump %{ + evaluate-commands -save-regs a %{ # use evaluate-commands to ensure jumps are collapsed + try %{ + evaluate-commands -draft %{ + execute-keys ',xs^([^:\n]+):(\d+):(\d+)?' + set-register a %reg{1} %reg{2} %reg{3} + } + set-option buffer jump_current_line %val{cursor_line} + evaluate-commands -try-client %opt{jumpclient} -verbatim -- edit -existing -- %reg{a} + try %{ focus %opt{jumpclient} } + } + } +} + +define-command jump-next -params 1 -docstring %{ + jump-next : jump to next location listed in the given *grep*-like location list buffer. +} %{ + evaluate-commands -try-client %opt{jumpclient} -save-regs / %{ + buffer %arg{1} + jump-select-next + jump + } + try %{ + evaluate-commands -client %opt{toolsclient} %{ + buffer %arg{1} + execute-keys gg %opt{jump_current_line}g + } + } +} +complete-command jump-next buffer +define-command -hidden jump-select-next %{ + # First jump to end of buffer so that if jump_current_line == 0 + # 0g will be a no-op and we'll jump to the first result. + # Yeah, thats ugly... + execute-keys ge %opt{jump_current_line}g /^[^:\n]+:\d+: +} + +define-command jump-previous -params 1 -docstring %{ + jump-previous : jump to previous location listed in the given *grep*-like location list buffer. +} %{ + evaluate-commands -try-client %opt{jumpclient} -save-regs / %{ + buffer %arg{1} + jump-select-previous + jump + } + try %{ + evaluate-commands -client %opt{toolsclient} %{ + buffer %arg{1} + execute-keys gg %opt{jump_current_line}g + } + } +} +complete-command jump-previous buffer +define-command -hidden jump-select-previous %{ + # See comment in jump-select-next + execute-keys ge %opt{jump_current_line}g ^[^:\n]+:\d+: +} diff --git a/rc/tools/lint.kak b/rc/tools/lint.kak index 9996b0e8e..e52a468b6 100644 --- a/rc/tools/lint.kak +++ b/rc/tools/lint.kak @@ -1,3 +1,5 @@ +# require-module jump + declare-option \ -docstring %{ The shell command used by lint-buffer and lint-selections. @@ -11,14 +13,11 @@ declare-option -hidden line-specs lint_messages declare-option -hidden int lint_error_count declare-option -hidden int lint_warning_count -declare-option -docstring "name of the client in which utilities display information" \ - str toolsclient - define-command -hidden -params 1 lint-open-output-buffer %{ evaluate-commands -try-client %opt{toolsclient} %{ edit! -fifo "%arg{1}/fifo" -debug *lint-output* set-option buffer filetype make - set-option buffer make_current_error_line 0 + set-option buffer jump_current_line 0 } } diff --git a/rc/tools/make.kak b/rc/tools/make.kak index 5d4813d3a..895877d13 100644 --- a/rc/tools/make.kak +++ b/rc/tools/make.kak @@ -1,12 +1,11 @@ +# require-module jump + declare-option -docstring "shell command run to build the project" \ str makecmd make declare-option -docstring "pattern that describes lines containing information about errors in the output of the `makecmd` command. Capture groups must be: 1: filename 2: line number 3: optional column 4: optional error description" \ regex make_error_pattern "^([^:\n]+):(\d+):(?:(\d+):)? (?:fatal )?error:([^\n]+)?" -declare-option -docstring "name of the client in which utilities display information" \ - str toolsclient -declare-option -hidden int make_current_error_line define-command -params .. \ -docstring %{ @@ -20,7 +19,7 @@ define-command -params .. \ printf %s\\n "evaluate-commands -try-client '$kak_opt_toolsclient' %{ edit! -fifo ${output} -scroll *make* set-option buffer filetype make - set-option buffer make_current_error_line 0 + set-option buffer jump_current_line 0 hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } } }" }} @@ -28,7 +27,7 @@ define-command -params .. \ add-highlighter shared/make group add-highlighter shared/make/ regex "^([^:\n]+):(\d+):(?:(\d+):)?\h+(?:((?:fatal )?error)|(warning)|(note)|(required from(?: here)?))?.*?$" 1:cyan 2:green 3:green 4:red 5:yellow 6:blue 7:yellow add-highlighter shared/make/ regex "^\h*(~*(?:(\^)~*)?)$" 1:green 2:cyan+b -add-highlighter shared/make/ line '%opt{make_current_error_line}' default+b +add-highlighter shared/make/ line '%opt{jump_current_line}' default+b hook -group make-highlight global WinSetOption filetype=make %{ add-highlighter window/make ref make @@ -36,13 +35,13 @@ hook -group make-highlight global WinSetOption filetype=make %{ } hook global WinSetOption filetype=make %{ + alias buffer jump make-jump + alias buffer jump-select-next make-select-next + alias buffer jump-select-previous make-select-previous hook buffer -group make-hooks NormalKey make-jump hook -once -always window WinSetOption filetype=.* %{ remove-hooks buffer make-hooks } } -declare-option -docstring "name of the client in which all source code jumps will be executed" \ - str jumpclient - define-command -hidden make-open-error -params 4 %{ evaluate-commands -try-client %opt{jumpclient} %{ edit -existing "%arg{1}" %arg{2} %arg{3} @@ -59,45 +58,31 @@ define-command -hidden make-jump %{ execute-keys gl "Entering directory" # Try to parse the error into capture groups, failing on absolute paths execute-keys s "Entering directory [`']([^']+)'.*\n([^:\n/][^:\n]*):(\d+):(?:(\d+):)?([^\n]+)\n?\z" l - set-option buffer make_current_error_line %val{cursor_line} + set-option buffer jump_current_line %val{cursor_line} set-register a "%reg{1}/%reg{2}" "%reg{3}" "%reg{4}" "%reg{5}" } catch %{ set-register / %opt{make_error_pattern} execute-keys sl - set-option buffer make_current_error_line %val{cursor_line} + set-option buffer jump_current_line %val{cursor_line} set-register a "%reg{1}" "%reg{2}" "%reg{3}" "%reg{4}" } } make-open-error %reg{a} } } - -define-command make-next-error -docstring 'Jump to the next make error' %{ - evaluate-commands -try-client %opt{jumpclient} -save-regs / %{ - buffer '*make*' +define-command -hidden make-select-next %{ set-register / %opt{make_error_pattern} - execute-keys "%opt{make_current_error_line}ggl" "/" - make-jump - } - try %{ - evaluate-commands -client %opt{toolsclient} %{ - buffer '*make*' - execute-keys %opt{make_current_error_line}g - } - } + execute-keys "%opt{jump_current_line}ggl" "/" +} +define-command -hidden make-select-previous %{ + set-register / %opt{make_error_pattern} + execute-keys "%opt{jump_current_line}g" "" } -define-command make-previous-error -docstring 'Jump to the previous make error' %{ - evaluate-commands -try-client %opt{jumpclient} -save-regs / %{ - buffer '*make*' - set-register / %opt{make_error_pattern} - execute-keys "%opt{make_current_error_line}g" "" - make-jump - } - try %{ - evaluate-commands -client %opt{toolsclient} %{ - buffer '*make*' - execute-keys %opt{make_current_error_line}g - } - } +define-command make-next-error -docstring %{alias for "jump-next *make*"} %{ + jump-next *make* +} + +define-command make-previous-error -docstring %{alias for "jump-previous *make*"} %{ + jump-previous *make* }