2021-03-19 18:00:49 +00:00
|
|
|
# Author: Andrey Listopadov
|
|
|
|
# Module for grepping file contents
|
|
|
|
# https://github.com/andreyorst/fzf.kak
|
2019-11-13 11:58:55 +00:00
|
|
|
|
2021-03-19 18:00:49 +00:00
|
|
|
hook global ModuleLoaded fzf %{
|
|
|
|
map -docstring 'grep file contents recursively' global fzf g ': require-module fzf-grep; fzf-grep<ret>'
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module fzf-grep %§
|
2019-11-13 11:58:55 +00:00
|
|
|
|
2022-01-08 07:39:49 +00:00
|
|
|
declare-option -docstring "hat command to use to provide a list of grep search matches.
|
|
|
|
Grep output must follow the format of 'filename:line-number:text', and specify a pattern to match across all file contents.
|
|
|
|
By default, an empty pattern is searched, effectively matching every line in every file.
|
|
|
|
GNU grep and ripgrep are supported by default.
|
2019-11-13 11:58:55 +00:00
|
|
|
|
|
|
|
Default value:
|
2022-01-08 07:39:49 +00:00
|
|
|
grep -RHn '' ." \
|
2019-11-13 11:58:55 +00:00
|
|
|
str fzf_grep_command 'grep'
|
|
|
|
|
|
|
|
|
|
|
|
define-command -hidden fzf-grep %{ evaluate-commands %sh{
|
2021-03-20 19:44:19 +00:00
|
|
|
if [ -z "$(command -v "${kak_opt_fzf_grep_command%% *}")" ]; then
|
2019-11-13 11:58:55 +00:00
|
|
|
printf "%s\n" "echo -markup '{Information}''$kak_opt_fzf_grep_command'' is not installed. Falling back to ''grep'''"
|
|
|
|
kak_opt_fzf_grep_command="grep"
|
|
|
|
fi
|
|
|
|
case $kak_opt_fzf_grep_command in
|
|
|
|
(grep) cmd="grep -RHn '' ." ;;
|
|
|
|
(rg) cmd="rg --line-number --no-column --no-heading --color=never ''" ;;
|
|
|
|
(grep*|rg*) cmd=$kak_opt_fzf_grep_command ;;
|
|
|
|
(*) items_executable=$(printf "%s\n" "$kak_opt_fzf_grep_command" | grep -o -E "[[:alpha:]]+" | head -1)
|
2019-12-17 11:29:59 +00:00
|
|
|
printf "%s\n" "echo -markup %{{Information}Warning: '$items_executable' is not supported by fzf.kak.}"
|
2019-11-13 11:58:55 +00:00
|
|
|
cmd=$kak_opt_fzf_grep_command ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
cmd="$cmd 2>/dev/null"
|
|
|
|
|
|
|
|
title="fzf grep"
|
|
|
|
message="grep through contents of all files recursively.
|
|
|
|
<ret>: open search result in new buffer.
|
2021-03-21 10:33:27 +00:00
|
|
|
${kak_opt_fzf_window_map:-ctrl-w}: open search result in new terminal"
|
2021-03-20 19:44:19 +00:00
|
|
|
[ -n "${kak_client_env_TMUX:-}" ] && tmux_keybindings="
|
2021-03-21 10:33:27 +00:00
|
|
|
${kak_opt_fzf_horizontal_map:-ctrl-s}: open search result in horizontal split
|
|
|
|
${kak_opt_fzf_vertical_map:-ctrl-v}: open search result in vertical split"
|
2019-11-13 11:58:55 +00:00
|
|
|
|
|
|
|
printf "%s\n" "info -title '${title}' '${message}${tmux_keybindings}'"
|
2021-03-21 10:33:27 +00:00
|
|
|
[ -n "${kak_client_env_TMUX}" ] && additional_flags="--expect ${kak_opt_fzf_vertical_map:-ctrl-v} --expect ${kak_opt_fzf_horizontal_map:-ctrl-s}"
|
|
|
|
printf "%s\n" "fzf -kak-cmd %{evaluate-commands} -fzf-args %{--expect ${kak_opt_fzf_window_map:-ctrl-w} $additional_flags --delimiter=':' -n'3..'} -items-cmd %{$cmd} -filter %{sed -E 's/([^:]+):([^:]+):.*/edit -existing \1; execute-keys \2gvc/'}"
|
2019-11-13 11:58:55 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
§
|