From 208d4f2173c1630408b836312271b3c675ddc5eb Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 15 Jan 2017 16:15:51 +0900 Subject: [PATCH] [shell] Make layout configurable via $FZF_DEFAULT_OPTS and $FZF_{KEY}_OPTS --- shell/completion.bash | 7 +++---- shell/completion.zsh | 7 +++---- shell/key-bindings.bash | 11 +++++------ shell/key-bindings.fish | 24 ++++++++++++++++++------ shell/key-bindings.zsh | 10 +++++----- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/shell/completion.bash b/shell/completion.bash index 86f8e15..ad778d1 100644 --- a/shell/completion.bash +++ b/shell/completion.bash @@ -35,8 +35,7 @@ bind '"\e[0n": redraw-current-line' __fzfcmd_complete() { [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] && - echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || - echo "fzf --height ${FZF_TMUX_HEIGHT:-40%} --reverse" + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" } _fzf_orig_completion_filter() { @@ -149,7 +148,7 @@ __fzf_generic_path_completion() { leftover=${leftover/#\/} [ -z "$dir" ] && dir='.' [ "$dir" != "/" ] && dir="${dir/%\//}" - matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $2 -q "$leftover" | while read -r item; do + matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $2 -q "$leftover" | while read -r item; do printf "%q$3 " "$item" done) matches=${matches% } @@ -184,7 +183,7 @@ _fzf_complete() { if [[ "$cur" == *"$trigger" ]]; then cur=${cur:0:${#cur}-${#trigger}} - selected=$(cat | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $1 -q "$cur" | $post | tr '\n' ' ') + selected=$(cat | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" $fzf $1 -q "$cur" | $post | tr '\n' ' ') selected=${selected% } # Strip trailing space not to repeat "-o nospace" printf '\e[5n' diff --git a/shell/completion.zsh b/shell/completion.zsh index 9a33a14..850aab6 100644 --- a/shell/completion.zsh +++ b/shell/completion.zsh @@ -32,8 +32,7 @@ fi __fzfcmd_complete() { [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] && - echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || - echo "fzf --height ${FZF_TMUX_HEIGHT:-40%} --reverse" + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" } __fzf_generic_path_completion() { @@ -56,7 +55,7 @@ __fzf_generic_path_completion() { [ -z "$dir" ] && dir='.' [ "$dir" != "/" ] && dir="${dir/%\//}" dir=${~dir} - matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${=fzf} ${=fzf_opts} -q "$leftover" | while read item; do + matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${=fzf} ${=fzf_opts} -q "$leftover" | while read item; do echo -n "${(q)item}$suffix " done) matches=${matches% } @@ -99,7 +98,7 @@ _fzf_complete() { fzf="$(__fzfcmd_complete)" _fzf_feed_fifo "$fifo" - matches=$(cat "$fifo" | FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${=fzf} ${=fzf_opts} -q "${(Q)prefix}" | $post | tr '\n' ' ') + matches=$(cat "$fifo" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" ${=fzf} ${=fzf_opts} -q "${(Q)prefix}" | $post | tr '\n' ' ') if [ -n "$matches" ]; then LBUFFER="$lbuf$matches" fi diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index 382302d..06e9656 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -5,7 +5,7 @@ __fzf_select__() { -o -type f -print \ -o -type d -print \ -o -type l -print 2> /dev/null | cut -b3-"}" - eval "$cmd | fzf --height ${FZF_TMUX_HEIGHT:-40%} --reverse -m $@ $FZF_CTRL_T_OPTS" | while read -r item; do + eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" fzf -m "$@" | while read -r item; do printf '%q ' "$item" done echo @@ -19,8 +19,7 @@ __fzf_use_tmux__() { __fzfcmd() { __fzf_use_tmux__ && - echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || - echo "fzf --height ${FZF_TMUX_HEIGHT:-40%} --reverse" + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" } __fzf_select_tmux__() { @@ -39,7 +38,7 @@ fzf-file-widget() { if __fzf_use_tmux__; then __fzf_select_tmux__ else - local selected="$(__fzf_select__ --height ${FZF_TMUX_HEIGHT:-40%} --reverse)" + local selected="$(__fzf_select__)" READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}" READLINE_POINT=$(( READLINE_POINT + ${#selected} )) fi @@ -49,7 +48,7 @@ __fzf_cd__() { local cmd dir cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ -o -type d -print 2> /dev/null | sed 1d | cut -b3-"}" - dir=$(eval "$cmd | $(__fzfcmd) +m $FZF_ALT_C_OPTS") && printf 'cd %q' "$dir" + dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m) && printf 'cd %q' "$dir" } __fzf_history__() ( @@ -57,7 +56,7 @@ __fzf_history__() ( shopt -u nocaseglob nocasematch line=$( HISTTIMEFORMAT= history | - eval "$(__fzfcmd) +s --tac --no-reverse +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS" | + FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS +s --tac --no-reverse -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS +m" $(__fzfcmd) | command grep '^ *[0-9]') && if [[ $- =~ H ]]; then sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line" diff --git a/shell/key-bindings.fish b/shell/key-bindings.fish index fc61844..3fb7da8 100644 --- a/shell/key-bindings.fish +++ b/shell/key-bindings.fish @@ -21,7 +21,11 @@ function fzf_key_bindings -o -type d -print \ -o -type l -print 2> /dev/null | sed 's#^\./##'" - eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m $FZF_CTRL_T_OPTS" | while read -l r; set result $result $r; end + set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40% + begin + set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" + eval "$FZF_CTRL_T_COMMAND | "(__fzfcmd)" -m" | while read -l r; set result $result $r; end + end if [ -z "$result" ] commandline -f repaint return @@ -39,8 +43,12 @@ function fzf_key_bindings end function fzf-history-widget -d "Show command history" - history | eval (__fzfcmd) +s +m --no-reverse --tiebreak=index $FZF_CTRL_R_OPTS -q '(commandline)' | read -l result - and commandline -- $result + set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40% + begin + set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT $FZF_DEFAULT_OPTS +s --no-reverse --tiebreak=index $FZF_CTRL_R_OPTS +m" + history | eval (__fzfcmd) -q '(commandline)' | read -l result + and commandline -- $result + end commandline -f repaint end @@ -48,8 +56,12 @@ function fzf_key_bindings set -q FZF_ALT_C_COMMAND; or set -l FZF_ALT_C_COMMAND " command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \ -o -type d -print 2> /dev/null | sed 1d | cut -b3-" - eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m $FZF_ALT_C_OPTS" | read -l result - [ "$result" ]; and cd $result + set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40% + begin + set -lx FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" + eval "$FZF_ALT_C_COMMAND | "(__fzfcmd)" +m" | read -l result + [ "$result" ]; and cd $result + end commandline -f repaint end @@ -59,7 +71,7 @@ function fzf_key_bindings if [ $FZF_TMUX -eq 1 ] echo "fzf-tmux -d$FZF_TMUX_HEIGHT" else - echo "fzf --height $FZF_TMUX_HEIGHT --reverse" + echo "fzf" end end diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index 7e24d92..c8705ad 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -9,7 +9,7 @@ __fsel() { -o -type d -print \ -o -type l -print 2> /dev/null | cut -b3-"}" setopt localoptions pipefail 2> /dev/null - eval "$cmd | $(__fzfcmd) -m $FZF_CTRL_T_OPTS" | while read item; do + eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do echo -n "${(q)item} " done local ret=$? @@ -23,8 +23,7 @@ __fzf_use_tmux__() { __fzfcmd() { __fzf_use_tmux__ && - echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || - echo "fzf --height ${FZF_TMUX_HEIGHT:-40%} --reverse" + echo "fzf-tmux -d${FZF_TMUX_HEIGHT:-40%}" || echo "fzf" } fzf-file-widget() { @@ -42,7 +41,7 @@ fzf-cd-widget() { local cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ -o -type d -print 2> /dev/null | sed 1d | cut -b3-"}" setopt localoptions pipefail 2> /dev/null - cd "${$(eval "$cmd | $(__fzfcmd) +m $FZF_ALT_C_OPTS"):-.}" + cd "${$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m):-.}" local ret=$? zle reset-prompt typeset -f zle-line-init >/dev/null && zle zle-line-init @@ -55,7 +54,8 @@ bindkey '\ec' fzf-cd-widget fzf-history-widget() { local selected num setopt localoptions noglobsubst pipefail 2> /dev/null - selected=( $(fc -l 1 | eval "$(__fzfcmd) +s --tac --no-reverse +m -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS -q ${(q)LBUFFER}") ) + selected=( $(fc -l 1 | + FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS +s --tac --no-reverse -n2..,.. --tiebreak=index --toggle-sort=ctrl-r $FZF_CTRL_R_OPTS +m --query=${(q)LBUFFER}" $(__fzfcmd)) ) local ret=$? if [ -n "$selected" ]; then num=$selected[1]