From dedc704f1e97df428cae46bb11ea2095cad892a9 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Tue, 25 Sep 2018 22:20:21 +0300 Subject: [PATCH 1/5] add fuzzy buffer search --- rc/fzf.kak | 88 +++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index 78e8385..c665045 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -58,6 +58,7 @@ map global fzf -docstring "open buffer" b ': fzf-buffer' map global fzf -docstring "change directory" c ': fzf-cd' map global fzf -docstring "open file" f ': fzf-file' map global fzf -docstring "edif file in git tree" g ': fzf-git' +map global fzf -docstring "search in buffer" s ': fzf-buffer-search' map global fzf -docstring "find tag" t ': fzf-tag' # Commands @@ -93,7 +94,16 @@ define-command -hidden fzf-file %{ cmd=$kak_opt_fzf_file_command ;; esac - eval echo 'fzf \"edit \$1\" \"$cmd\"' + title="fzf file" + [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" +: open file in horizontal split +: open file in vertical split" + message="Open single or multiple files. +: open file in new buffer. +: 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" + eval echo 'fzf \"edit \$1\" \"$cmd\" \"-m --expect ctrl-w $additional_flags\"' } } @@ -111,7 +121,15 @@ define-command -hidden fzf-git %{ cmd=$kak_opt_fzf_git_command ;; esac - eval echo 'fzf \"edit \$1\" \"$cmd\"' + title="fzf git" + [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" +: open file in horizontal split +: open file in vertical split" + message="Open single or multiple files from git tree. +: open file in new buffer. +: open file in new window $additional_keybindings" + echo "info -title '$title' '$message'" + eval echo 'fzf \"edit \$1\" \"$cmd\" \"-m --expect ctrl-v --expect ctrl-s\"' } } @@ -129,16 +147,39 @@ define-command -hidden fzf-tag %{ cmd=$kak_opt_fzf_tag_command ;; esac - eval echo 'fzf \"ctags-search \$1\" \"$cmd\"' + title="fzf tag" + [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" +: open tag in horizontal split +: open tag in vertical split" + message="Jump to a symbol''s definition. +: open tag in new buffer. +: open tag in new window $additional_keybindings" + echo "info -title '$title' '$message'" + eval echo 'fzf \"ctags-search \$1\" \"$cmd\" \"--expect ctrl-v --expect ctrl-s\"' } } define-command -hidden fzf-cd %{ + evaluate-commands %sh{ + title="fzf change directory" + message="Change the server''s working directory" + echo "info -title '$title' '$message'" + } fzf "cd $1" "(echo .. && find \( -path '*/.svn*' -o -path '*/.git*' \) -prune -o -type d -print)" } -define-command -hidden fzf -params 2 %{ evaluate-commands %sh{ +define-command fzf-buffer-search %{ + evaluate-commands %sh{ + title="fzf buffer search" + message="Search buffer with fzf, and jump to result location" + echo "info -title '$title' '$message'" + } + fzf "execute-keys $1 gx" "nl -b a -n ln %val{buffile}" "--reverse | cut -f '1'" +} + +define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ callback=$1 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) @@ -147,49 +188,14 @@ define-command -hidden fzf -params 2 %{ evaluate-commands %sh{ exit fi - case $callback in - cd*) - title="fzf change directory" - message="Change the server''s working directory" - additional_flags= - ;; - ctags-search*) - title="fzf tag" - [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" -: open tag in horizontal split -: open tag in vertical split" - message="Jump to a symbol''s definition. -: open tag in new buffer. -: open tag in new window $additional_keybindings" - additional_flags="--expect ctrl-v --expect ctrl-s" - ;; - edit*) - title="fzf edit" - [ "$items_executable" = "git" ] && additional_info=" from git tree" - [ ! -z "${kak_client_env_TMUX}" ] && additional_keybindings=" -: open file in horizontal split -: open file in vertical split" - message="Open single or multiple files$additional_info. -: open file in new buffer. -: open file in new window $additional_keybindings" - additional_flags="-m --expect ctrl-v --expect ctrl-s" - ;; - *) - title="fzf unknown command" - message="This command is not known by fzf.kak plugin. You can send a PR with it to https://github.com/andreyorst/fzf.kak" - ;; - esac - - echo "info -title '$title' '$message'" - tmp=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-fzf.XXXXXX)) exec=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-exec.XXXXXX)) if [ ! -z "${kak_client_env_TMUX}" ]; then - cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-w $additional_flags> $tmp" + cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-w $additional_flags > $tmp" elif [ ! -z "${kak_opt_termcmd}" ]; then path=$(pwd) - cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 -m --expect ctrl-w > $tmp'\"" + cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 -m --expect ctrl-w $additional_flags> $tmp'\"" else echo "fail termcmd option is not set" fi From e28e055c4ab1a97d4fded3b334d35f5895104186 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Tue, 25 Sep 2018 22:48:38 +0300 Subject: [PATCH 2/5] fix some errors --- rc/fzf.kak | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index c665045..baca7e6 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -11,7 +11,7 @@ # │ different fzf commands. │ # ╰─────────────────────────────────╯ -try %{ declare-user-mode fzf } +try %{ declare-user-mode fzf } catch %{fail "Can't declare mode 'fzf' - already exists"} # Options declare-option -docstring "command to provide list of files to fzf. Arguments are supported @@ -101,7 +101,7 @@ define-command -hidden fzf-file %{ message="Open single or multiple files. : open file in new buffer. : open file in new window $additional_keybindings" - echo "info -title '$title' '$message'" + echo "info -title '$title' '$message'" [ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s" eval echo 'fzf \"edit \$1\" \"$cmd\" \"-m --expect ctrl-w $additional_flags\"' } @@ -128,8 +128,9 @@ define-command -hidden fzf-git %{ message="Open single or multiple files from git tree. : open file in new buffer. : open file in new window $additional_keybindings" - echo "info -title '$title' '$message'" - eval echo 'fzf \"edit \$1\" \"$cmd\" \"-m --expect ctrl-v --expect ctrl-s\"' + echo "info -title '$title' '$message'" + [ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s" + eval echo 'fzf \"edit \$1\" \"$cmd\" \"-m --expect ctrl-w $additional_flags\"' } } @@ -155,9 +156,11 @@ define-command -hidden fzf-tag %{ : open tag in new buffer. : open tag in new window $additional_keybindings" echo "info -title '$title' '$message'" - eval echo 'fzf \"ctags-search \$1\" \"$cmd\" \"--expect ctrl-v --expect ctrl-s\"' + [ ! -z "${kak_client_env_TMUX}" ] && additional_flags="--expect ctrl-v --expect ctrl-s" + eval echo 'fzf \"ctags-search \$1\" \"$cmd\" \"--expect ctrl-w $additional_flags\"' } } + define-command -hidden fzf-cd %{ evaluate-commands %sh{ title="fzf change directory" @@ -167,7 +170,7 @@ define-command -hidden fzf-cd %{ fzf "cd $1" "(echo .. && find \( -path '*/.svn*' -o -path '*/.git*' \) -prune -o -type d -print)" } -define-command fzf-buffer-search %{ +define-command -hidden fzf-buffer-search %{ evaluate-commands %sh{ title="fzf buffer search" message="Search buffer with fzf, and jump to result location" @@ -192,10 +195,10 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ exec=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-exec.XXXXXX)) if [ ! -z "${kak_client_env_TMUX}" ]; then - cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-w $additional_flags > $tmp" + cmd="$items_command | fzf-tmux -d 15 --color=16 $additional_flags > $tmp" elif [ ! -z "${kak_opt_termcmd}" ]; then path=$(pwd) - cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 -m --expect ctrl-w $additional_flags> $tmp'\"" + cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 $additional_flags> $tmp'\"" else echo "fail termcmd option is not set" fi From 6a15b1c7afe264fbd2982fd2c85f54b4b73b3f6f Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Tue, 25 Sep 2018 23:32:57 +0300 Subject: [PATCH 3/5] workaround weird error --- rc/fzf.kak | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index baca7e6..3201bc5 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -195,10 +195,10 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ exec=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-exec.XXXXXX)) if [ ! -z "${kak_client_env_TMUX}" ]; then - cmd="$items_command | fzf-tmux -d 15 --color=16 $additional_flags > $tmp" + cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-w $additional_flags > $tmp" elif [ ! -z "${kak_opt_termcmd}" ]; then path=$(pwd) - cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 $additional_flags> $tmp'\"" + cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 --expect ctrl-w $additional_flags > $tmp'\"" else echo "fail termcmd option is not set" fi From 95b4e05c7f8900288d905a8d54d03eba8900bbaf Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Thu, 27 Sep 2018 12:00:49 +0300 Subject: [PATCH 4/5] replace cd alias with change-directory. Do not close fzf when directory changed --- rc/fzf.kak | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index 3201bc5..c8b4b9f 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -167,7 +167,7 @@ define-command -hidden fzf-cd %{ message="Change the server''s working directory" echo "info -title '$title' '$message'" } - fzf "cd $1" "(echo .. && find \( -path '*/.svn*' -o -path '*/.git*' \) -prune -o -type d -print)" + fzf "change-directory $1" "(echo .. && find \( -path '*/.svn*' -o -path '*/.git*' \) -prune -o -type d -print)" } define-command -hidden fzf-buffer-search %{ @@ -208,7 +208,7 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ if [ -s $tmp ]; then ( read action - if [ "${callback% *}" != "cd" ]; then + if [ "${callback% *}" != "change-directory" ]; then case $action in "ctrl-w") wincmd="x11-new" @@ -221,8 +221,11 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ wincmd= ;; esac callback="$wincmd $callback" + echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec + else + echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec + echo "echo eval -client $kak_client \"fzf-cd\" | kak -p $kak_session" >> $exec fi - echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec chmod 755 $exec while read file; do $exec $file From 1133348289e872ccf9be8ababe9836ec62961801 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Thu, 27 Sep 2018 13:57:27 +0300 Subject: [PATCH 5/5] workaround problem search and cd --- rc/fzf.kak | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index c8b4b9f..dca9fff 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -176,7 +176,7 @@ define-command -hidden fzf-buffer-search %{ message="Search buffer with fzf, and jump to result location" echo "info -title '$title' '$message'" } - fzf "execute-keys $1 gx" "nl -b a -n ln %val{buffile}" "--reverse | cut -f '1'" + fzf "execute-keys $1 gx" "nl -b a -n ln %val{buffile}" "--reverse | awk '{print $1}'" } define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ @@ -195,13 +195,14 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ exec=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-exec.XXXXXX)) if [ ! -z "${kak_client_env_TMUX}" ]; then - cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-w $additional_flags > $tmp" + cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-q $additional_flags > $tmp" elif [ ! -z "${kak_opt_termcmd}" ]; then path=$(pwd) - cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 --expect ctrl-w $additional_flags > $tmp'\"" + cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf --color=16 --expect ctrl-q $additional_flags > $tmp'\"" else echo "fail termcmd option is not set" fi + echo $cmd > ~/cmd ( eval "$cmd" @@ -211,16 +212,16 @@ define-command -hidden fzf -params 2..3 %{ evaluate-commands %sh{ if [ "${callback% *}" != "change-directory" ]; then case $action in "ctrl-w") - wincmd="x11-new" - [ ! -z "${kak_client_env_TMUX}" ] && wincmd="tmux-new-window" ;; + wincmd="x11-new " + [ ! -z "${kak_client_env_TMUX}" ] && wincmd="tmux-new-window " ;; "ctrl-s") - wincmd="tmux-new-vertical" ;; + wincmd="tmux-new-vertical " ;; "ctrl-v") - wincmd="tmux-new-horizontal" ;; + wincmd="tmux-new-horizontal " ;; *) wincmd= ;; esac - callback="$wincmd $callback" + callback="$wincmd$callback" echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec else echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec