Skip to content

Commit

Permalink
[shell] update to fzf's latest scripts(20181022)
Browse files Browse the repository at this point in the history
  • Loading branch information
lotabout committed Oct 22, 2018
1 parent 929482e commit 0431f69
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 46 deletions.
33 changes: 21 additions & 12 deletions shell/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ __skimcmd_complete() {
echo "sk-tmux -d${SKIM_TMUX_HEIGHT:-40%}" || echo "sk"
}

_skim_orig_completion_filter() {
sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _skim_orig_completion_\3="\1 %s \3 #\2";/' |
awk -F= '{gsub(/[^A-Za-z0-9_= ;]/, "_", $1); print $1"="$2}'
__skim_orig_completion_filter() {
sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _skim_orig_completion_\3="\1 %s \3 #\2"; [[ "\1" = *" -o nospace "* ]] \&\& [[ ! "$__skim_nospace_commands" = *" \3 "* ]] \&\& __skim_nospace_commands="$__skim_nospace_commands \3 ";/' |
awk -F= '{OFS = FS} {gsub(/[^A-Za-z0-9_= ;]/, "_", $1);}1'
}

_skim_opts_completion() {
Expand Down Expand Up @@ -111,7 +111,7 @@ _skim_opts_completion() {
}

_skim_handle_dynamic_completion() {
local cmd orig_var orig ret orig_cmd
local cmd orig_var orig ret orig_cmd orig_complete
cmd="$1"
shift
orig_cmd="$1"
Expand All @@ -120,10 +120,18 @@ _skim_handle_dynamic_completion() {
if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then
$orig "$@"
elif [ -n "$_skim_completion_loader" ]; then
orig_complete=$(complete -p "$cmd" 2> /dev/null)
_completion_loader "$@"
ret=$?
eval "$(complete | command grep "\-F.* $orig_cmd$" | _skim_orig_completion_filter)"
source "${BASH_SOURCE[0]}"
# _completion_loader may not have updated completion for the command
if [ "$(complete -p "$cmd" 2> /dev/null)" != "$orig_complete" ]; then
eval "$(complete | command grep " -F.* $orig_cmd$" | __skim_orig_completion_filter)"
if [[ "$__skim_nospace_commands" = *" $orig_cmd "* ]]; then
eval "${orig_complete/ -F / -o nospace -F }"
else
eval "$orig_complete"
fi
fi
return $ret
fi
}
Expand All @@ -139,7 +147,7 @@ __skim_generic_path_completion() {
base=${cur:0:${#cur}-${#trigger}}
eval "base=$base"

dir="$base"
[[ $base = *"/"* ]] && dir="$base"
while true; do
if [ -z "$dir" ] || [ -d "$dir" ]; then
leftover=${base/#"$dir"}
Expand All @@ -150,6 +158,7 @@ __skim_generic_path_completion() {
printf "%q$3 " "$item"
done)
matches=${matches% }
[[ -z "$3" ]] && [[ "$__skim_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "
if [ -n "$matches" ]; then
COMPREPLY=( "$matches" )
else
Expand Down Expand Up @@ -204,7 +213,7 @@ _skim_complete_kill() {
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
local selected skim
skim="$(__skimcmd_complete)"
selected=$(ps -ef | sed 1d | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-50%} --min-height 15 --reverse $SKIM_DEFAULT_OPTIONS --preview 'echo {}' --preview-window down:3:wrap $SKIM_COMPLETION_OPTS" $skim -m | awk '{print $2}' | tr '\n' ' ')
selected=$(command ps -ef | sed 1d | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-50%} --min-height 15 --reverse $SKIM_DEFAULT_OPTIONS --preview 'echo {}' --preview-window down:3:wrap $SKIM_COMPLETION_OPTS" $skim -m | awk '{print $2}' | tr '\n' ' ')
printf '\e[5n'
if [ -n "$selected" ]; then
COMPREPLY=( "$selected" )
Expand All @@ -219,8 +228,8 @@ _skim_complete_telnet() {
}
_skim_complete_ssh() {
_skim_complete '-m' "$@" < <(
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*') \
<(command grep -oE '^[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | awk '{ print $1 " " $1 }') \
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host ' | command grep -v '[*?]' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \
<(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
<(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |
awk '{if (length($2) > 0) {print $2}}' | sort -u
)
Expand Down Expand Up @@ -254,9 +263,9 @@ a_cmds="
svn tar unzip zip"
x_cmds="kill ssh telnet unset unalias export"
# Preserve existing completion
eval $(complete |
eval "$(complete |
sed -E '/-F/!d; / _skim/d; '"/ ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g; s/+/\\+/g'))$/"'!d' |
_skim_orig_completion_filter)
__skim_orig_completion_filter)"
if type _completion_loader > /dev/null 2>&1; then
_skim_completion_loader=1
fi
Expand Down
26 changes: 11 additions & 15 deletions shell/completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ fi

__skimcmd_complete() {
[ -n "$TMUX_PANE" ] && [ "${SKIM_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] &&
echo "skim-tmux -d${SKIM_TMUX_HEIGHT:-40%}" || echo "sk"
echo "sk-tmux -d${SKIM_TMUX_HEIGHT:-40%}" || echo "sk"
}

__skim_generic_path_completion() {
local base lbuf compgen skim_opts suffix tail skim dir leftover matches
# (Q) flag removes a quoting level: "foo\ bar" => "foo bar"
base=${(Q)1}
base=$1
lbuf=$2
compgen=$3
skim_opts=$4
Expand All @@ -44,23 +43,22 @@ __skim_generic_path_completion() {
skim="$(__skimcmd_complete)"

setopt localoptions nonomatch
dir="$base"
eval "base=$base"
[[ $base = *"/"* ]] && dir="$base"
while [ 1 ]; do
if [[ -z "$dir" || -d ${~dir} ]]; then
if [[ -z "$dir" || -d ${dir} ]]; then
leftover=${base/#"$dir"}
leftover=${leftover/#\/}
[ -z "$dir" ] && dir='.'
[ "$dir" != "/" ] && dir="${dir/%\//}"
dir=${~dir}
matches=$(eval "$compgen $(printf %q "$dir")" | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-40%} --reverse $SKIM_DEFAULT_OPTIONS $SKIM_COMPLETION_OPTS" ${=skim} ${=skim_opts} -q "$leftover" | while read item; do
echo -n "${(q)item}$suffix "
done)
matches=${matches% }
if [ -n "$matches" ]; then
LBUFFER="$lbuf$matches$tail"
fi
zle redisplay
typeset -f zle-line-init >/dev/null && zle zle-line-init
zle reset-prompt
break
fi
dir=$(dirname "$dir")
Expand Down Expand Up @@ -93,8 +91,7 @@ _skim_complete() {
if [ -n "$matches" ]; then
LBUFFER="$lbuf$matches"
fi
zle redisplay
typeset -f zle-line-init >/dev/null && zle zle-line-init
zle reset-prompt
command rm -f "$fifo"
}
_skim_complete_telnet() {
Expand All @@ -105,8 +102,8 @@ _skim_complete_telnet() {
}
_skim_complete_ssh() {
_skim_complete '-m' "$@" < <(
command cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host' | command grep -v '*') \
<(command grep -oE '^[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | awk '{ print $1 " " $1 }') \
command cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | command grep -i '^host ' | command grep -v '[*?]' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}') \
<(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
<(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |
awk '{if (length($2) > 0) {print $2}}' | sort -u
)
Expand Down Expand Up @@ -144,12 +141,11 @@ skim-completion() {
# Kill completion (do not require trigger sequence)
if [ $cmd = kill -a ${LBUFFER[-1]} = ' ' ]; then
skim="$(__skimcmd_complete)"
matches=$(ps -ef | sed 1d | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-50%} --min-height 15 --reverse $SKIM_DEFAULT_OPTIONS --preview 'echo {}' --preview-window down:3:wrap $SKIM_COMPLETION_OPTS" ${=skim} -m | awk '{print $2}' | tr '\n' ' ')
matches=$(command ps -ef | sed 1d | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-50%} --min-height 15 --reverse $SKIM_DEFAULT_OPTIONS --preview 'echo {}' --preview-window down:3:wrap $SKIM_COMPLETION_OPTS" ${=skim} -m | awk '{print $2}' | tr '\n' ' ')
if [ -n "$matches" ]; then
LBUFFER="$LBUFFER$matches"
fi
zle redisplay
typeset -f zle-line-init >/dev/null && zle zle-line-init
zle reset-prompt
# Trigger sequence given
elif [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then
d_cmds=(${=SKIM_COMPLETION_DIR_COMMANDS:-cd pushd rmdir})
Expand Down
22 changes: 11 additions & 11 deletions shell/key-bindings.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# copied and modified from https://github.com/junegunn/fzf/blob/master/shell/key-bindings.bash
#
__skim_select__() {
local cmd="${skim_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
local cmd="${SKIM_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2> /dev/null | cut -b3-"}"
eval "$cmd" | SKIM_DEFAULT_OPTIONS="--height ${skim_TMUX_HEIGHT:-40%} --reverse $SKIM_DEFAULT_OPTIONS $skim_CTRL_T_OPTS" skim -m "$@" | while read -r item; do
eval "$cmd" | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-40%} --reverse $SKIM_DEFAULT_OPTIONS $SKIM_CTRL_T_OPTS" skim -m "$@" | while read -r item; do
printf '%q ' "$item"
done
echo
Expand All @@ -16,24 +16,24 @@ __skim_select__() {
if [[ $- =~ i ]]; then

__skim_use_tmux__() {
[ -n "$TMUX_PANE" ] && [ "${skim_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]
[ -n "$TMUX_PANE" ] && [ "${SKIM_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ]
}

__skimcmd() {
__skim_use_tmux__ &&
echo "sk-tmux -d${skim_TMUX_HEIGHT:-40%}" || echo "sk"
echo "sk-tmux -d${SKIM_TMUX_HEIGHT:-40%}" || echo "sk"
}

__skim_select_tmux__() {
local height
height=${skim_TMUX_HEIGHT:-40%}
height=${SKIM_TMUX_HEIGHT:-40%}
if [[ $height =~ %$ ]]; then
height="-p ${height%\%}"
else
height="-l $height"
fi

tmux split-window $height "cd $(printf %q "$PWD"); SKIM_DEFAULT_OPTIONS=$(printf %q "$SKIM_DEFAULT_OPTIONS") PATH=$(printf %q "$PATH") skim_CTRL_T_COMMAND=$(printf %q "$skim_CTRL_T_COMMAND") skim_CTRL_T_OPTS=$(printf %q "$skim_CTRL_T_OPTS") bash -c 'source \"${BASH_SOURCE[0]}\"; RESULT=\"\$(__skim_select__ --no-height)\"; tmux setb -b skim \"\$RESULT\" \\; pasteb -b skim -t $TMUX_PANE \\; deleteb -b skim || tmux send-keys -t $TMUX_PANE \"\$RESULT\"'"
tmux split-window $height "cd $(printf %q "$PWD"); SKIM_DEFAULT_OPTIONS=$(printf %q "$SKIM_DEFAULT_OPTIONS") PATH=$(printf %q "$PATH") SKIM_CTRL_T_COMMAND=$(printf %q "$SKIM_CTRL_T_COMMAND") SKIM_CTRL_T_OPTS=$(printf %q "$SKIM_CTRL_T_OPTS") bash -c 'source \"${BASH_SOURCE[0]}\"; RESULT=\"\$(__skim_select__ --no-height)\"; tmux setb -b skim \"\$RESULT\" \\; pasteb -b skim -t $TMUX_PANE \\; deleteb -b skim || tmux send-keys -t $TMUX_PANE \"\$RESULT\"'"
}

skim-file-widget() {
Expand All @@ -48,17 +48,17 @@ skim-file-widget() {

__skim_cd__() {
local cmd dir
cmd="${skim_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
cmd="${SKIM_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type d -print 2> /dev/null | cut -b3-"}"
dir=$(eval "$cmd" | SKIM_DEFAULT_OPTIONS="--height ${skim_TMUX_HEIGHT:-40%} --reverse $SKIM_DEFAULT_OPTIONS $skim_ALT_C_OPTS" $(__skimcmd) -m) && printf 'cd %q' "$dir"
dir=$(eval "$cmd" | SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-40%} --reverse $SKIM_DEFAULT_OPTIONS $SKIM_ALT_C_OPTS" $(__skimcmd) -m) && printf 'cd %q' "$dir"
}

__skim_history__() (
local line
shopt -u nocaseglob nocasematch
line=$(
HISTTIMEFORMAT= history |
SKIM_DEFAULT_OPTIONS="--height ${skim_TMUX_HEIGHT:-40%} $SKIM_DEFAULT_OPTIONS --reverse -n2..,.. --tac $skim_CTRL_R_OPTS -m" $(__skimcmd) |
SKIM_DEFAULT_OPTIONS="--height ${SKIM_TMUX_HEIGHT:-40%} $SKIM_DEFAULT_OPTIONS --tac --sync -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $SKIM_CTRL_R_OPTS -m" $(__skimcmd) |
command grep '^ *[0-9]') &&
if [[ $- =~ H ]]; then
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
Expand All @@ -82,7 +82,7 @@ if [[ ! -o vi ]]; then
fi

# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": " \C-e\C-u`__skim_history__`\e\C-e\e^\er"'
bind '"\C-r": " \C-e\C-u\C-y\ey\C-u`__skim_history__`\e\C-e\er\e^"'

# ALT-C - cd into the selected directory
bind '"\ec": " \C-e\C-u`__skim_cd__`\e\C-e\er\C-m"'
Expand Down Expand Up @@ -112,7 +112,7 @@ else
bind -m vi-command '"\C-t": "i\C-t"'

# CTRL-R - Paste the selected command from history into the command line
bind '"\C-r": "\C-x\C-addi`__skim_history__`\C-x\C-e\C-x^\C-x\C-a$a\C-x\C-r"'
bind '"\C-r": "\C-x\C-addi`__skim_history__`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"'
bind -m vi-command '"\C-r": "i\C-r"'

# ALT-C - cd into the selected directory
Expand Down
Loading

0 comments on commit 0431f69

Please sign in to comment.