kakoune/rc/tools/grep.kak
Johannes Altmanninger 88aff72bc8 rc grep/make: don't touch user selections
Since the default make error pattern spans until the end of the
line, make-jump select the whole line, moving the cursor to the end.
This is especially  bad when the error message is very long and hence
the cursor movement scrolls the viewport so the file:line:col is no
longer visible.

Stop moving the cursor in `*make*` and `*grep*` buffers.
We already have highlighting to indicate the current line.
2024-02-27 00:03:43 +11:00

99 lines
3.6 KiB
Plaintext

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 [<arguments>]: grep utility wrapper
All optional arguments are forwarded to the grep utility
Passing no argument will perform a literal-string grep for the current selection
} grep %{ evaluate-commands %sh{
if [ $# -eq 0 ]; then
case "$kak_opt_grepcmd" in
ag\ * | git\ grep\ * | grep\ * | rg\ * | ripgrep\ * | ugrep\ * | ug\ *)
set -- -F "${kak_selection}"
;;
ack\ *)
set -- -Q "${kak_selection}"
;;
*)
set -- "${kak_selection}"
;;
esac
fi
output=$(mktemp -d "${TMPDIR:-/tmp}"/kak-grep.XXXXXXXX)/fifo
mkfifo ${output}
( ${kak_opt_grepcmd} "$@" 2>&1 | tr -d '\r' > ${output} 2>&1 & ) > /dev/null 2>&1 < /dev/null
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
hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -r $(dirname ${output}) } }
}"
}}
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
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/grep }
}
hook global WinSetOption filetype=grep %{
hook buffer -group grep-hooks NormalKey <ret> grep-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+)?<ret>'
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} }
}
}
}
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<a-l> will be a no-op and we'll jump to the first result.
# Yeah, thats ugly...
execute-keys ge %opt{grep_current_line}g<a-l> /^[^:\n]+:\d+:<ret>
grep-jump
}
try %{
evaluate-commands -client %opt{toolsclient} %{
buffer '*grep*'
execute-keys gg %opt{grep_current_line}g
}
}
}
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<a-h> <a-/>^[^:\n]+:\d+:<ret>
grep-jump
}
try %{
evaluate-commands -client %opt{toolsclient} %{
buffer '*grep*'
execute-keys gg %opt{grep_current_line}g
}
}
}