From d855e32b909955c97ef9976cb5f8704cb36a7736 Mon Sep 17 00:00:00 2001 From: andreyorst Date: Mon, 24 Sep 2018 10:37:45 +0300 Subject: [PATCH 1/6] WIP x11 support --- rc/fzf.kak | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index 16754ba..94f2ed2 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -11,21 +11,20 @@ # │ different fzf commands. │ # ╰─────────────────────────────────╯ -# New mode -evaluate-commands %sh{ modes="_ "$kak_user_modes; [ -z "${modes##*fzf*}" ] || echo declare-user-mode fzf } +declare-user-mode fzf # Options declare-option -docstring "command to provide list of files to fzf. Supported tools: - find - GNU Find - ag - The Silver Searcher - rg - ripgrep + find - GNU Find + ag - The Silver Searcher + rg - ripgrep Arguments are also can be passed along with it. Default arguments are: - find -type f - ag -l -f --hidden --one-device . - rg -L --hidden --files + find -type f + ag -l -f --hidden --one-device . + rg -L --hidden --files " \ str fzf_file_command "find" @@ -43,14 +42,14 @@ map global fzf -docstring "change directory" c ': fzf-cd' map global fzf -docstring "edif file in git tree" g ': fzf-git' # Commands -define-command -override -docstring \ +define-command -docstring \ "fzf-mode: Enter fzf-mode This is to be used in mappings to enter fzf-mode For example: map global normal ': fzf-mode' " \ fzf-mode %{ evaluate-commands 'enter-user-mode fzf' } -define-command -override -hidden -docstring \ +define-command -hidden -docstring \ "fzf-file: Run fzf to open file Configurable options: fzf_file_command: command to run with fzf to list possible files. @@ -83,37 +82,41 @@ fzf-file %{ } } -define-command -override -hidden fzf-git %{ +define-command -hidden fzf-git %{ fzf "edit $1" "git ls-tree --name-only -r HEAD" } -define-command -override -hidden fzf-tag %{ +define-command -hidden fzf-tag %{ fzf "ctags-search $1" "readtags -l | cut -f1 | sort -u" } -define-command -override -hidden fzf-cd %{ +define-command -hidden fzf-cd %{ fzf "change-directory $1" "find \( -path '*/.svn*' -o -path '*/.git*' \) -prune -o -type d -print" } -define-command -override -hidden fzf -params 2 %{ evaluate-commands %sh{ - if [ -z "${kak_client_env_TMUX}" ]; then - echo 'fail "client was not started under tmux"' - exit +define-command -hidden fzf -params 2 %{ evaluate-commands %sh{ + callback=$1 + items_command=$2 + if [ ! -z "${kak_client_env_TMUX}" ]; then + cmd="$items_command | fzf-tmux -d 15 --color=16" + fi + if [ ! -z "${kak_opt_termcmd}" ]; then + cmd="$kak_opt_termcmd \"sh -c '$items_command | fzf'\"" + else + echo "fail termcmd option is not set" + # exit fi tmp=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-fzf.XXXXXX)) exec=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-exec.XXXXXX)) - callback=$1; shift - items_command=$1; shift - if [ -z $(command -v $(echo $items_command | head -n 1)) ]; then - eval echo fail "\'$(echo $items_command | head -n 1)' executable not found. Is it installed?" + items_executable=$(echo $items_command | awk '{print $1}') + if [ -z $(command -v $items_executable) ]; then + eval echo fail "\'$items_executable' executable not found. Is it installed?" exit fi - flags='--color=16' - [ -z "${@##* -multi*}" ] && flags="$flags -m" echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec chmod 755 $exec ( - eval "$items_command | fzf-tmux -d 15 $flags > $tmp" + eval "$cmd > $tmp" (while read file; do $exec $file done) < $tmp @@ -122,7 +125,7 @@ define-command -override -hidden fzf -params 2 %{ evaluate-commands %sh{ ) > /dev/null 2>&1 < /dev/null & }} -define-command -override -hidden fzf-buffer %{ evaluate-commands %sh{ +define-command -hidden fzf-buffer %{ evaluate-commands %sh{ if [ -z "${kak_client_env_TMUX}" ]; then echo 'fail "client was not started under tmux"' exit From 9f8472d8ac12a8d993d19c6b189ec9983e20ea2e Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 24 Sep 2018 11:44:28 +0300 Subject: [PATCH 2/6] support x11 via termcmd --- rc/fzf.kak | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index 94f2ed2..b866cb5 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -97,17 +97,16 @@ define-command -hidden fzf-cd %{ define-command -hidden fzf -params 2 %{ evaluate-commands %sh{ callback=$1 items_command=$2 - if [ ! -z "${kak_client_env_TMUX}" ]; then - cmd="$items_command | fzf-tmux -d 15 --color=16" - fi - if [ ! -z "${kak_opt_termcmd}" ]; then - cmd="$kak_opt_termcmd \"sh -c '$items_command | fzf'\"" - else - echo "fail termcmd option is not set" - # exit - fi 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 > $tmp" + elif [ ! -z "${kak_opt_termcmd}" ]; then + path=$(pwd) + cmd="$kak_opt_termcmd \"sh -c 'cd $path && $items_command | fzf > $tmp'\"" + else + echo "fail termcmd option is not set" + fi items_executable=$(echo $items_command | awk '{print $1}') if [ -z $(command -v $items_executable) ]; then eval echo fail "\'$items_executable' executable not found. Is it installed?" @@ -116,7 +115,7 @@ define-command -hidden fzf -params 2 %{ evaluate-commands %sh{ echo "echo eval -client $kak_client \"$callback\" | kak -p $kak_session" > $exec chmod 755 $exec ( - eval "$cmd > $tmp" + eval "$cmd" (while read file; do $exec $file done) < $tmp From e88a9159aae5560649d8853dc9923c48e7365441 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 24 Sep 2018 11:54:05 +0300 Subject: [PATCH 3/6] add note about X11 support to readme --- README.md | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 00703e1..1f7a1b2 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,31 @@ **fzf.kak** is a plugin for Kakoune editor, that brings integration with fzf tool. This plugin is being tested against Kakoune 2018.09.04. -## Installation -This plugin requires Tmux. Support for X11 is planned in the near future. +### Dependencies -Assuming you're using [plug.kak](https://github.com/andreyorst/plug.kak) plugin -manager, add this to your `.kakrc`: +#### tmux +If you're using tmux make sure, that fzf-tmux script is installed and available. + +#### X11 +Script works with X11 via `termcmd` option, but needs more testing, so think of +it as experimental feature for now. + +#### GNU Screen + +GNU Screen is not yet supported. + +## Installation + +Recommended way to install is to use [plug.kak](https://github.com/andreyorst/plug.kak) plugin +manager. You can install **fzf.kak** by adding this to your `kakrc`: ```kak plug andreyorst/fzf.kak ``` -Reload Kakoune config by and run `:plug-install`. Or install this plugin any other preferred way. +Then reload Kakoune config or restart Kakoune and run `:plug-install`. + +Or install this plugin any other preferred way. ## Usage From 1fa67070f85f4b82647c3c71b0c548b27787827c Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 24 Sep 2018 12:11:28 +0300 Subject: [PATCH 4/6] add support for buffer switching in X11 --- rc/fzf.kak | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/rc/fzf.kak b/rc/fzf.kak index b866cb5..3835e7a 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -125,21 +125,28 @@ define-command -hidden fzf -params 2 %{ evaluate-commands %sh{ }} define-command -hidden fzf-buffer %{ evaluate-commands %sh{ - if [ -z "${kak_client_env_TMUX}" ]; then - echo 'fail "client was not started under tmux"' - exit - fi tmp=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-fzf.XXXXXX)) setbuf=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-setbuf.XXXXXX)) delbuf=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-delbuf.XXXXXX)) + buffers=$(mktemp $(eval echo $kak_opt_fzf_tmp/kak-buffers.XXXXXX)) + items_command="echo $kak_buflist | tr ' ' '\n' | sort" + if [ ! -z "${kak_client_env_TMUX}" ]; then + # cmd="$items_command | fzf-tmux -d 15 --color=16 -e --preview='$setbuf {}' --preview-window=up:hidden --expect ctrl-d > $tmp" + cmd="$items_command | fzf-tmux -d 15 --color=16 --expect ctrl-d > $tmp" + elif [ ! -z "${kak_opt_termcmd}" ]; then + path=$(pwd) + eval "echo $kak_buflist | tr ' ' '\n' | sort > $buffers" + cmd="$kak_opt_termcmd \"sh -c 'cat $buffers | fzf --color=16 --expect ctrl-d > $tmp'\"" + else + echo "fail termcmd option is not set" + fi echo "echo eval -client $kak_client \"buffer \$1\" | kak -p $kak_session" > $setbuf echo "echo eval -client $kak_client \"delete-buffer \$1\" | kak -p $kak_session" > $delbuf echo "echo eval -client $kak_client \"fzf-buffer \" | kak -p $kak_session" >> $delbuf chmod 755 $setbuf chmod 755 $delbuf ( - eval "echo $kak_buflist | tr ' ' '\n' | sort | - fzf-tmux -d 15 --color=16 -e --preview='$setbuf {}' --preview-window=up:hidden --expect ctrl-d > $tmp" + eval "$cmd" if [ -s $tmp ]; then ( read action read buf From 91c7ee8f99789fd3762d3bb9fa448e0d44a84db3 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 24 Sep 2018 12:13:19 +0300 Subject: [PATCH 5/6] remove tmp buffers file --- rc/fzf.kak | 1 + 1 file changed, 1 insertion(+) diff --git a/rc/fzf.kak b/rc/fzf.kak index 3835e7a..a2ceea8 100644 --- a/rc/fzf.kak +++ b/rc/fzf.kak @@ -162,6 +162,7 @@ define-command -hidden fzf-buffer %{ evaluate-commands %sh{ rm $tmp rm $setbuf rm $delbuf + rm $buffers ) > /dev/null 2>&1 < /dev/null & }} From 538c80113fe318f0a54d25333855bcf6a8cdb83c Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Mon, 24 Sep 2018 12:14:39 +0300 Subject: [PATCH 6/6] remove override --- CONTRIBUTING.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 809d50e..2d49e2b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,9 +10,8 @@ want to resource kakrc plugin would not provide any warnings. Good code: ```kak -define-command -override -hidden -docstring \ +define-command -hidden -docstring \ "This is an example of adding new fzf-mode command. - Note that '-override' is used since script should always be resourceable If shell scripting is involved please follow POSIX standards, and test your code in POSIX shells, like 'dash', 'ash', and popular POSIX-compatible shells, like 'bash' and 'zsh' You earn bonus points if your script works in 'fish'. @@ -58,7 +57,7 @@ and fallback to another if not. Is so `fzf` can be called from shell expansion ` Good code: ```kak -define-command -override -hidden -docstring \ +define-command -hidden -docstring \ "This is an example of using fzf command from shell expansion. Note that first argument should be used with escaped '$1'. This is intentional because you will echo whole command out of shell expansion and without escaping '$1'