diff --git a/rc/fzf-modules/fzf-file.kak b/rc/fzf-modules/fzf-file.kak index 30a053c..6cbd79f 100644 --- a/rc/fzf-modules/fzf-file.kak +++ b/rc/fzf-modules/fzf-file.kak @@ -57,6 +57,6 @@ define-command -hidden fzf-file %{ evaluate-commands %sh{ printf "%s\n" "info -title 'fzf file' '$message$tmux_keybindings'" [ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s" - printf "%s\n" "fzf %{edit -existing} %{$cmd} %{-m --expect ctrl-w $additional_flags}" + printf "%s\n" "fzf -preview -kak-cmd %{edit -existing} -items-cmd %{$cmd} -fzf-args %{-m --expect ctrl-w $additional_flags}" }} diff --git a/rc/fzf.kak b/rc/fzf.kak index 7dc2a7e..6cea268 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -101,121 +101,6 @@ define-command -hidden fzf-window -params .. %{ } } -define-command -hidden -docstring \ -"fzf [ ]: generic fzf command. -This command can be used to create new fzf wrappers for various Kakoune or external -features. More about arguments: - -: -The is a Kakoune command that should be used after fzf returns some result. -For example to open file chooser we can call fzf with `edit` as a command: -'fzf %{edit} %{}' -After choosing one or more files in fzf, will be used with each of them. - - -This is the shell command that is used to provide list of values to fzf. It can be -any command that provides newline separated list of items, which is then piped to fzf. - - -These are additional flags for fzf program, that are passed to it. You can check them -in fzf manual. - - -This is extra commands that are preformed after fzf finishes and main command was -executed. This can be used to invoke fzf back, like in fzf-cd command, or to execute -any other Kakoune command that is meaningfull in current situation. This is more is -a workaround of problem with executing composite commands, where fzf result should -be in the middle of the command and may be changed or removed it further versions. - -If you want to develop a module with fzf command, feel free to check for existing -module implementations in 'rc/fzf-modules' directory." \ -fzf -params 2..4 %{ evaluate-commands %sh{ - command=$1 - items_command=$2 - additional_flags=$3 - extra_action=$4 - tmux_height=$kak_opt_fzf_tmux_height - - items_executable=$(printf "%s\n" "$items_command" | grep -o -E "[[:alpha:]]+" | head -1) - if [ -z "$(command -v $items_executable)" ]; then - printf "%s\n" "fail %{'$items_executable' executable not found}" - exit - fi - - if [ -z "${command##edit*}" ] && [ $kak_opt_fzf_preview = "true" ]; then - case $kak_opt_fzf_highlight_cmd in - bat) - highlighter="bat --color=always --style=plain {}" ;; - coderay) - highlighter="coderay {}" ;; - highlight) - highlighter="highlight --failsafe -O ansi {}" ;; - rouge) - highlighter="rougify {}" ;; - bat*|coderay*|highlight*|rougify*) - highlighter=$kak_opt_fzf_highlight_cmd ;; - *) - executable=$(printf "%s\n" "$kak_opt_fzf_highlight_cmd" | grep -o -E '[[:alpha:]]+' | head -1) - printf "%s\n" "echo -markup %{{Information}'$executable' highlighter is not supported by the script. fzf.kak may not work as you expect.}" - highlighter=$kak_opt_fzf_highlight_cmd ;; - esac - - tmux_height=$kak_opt_fzf_file_preview_tmux_height - additional_flags="--preview '($highlighter || cat {}) 2>/dev/null | head -n $kak_opt_fzf_preview_lines' --preview-window=\$pos $additional_flags" - fi - - if [ -n "$kak_client_env_TMUX" ]; then - preview_pos="pos=right:$kak_opt_fzf_preview_width;" - else - preview_pos="sleep 0.1; [ \$(tput cols) -gt \$(expr \$(tput lines) \* 2) ] && pos=right:$kak_opt_fzf_preview_width || pos=top:$kak_opt_fzf_preview_height;" - fi - - tmp=$(mktemp ${TMPDIR:-/tmp}/kak-fzf-tmp.XXXXXX) - fzfcmd=$(mktemp ${TMPDIR:-/tmp}/kak-fzfcmd.XXXXXX) - printf "%s\n" "cd \"$PWD\" && $preview_pos $items_command | SHELL=$(command -v sh) $kak_opt_fzf_implementation $additional_flags > $tmp; rm $fzfcmd" > $fzfcmd - chmod 755 $fzfcmd - - if [ -n "$kak_client_env_TMUX" ]; then - [ -n "${tmux_height%%*%}" ] && measure="-l" || measure="-p" - cmd="nop %sh{ command tmux split-window $measure ${tmux_height%%%*} 'sh -c $fzfcmd' }" - else - cmd="terminal %{$fzfcmd}" - fi - - ( - printf "%s\n" "${cmd}" | kak -p ${kak_session} - while [ -e $fzfcmd ]; do - sleep 0.1 - done - if [ -s $tmp ]; then - ( - read action - case $action in - ctrl-w) - wincmd="fzf-window" ;; - ctrl-s) - wincmd="fzf-vertical" ;; - ctrl-v) - wincmd="fzf-horizontal" ;; - *) - if [ -n "$action" ]; then - printf "%s\n" "evaluate-commands -client $kak_client '$command' '$action'" | kak -p $kak_session - [ -n "$extra_action" ] && printf "%s\n" "evaluate-commands -client $kak_client $extra_action" | kak -p $kak_session - fi ;; - esac - kakoune_command() { - 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 - kakoune_command "$item" | kak -p $kak_session - done - ) < $tmp - fi - rm $tmp - ) > /dev/null 2>&1 < /dev/null & -}} - define-command -docstring \ "fzf : generic fzf command. This command can be used to create new fzf wrappers for various Kakoune or external features. @@ -225,11 +110,10 @@ Switches: -fzf-args : Additional flags for fzf program -post-action : Extra commands that are preformed after `-kak-cmd' command. -preview: should fzf window include preview" \ -new-fzf -shell-script-candidates %{echo "-kak-cmd\n-items-cmd\n-fzf-args\n-post-action\n"} -params .. %{ evaluate-commands %sh{ - tmux_height=$kak_opt_fzf_tmux_height +fzf -shell-script-candidates %{echo "-kak-cmd\n-items-cmd\n-fzf-args\n-post-action\n"} -params .. %{ evaluate-commands %sh{ while [ $# -gt 0 ]; do case $1 in - -kak-cmd) shift; kakoune_cmd="${kakoune_cmd} $1" ;; + -kak-cmd) shift; kakoune_cmd="$1" ;; -items-cmd) shift; items_cmd="${items_cmd} $1" ;; -fzf-args) shift; fzf_args="${fzf_args} $1" ;; -post-action) shift; post_action="${post_action} $1" ;; @@ -239,6 +123,13 @@ new-fzf -shell-script-candidates %{echo "-kak-cmd\n-items-cmd\n-fzf-args\n-post- shift done + printf "echo -debug %%{kakoune_cmd: %s}\n" "'$kakoune_cmd'" + printf "echo -debug %%{items_cmd: %s}\n" "'$items_cmd'" + printf "echo -debug %%{fzf_args: %s}\n" "'$fzf_args'" + printf "echo -debug %%{post_action: %s}\n" "'$post_action'" + printf "echo -debug %%{preview: %s}\n" "'$preview'" + printf "echo -debug %%{ignored: %s}\n" "'$ignored'" + if [ "$preview" = "true" ] && [ ${kak_opt_fzf_preview} = "true" ]; then case ${kak_opt_fzf_highlight_cmd} in bat) highlight_cmd="bat --color=always --style=plain {}" ;; @@ -248,7 +139,7 @@ new-fzf -shell-script-candidates %{echo "-kak-cmd\n-items-cmd\n-fzf-args\n-post- *) highlight_cmd="${kak_opt_fzf_highlight_cmd}" ;; esac if [ -n "${kak_client_env_TMUX}" ]; then - [ -z "${kakoune_cmd##edit*}" ] && tmux_height=$kak_opt_fzf_file_preview_tmux_height + [ -z "${kakoune_cmd##edit*}" ] && tmux_height="$kak_opt_fzf_file_preview_tmux_height" preview_position="pos=right:${kak_opt_fzf_preview_width};" else preview_position="sleep 0.1; [ \$(tput cols) -gt \$(expr \$(tput lines) \* 2) ] && pos=right:${kak_opt_fzf_preview_width} || pos=top:${kak_opt_fzf_preview_height};" @@ -263,6 +154,7 @@ new-fzf -shell-script-candidates %{echo "-kak-cmd\n-items-cmd\n-fzf-args\n-post- chmod 755 ${fzfcmd} if [ -n "${kak_client_env_TMUX}" ]; then + [ -z "$tmux_height" ] && tmux_height=$kak_opt_fzf_tmux_height [ -n "${tmux_height%%*%}" ] && measure="-l" || measure="-p" cmd="nop %sh{ command tmux split-window ${measure} ${tmux_height%%%*} 'sh -c ${fzfcmd}' }" else