1
0
Fork 0

Merge pull request #47 from andreyorst/populate-grep

Populate grep
This commit is contained in:
Andrey Orst 2019-03-30 17:18:00 +03:00 committed by GitHub
commit 02b02b266e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 16 deletions

View file

@ -194,6 +194,8 @@ these arguments:
- `-kak-cmd`: A Kakoune command that is applied to `fzf` resulting value, e.g.
`edit -existing`, `change-directory`, e.t.c.
- `-multiple-cmd`: A Kakoune command that is applied when multiple items
selected to every item but the first one.
- `-items-cmd`: A command that is used as a pipe to provide list of values to
`fzf`. For example, if we want to pass list of all files recursively in
current directory, we would use `-items-cmd %{find .}` which will be piped to

View file

@ -103,6 +103,7 @@ define-command -docstring \
Switches:
-kak-cmd <command>: A Kakoune cmd that is applied to fzf resulting value
-multiple-cmd <command>: A Kakoune cmd that is applied all multiple selected files but the first one
-items-cmd <items command>: A command that is used as a pipe to provide list of values to fzf
-fzf-impl <implementation>: Owerride fzf implementation variable
-fzf-args <args>: Additional flags for fzf program
@ -112,6 +113,7 @@ Switches:
-post-action <commands>: Extra commands that are preformed after `-kak-cmd' command" \
-shell-script-completion %{
printf "%s\n" "-kak-cmd
-multiple-cmd
-items-cmd
-fzf-impl
-fzf-args
@ -125,17 +127,20 @@ fzf -params .. %{ evaluate-commands %sh{
while [ $# -gt 0 ]; do
case $1 in
-kak-cmd) shift; kakoune_cmd="$1" ;;
-items-cmd) shift; items_cmd="$1 |" ;;
-fzf-impl) shift; fzf_impl="$1" ;;
-fzf-args) shift; fzf_args="$1" ;;
-preview-cmd) shift; preview_cmd="$1" ;;
-preview) preview="true" ;;
-filter) shift; filter="| $1" ;;
-post-action) shift; post_action="$1" ;;
-kak-cmd) shift; kakoune_cmd="$1" ;;
-multiple-cmd) shift; multiple_cmd="$1" ;;
-items-cmd) shift; items_cmd="$1 |" ;;
-fzf-impl) shift; fzf_impl="$1" ;;
-fzf-args) shift; fzf_args="$1" ;;
-preview-cmd) shift; preview_cmd="$1" ;;
-preview) preview="true" ;;
-filter) shift; filter="| $1" ;;
-post-action) shift; post_action="$1" ;;
esac; shift
done
[ -z "$multiple_cmd" ] && multiple_cmd="$kakoune_cmd"
if [ "${preview}" = "true" ]; then
# bake position option to define them at runtime
if [ -n "${kak_client_env_TMUX}" ]; then
@ -196,8 +201,12 @@ fzf -params .. %{ evaluate-commands %sh{
esac
if [ -n "${item}" ]; then
printf "%s\n" "evaluate-commands -client ${kak_client} ${wincmd} %{${kakoune_cmd} %{${item}}}"
break
fi
done
while read line; do
printf "%s\n" "evaluate-commands -client ${kak_client} ${wincmd} %{${multiple_cmd} %{${line}}}"
done
if [ -n "${post_action}" ]; then
printf "%s\n" "evaluate-commands -client ${kak_client} %{${post_action}}"
fi

View file

@ -15,6 +15,8 @@ Default value:
grep -RHn" \
str fzf_sk_grep_command 'grep -RHn'
declare-option -hidden str fzf_sk_first_file ''
try %{ declare-user-mode fzf }
evaluate-commands %sh{
@ -38,14 +40,31 @@ define-command -hidden fzf-sk-grep %{ evaluate-commands %sh{
printf "%s\n" "info -title '${title}' '${message}${tmux_keybindings}'"
[ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s"
printf "%s\n" "fzf -kak-cmd %{fzf-sk-grep-handler} -fzf-impl %{sk -i -c '$kak_opt_fzf_sk_grep_command {}'} -fzf-args %{--expect ctrl-w $additional_flags}"
printf "%s\n" "fzf -kak-cmd %{fzf-sk-grep-handler} -fzf-impl %{sk -m -i -c '$kak_opt_fzf_sk_grep_command {}'} -fzf-args %{--expect ctrl-w $additional_flags} -multiple-cmd %{fzf-sk-populate-grep} -post-action %{buffer %opt{fzf_sk_first_file}}"
}}
define-command -hidden fzf-sk-grep-handler -params 1 %{ evaluate-commands %sh{
printf "%s\n" "$1" | awk '{
file = $0; sub(/:.*/, "", file); gsub("&", "&&", file);
line = $0; sub(/[^:]+:/, "", line); sub(/:.*/, "", line)
print "edit -existing %&" file "&; execute-keys %&" line "gxvc&";
}'
}}
define-command -hidden fzf-sk-grep-handler -params 1 %{
evaluate-commands %sh{
printf "%s\n" "$1" | awk '{
file = $0; sub(/:.*/, "", file); gsub("&", "&&", file);
line = $0; sub(/[^:]+:/, "", line); sub(/:.*/, "", line)
print "edit -existing %&" file "&; execute-keys %&" line "gxvc&"
print "set-option global fzf_sk_first_file %&" file "&"
}'
}
fzf-sk-populate-grep %arg{1}
}
define-command -hidden fzf-sk-populate-grep -params 1 %{
try %{
buffer *grep*
} catch %{
edit -scratch *grep*
set-option buffer filetype grep
}
evaluate-commands -buffer *grep* %{
set-register dquote %arg{1}
execute-keys gjPo
}
}