From 0ea9b9b458f3d36033f73d7dc06102f274a55bb6 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Sat, 2 Mar 2019 22:15:33 +0300 Subject: [PATCH 1/4] initial implementation of #39 --- rc/fzf-modules/sk-grep.kak | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 rc/fzf-modules/sk-grep.kak diff --git a/rc/fzf-modules/sk-grep.kak b/rc/fzf-modules/sk-grep.kak new file mode 100644 index 0000000..1dcdeb6 --- /dev/null +++ b/rc/fzf-modules/sk-grep.kak @@ -0,0 +1,38 @@ +# ╭─────────────╥────────────────────────╮ +# │ Author: ║ File: │ +# │ Andrey Orst ║ sk-grep.kak │ +# ╞═════════════╩════════════════════════╡ +# │ Module running interactive grep with │ +# │ ski for fzf.kak │ +# ╞══════════════════════════════════════╡ +# │ GitHub.com/andreyorst/fzf.kak │ +# ╰──────────────────────────────────────╯ + +declare-option str fzf_sk_grep_command 'grep -r' + +evaluate-commands %sh{ + if [ -n "$(command -v sk)" ]; then + printf "%s\n" "map global fzf -docstring %{Interactive grep with skim} 'g' ': sk-interactive-grep'" + fi +} + +define-command -hidden sk-interactive-grep %{ evaluate-commands %sh{ + if [ -z "$(command -v sk)" ]; then + printf "%s\n" "echo -markup %{{Information}skim required to run this command}" + exit + fi + title="skim interactive grep" + message="Interactively grep pattern from current directory" + printf "%s\n" "info -title '$title' '$message'" + impl=$kak_opt_fzf_implementation + printf "%s\n" "set-option global fzf_implementation sk + fzf %{fzf-sk-grep-handler} %{echo >/dev/null 2>&1} %{-i -c '$kak_opt_fzf_sk_grep_command {}'} + set-option global fzf_implementation $impl" +}} + +define-command fzf-sk-grep-handler -params 1 %{ evaluate-commands %sh{ + file="${1%%:*}" + pattern="${1##:*}" + printf "%s\n" "edit -existing %{$file}; execute-keys /\Q$pattern" +}} + From fe29c469034f85240498b866d09cba70fc342d83 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Sun, 3 Mar 2019 21:31:23 +0300 Subject: [PATCH 2/4] add working skim handler --- rc/fzf-modules/sk-grep.kak | 30 ++++++++++++++++++++++-------- rc/fzf.kak | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/rc/fzf-modules/sk-grep.kak b/rc/fzf-modules/sk-grep.kak index 1dcdeb6..1cf8aa0 100644 --- a/rc/fzf-modules/sk-grep.kak +++ b/rc/fzf-modules/sk-grep.kak @@ -8,7 +8,10 @@ # │ GitHub.com/andreyorst/fzf.kak │ # ╰──────────────────────────────────────╯ -declare-option str fzf_sk_grep_command 'grep -r' +declare-option -docstring "what command to use to provide list of grep search matches. + + Default value: 'grep -r'" \ +str fzf_sk_grep_command 'grep -r' evaluate-commands %sh{ if [ -n "$(command -v sk)" ]; then @@ -22,17 +25,28 @@ define-command -hidden sk-interactive-grep %{ evaluate-commands %sh{ exit fi title="skim interactive grep" - message="Interactively grep pattern from current directory" - printf "%s\n" "info -title '$title' '$message'" + message="Interactively grep pattern from current directory +: open search result in new buffer. +: open search result in new window" + [ ! -z "${kak_client_env_TMUX}" ] && tmux_keybindings=" +: open search result in horizontal split +: open search result in vertical split" + + printf "%s\n" "info -title '${title}' '${message}${tmux_keybindings}'" + [ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s" impl=$kak_opt_fzf_implementation - printf "%s\n" "set-option global fzf_implementation sk - fzf %{fzf-sk-grep-handler} %{echo >/dev/null 2>&1} %{-i -c '$kak_opt_fzf_sk_grep_command {}'} + printf "%s\n" "set-option global fzf_implementation \"sk -i -c '$kak_opt_fzf_sk_grep_command {}'\" + fzf %{fzf-sk-grep-handler} %{echo >/dev/null 2>&1} %{--expect ctrl-w $additional_flags} set-option global fzf_implementation $impl" }} define-command fzf-sk-grep-handler -params 1 %{ evaluate-commands %sh{ - file="${1%%:*}" - pattern="${1##:*}" - printf "%s\n" "edit -existing %{$file}; execute-keys /\Q$pattern" + printf "%s\n" "$1" | awk '{ + file = $0; sub(/:.*/, "", file); gsub("&", "&&", file); + keys = $0; sub(/[^:]+:/, "", keys); gsub(/", keys); gsub(/\t/, "", keys); gsub("&", "&&", keys); gsub("\\\\/", "/", keys); + # print "echo -debug %&" file "&"; + # print "echo -debug %&" keys "&"; + print "edit -existing %&" file "&; execute-keys %&/\\Q" keys "vc&"; + }' }} diff --git a/rc/fzf.kak b/rc/fzf.kak index 7d3c5ec..9e96967 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -209,7 +209,7 @@ fzf -params 2..4 %{ evaluate-commands %sh{ fi ;; esac kakoune_command() { - printf "%s\n" "evaluate-commands -client $kak_client $wincmd $command %{$1}" + printf "%s\n" "evaluate-commands -client $kak_client $wincmd %{$command %{$1}}" [ -n "$extra_action" ] && printf "%s\n" "evaluate-commands -client $kak_client $extra_action" } while read item; do From c134913150752bd1e8f2539f0a82442548436b7d Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Sun, 3 Mar 2019 21:50:20 +0300 Subject: [PATCH 3/4] use line number in grep command, because it is more safe --- rc/fzf-modules/sk-grep.kak | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rc/fzf-modules/sk-grep.kak b/rc/fzf-modules/sk-grep.kak index 1cf8aa0..39f9984 100644 --- a/rc/fzf-modules/sk-grep.kak +++ b/rc/fzf-modules/sk-grep.kak @@ -9,9 +9,11 @@ # ╰──────────────────────────────────────╯ declare-option -docstring "what command to use to provide list of grep search matches. +Grep output must follow the format of 'filename:line-number:text' - Default value: 'grep -r'" \ -str fzf_sk_grep_command 'grep -r' +Default value: + grep -RHn" \ +str fzf_sk_grep_command 'grep -RHn' evaluate-commands %sh{ if [ -n "$(command -v sk)" ]; then @@ -43,10 +45,8 @@ define-command -hidden sk-interactive-grep %{ evaluate-commands %sh{ define-command fzf-sk-grep-handler -params 1 %{ evaluate-commands %sh{ printf "%s\n" "$1" | awk '{ file = $0; sub(/:.*/, "", file); gsub("&", "&&", file); - keys = $0; sub(/[^:]+:/, "", keys); gsub(/", keys); gsub(/\t/, "", keys); gsub("&", "&&", keys); gsub("\\\\/", "/", keys); - # print "echo -debug %&" file "&"; - # print "echo -debug %&" keys "&"; - print "edit -existing %&" file "&; execute-keys %&/\\Q" keys "vc&"; + line = $0; sub(/[^:]+:/, "", line); sub(/:.*/, "", line) + print "edit -existing %&" file "&; execute-keys %&" line "gxvc&"; }' }} From f50d455f36e7cf8defdd30baa468e524f6f7d8b1 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Wed, 6 Mar 2019 14:38:11 +0300 Subject: [PATCH 4/4] fix tmux splitting parameter --- rc/fzf.kak | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index 9e96967..5453d23 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -176,7 +176,7 @@ fzf -params 2..4 %{ evaluate-commands %sh{ chmod 755 $fzfcmd if [ -n "$kak_client_env_TMUX" ]; then - [ -n "${tmux_height%%*%}" ] && measure="-p" || measure="-p" + [ -n "${tmux_height%%*%}" ] && measure="-l" || measure="-p" cmd="command tmux split-window $measure ${tmux_height%%%*} 'sh -c $fzfcmd'" elif [ -n "$kak_opt_termcmd" ]; then cmd="$kak_opt_termcmd 'sh -c $fzfcmd'"