From 32c3f837c98d35bbdd5538f9e1e90c981704e953 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Thu, 11 Oct 2018 13:37:44 +0300 Subject: [PATCH] move preview part into base fzf function --- rc/fzf.kak | 110 ++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 57 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index 450d543..8923562 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -43,8 +43,8 @@ str fzf_git_command "git" declare-option -docstring "command to provide list of files in svn repository to fzf. Arguments are supported Supported tools: - : : - Subversion: ""svn"" + : : + Subversion: ""svn"" Default arguments: ""svn list -R . | grep -v '$/' | tr '\\n' '\\0'"" @@ -53,8 +53,8 @@ str fzf_svn_command "svn" declare-option -docstring "command to provide list of files in mercurial repository to fzf. Arguments are supported Supported tools: - : : - Mercurial SCM: ""hg"" + : : + Mercurial SCM: ""hg"" Default arguments: ""hg locate -f -0 -I .hg locate -f -0 -I ."" @@ -63,8 +63,8 @@ str fzf_hg_command "hg" declare-option -docstring "command to provide list of files in GNU Bazaar repository to fzf. Arguments are supported Supported tools: - : : - GNU Bazaar: ""bzr"" + : : + GNU Bazaar: ""bzr"" Default arguments: ""bzr ls -R --versioned -0"" @@ -83,9 +83,9 @@ str fzf_tag_command "readtags" declare-option -docstring "allow showing preview window Default value: - false + true " \ -bool fzf_preview false +bool fzf_preview true declare-option -docstring "amount of lines to pass to preview window Default value: 100" \ @@ -148,25 +148,19 @@ define-command -hidden fzf-file %{ fi case $kak_opt_fzf_file_command in find) - cmd="find -type f" - ;; + cmd="find -type f" ;; ag) - cmd="ag -l -f --hidden --one-device . " - ;; + cmd="ag -l -f --hidden --one-device . " ;; rg) - cmd="rg -L --hidden --files" - ;; + cmd="rg -L --hidden --files" ;; fd) - cmd="fd --type f --follow" - ;; + cmd="fd --type f --follow" ;; find*|ag*|rg*|fd*) - cmd=$kak_opt_fzf_file_command - ;; + cmd=$kak_opt_fzf_file_command ;; *) executable=$(echo $kak_opt_fzf_file_command | awk '{print $1}'| tr '(' ' ' | cut -d " " -f 2) echo "echo -markup '{Information}''$executable'' is not supported by the script. fzf.kak may not work as you expect.'" - cmd=$kak_opt_fzf_file_command - ;; + cmd=$kak_opt_fzf_file_command ;; esac title="fzf file" [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" @@ -177,35 +171,20 @@ define-command -hidden fzf-file %{ : open file in new window $additional_keybindings" echo "info -title '$title' '$message'" [ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s" - if [ $kak_opt_fzf_preview = "true" ]; then - case $kak_opt_fzf_highlighter in - coderay) - highlighter="coderay {}" - ;; - highlight) - highlighter="highlight --failsafe -O ansi {}" - ;; - rouge) - highlighter="rougify {}" - ;; - coderay*|highlight*|rougify*) - highlighter=$kak_opt_fzf_highlighter - ;; - *) - executable=$(echo $kak_opt_fzf_highlighter | awk '{print $1}'| tr '(' ' ' | cut -d " " -f 2) - echo "echo -markup '{Information}''$executable'' highlighter is not supported by the script. fzf.kak may not work as you expect.'" - highlighter=$kak_opt_fzf_highlighter - ;; - esac - cmd="sleep 0.1; if [ \$(tput cols) -gt \$(expr \$(tput lines) \\* 2) ]; then pos=right:50%%; else pos=top:60%%; fi; $cmd" - preview_opt="--preview '($highlighter || cat {}) 2>/dev/null | head -n $kak_opt_fzf_preview_lines' --preview-window=\$pos" - additional_flags="$preview_opt $additional_flags" - fi eval echo 'fzf \"edit \$1\" \"$cmd\" \"-m --expect ctrl-w $additional_flags\"' } } -define-command -hidden fzf-vcs %{ +define-command -docstring "Wrapper command for fzf vcs to automatically decect +used version control system. + +Supported vcs: + Git --fast-version-control: ""git"" + Subversion: ""svn"" + Mercurial SCM: ""hg"" + GNU Bazaar: ""bzr"" +" \ +-hidden fzf-vcs %{ evaluate-commands %sh{ commands="git rev-parse --is-inside-work-tree svn info @@ -297,15 +276,12 @@ define-command -hidden fzf-tag %{ evaluate-commands %sh{ case $kak_opt_fzf_tag_command in readtags) - cmd="readtags -l | cut -f1 | sort -u" - ;; + cmd="readtags -l | cut -f1 | sort -u" ;; readtags*) - cmd=$kak_opt_fzf_tag_command - ;; + cmd=$kak_opt_fzf_tag_command ;; *) echo "echo -markup '{Information}$kak_opt_fzf_tag_command is not supported by the script. fzf.kak may not work as you expect." - cmd=$kak_opt_fzf_tag_command - ;; + cmd=$kak_opt_fzf_tag_command ;; esac title="fzf tag" [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" @@ -338,9 +314,7 @@ define-command -hidden fzf-buffer-search %{ buffer_content=$(mktemp ${TMPDIR:-/tmp}/kak-curr-buff.XXXXXX) echo "execute-keys %{%cat>$buffer_content;}" echo "execute-keys $line g $char l" - echo "fzf \"execute-keys \$1 gx\" \"(nl -b a -n ln $buffer_content\" \"--reverse | cut -f 1)\"" - # sleep 1 is needed to because everything is done asynchronously, so file should not be deleted until it was read by fzf - echo "nop %sh{sleep 1; rm $buffer_content}" + echo "fzf \"execute-keys \$1 gx\" \"(nl -b a -n ln $buffer_content\" \"--reverse | cut -f 1; rm $buffer_content)\"" } } @@ -349,7 +323,6 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ items_command=$2 additional_flags=$3 - # 'tr' - if '(cmd1 && cmd2) | fzf' was passed 'awk' will return '(cmd1' items_executable=$(echo $items_command | awk '{print $1}' | tr '(' ' ' | cut -d " " -f 2) if [ -z $(command -v $items_executable) ]; then echo "fail \'$items_executable' executable not found" @@ -359,12 +332,35 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ tmp=$(mktemp $(eval echo ${TMPDIR:-/tmp}/kak-fzf.XXXXXX)) exec=$(mktemp $(eval echo ${TMPDIR:-/tmp}/kak-exec.XXXXXX)) + if [ "$(echo $items_command | awk 'print $1')" = "edit" ] && [ $kak_opt_fzf_preview = "true" ]; then + case $kak_opt_fzf_highlighter in + coderay) + highlighter="coderay {}" ;; + highlight) + highlighter="highlight --failsafe -O ansi {}" ;; + rouge) + highlighter="rougify {}" ;; + coderay*|highlight*|rougify*) + highlighter=$kak_opt_fzf_highlighter ;; + *) + executable=$(echo $kak_opt_fzf_highlighter | awk '{print $1}'| tr '(' ' ' | cut -d " " -f 2) + echo "echo -markup '{Information}''$executable'' highlighter is not supported by the script. fzf.kak may not work as you expect.'" + highlighter=$kak_opt_fzf_highlighter ;; + esac + if [ ! -z "${kak_client_env_TMUX}" ]; then + preview_pos='pos=right:50%;' + else + preview_pos='sleep 0.1; if [ $(tput cols) -gt $(expr $(tput lines) * 2) ]; then pos=right:50%; else pos=top:60%; fi;' + fi + additional_flags="--preview '($highlighter || cat {}) 2>/dev/null | head -n $kak_opt_fzf_preview_lines' --preview-window=\$pos $additional_flags" + fi + if [ ! -z "${kak_client_env_TMUX}" ]; then - cmd="$items_command | fzf-tmux -d $kak_opt_fzf_tmux_height --expect ctrl-q $additional_flags > $tmp" + cmd="$preview_pos $items_command | fzf-tmux -d $kak_opt_fzf_tmux_height --expect ctrl-q $additional_flags > $tmp" elif [ ! -z "${kak_opt_termcmd}" ]; then path=$(pwd) additional_flags=$(echo $additional_flags | sed "s:\$pos:\\\\\$pos:") - cmd="$kak_opt_termcmd \"sh -c \\\"cd $path && $items_command | fzf --expect ctrl-q $additional_flags > $tmp\\\"\"" + cmd="$kak_opt_termcmd \"sh -c \\\"cd $path && $preview_pos $items_command | fzf --expect ctrl-q $additional_flags > $tmp\\\"\"" else echo "fail termcmd option is not set" exit