Files
fzf/shell/completion.bash
T

319 lines
7.8 KiB
Bash
Raw Normal View History

2013-11-20 01:29:36 +09:00
#!/bin/bash
# ____ ____
# / __/___ / __/
# / /_/_ / / /_
# / __/ / /_/ __/
# /_/ /___/_/-completion.bash
#
# - $FZF_TMUX (default: 1)
# - $FZF_TMUX_HEIGHT (default: '40%')
2013-11-20 01:42:57 +09:00
# - $FZF_COMPLETION_TRIGGER (default: '**')
# - $FZF_COMPLETION_OPTS (default: empty)
2013-11-20 01:29:36 +09:00
# To use custom commands instead of find, override _fzf_compgen_{path,dir}
if ! declare -f _fzf_compgen_path > /dev/null; then
_fzf_compgen_path() {
echo "$1"
\find -L "$1" \
-name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
-a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
}
fi
if ! declare -f _fzf_compgen_dir > /dev/null; then
_fzf_compgen_dir() {
\find -L "$1" \
-name .git -prune -o -name .svn -prune -o -type d \
-a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'
}
fi
###########################################################
_fzf_orig_completion_filter() {
sed 's/^\(.*-F\) *\([^ ]*\).* \([^ ]*\)$/export _fzf_orig_completion_\3="\1 %s \3 #\2";/' |
awk -F= '{gsub(/[^a-z0-9_= ;]/, "_", $1); print $1"="$2}'
}
2013-11-20 01:29:36 +09:00
_fzf_opts_completion() {
2015-04-16 14:20:29 +09:00
local cur prev opts
2013-11-20 01:29:36 +09:00
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
2015-04-16 14:20:29 +09:00
prev="${COMP_WORDS[COMP_CWORD-1]}"
2014-07-14 12:48:31 +09:00
opts="
-x --extended
2015-11-03 22:49:32 +09:00
-e --exact
2014-07-14 12:48:31 +09:00
-i +i
-n --nth
-d --delimiter
2015-03-06 10:42:38 +09:00
+s --no-sort
--tac
2015-04-16 14:20:29 +09:00
--tiebreak
2015-05-21 00:38:40 +09:00
--bind
2014-07-14 12:48:31 +09:00
-m --multi
--no-mouse
2015-04-23 22:43:48 +09:00
--color
2014-07-14 12:48:31 +09:00
--black
--reverse
2015-04-16 14:20:29 +09:00
--no-hscroll
2015-04-23 22:43:48 +09:00
--inline-info
2014-07-14 12:48:31 +09:00
--prompt
-q --query
-1 --select-1
-0 --exit-0
-f --filter
2015-03-06 10:42:38 +09:00
--print-query
2015-04-16 14:20:29 +09:00
--expect
--toggle-sort
2015-06-14 00:43:44 +09:00
--sync
2015-06-21 17:40:36 +09:00
--cycle
2015-06-14 00:43:44 +09:00
--history
2015-07-23 00:58:20 +09:00
--history-size
2015-09-15 19:04:53 +09:00
--header
2015-07-26 23:02:04 +09:00
--header-lines
--margin"
2013-11-20 01:29:36 +09:00
2015-04-16 14:20:29 +09:00
case "${prev}" in
--tiebreak)
COMPREPLY=( $(compgen -W "length begin end index" -- ${cur}) )
return 0
;;
2015-04-23 22:43:48 +09:00
--color)
COMPREPLY=( $(compgen -W "dark light 16 bw" -- ${cur}) )
return 0
;;
2015-09-15 19:04:53 +09:00
--history)
2015-06-14 00:43:44 +09:00
COMPREPLY=()
return 0
;;
2015-04-16 14:20:29 +09:00
esac
2013-11-20 01:29:36 +09:00
if [[ ${cur} =~ ^-|\+ ]]; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
return 0
}
_fzf_handle_dynamic_completion() {
local cmd orig_var orig ret orig_cmd
cmd="$1"
shift
2015-07-03 23:47:10 +00:00
orig_cmd="$1"
orig_var="_fzf_orig_completion_$cmd"
orig="${!orig_var##*#}"
if [ -n "$orig" ] && type "$orig" > /dev/null 2>&1; then
$orig "$@"
elif [ -n "$_fzf_completion_loader" ]; then
_completion_loader "$@"
ret=$?
2015-07-03 23:47:10 +00:00
eval $(complete | \grep "\-F.* $orig_cmd$" | _fzf_orig_completion_filter)
source $BASH_SOURCE
return $ret
fi
}
2015-10-05 19:34:38 +09:00
__fzf_generic_path_completion() {
2015-04-14 00:59:45 +09:00
local cur base dir leftover matches trigger cmd fzf
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
2013-11-20 01:29:36 +09:00
COMPREPLY=()
trigger=${FZF_COMPLETION_TRIGGER-'**'}
2013-11-20 01:29:36 +09:00
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ ${cur} == *"$trigger" ]]; then
base=${cur:0:${#cur}-${#trigger}}
2013-11-20 01:29:36 +09:00
eval base=$base
dir="$base"
while [ 1 ]; do
if [ -z "$dir" -o -d "$dir" ]; then
leftover=${base/#"$dir"}
leftover=${leftover/#\/}
2015-12-28 21:11:04 +01:00
[ -z "$dir" ] && dir='.'
[ "$dir" != "/" ] && dir="${dir/%\//}"
2013-11-27 01:00:23 +09:00
tput sc
matches=$(eval "$1 $(printf %q "$dir")" | $fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
2015-03-04 12:59:23 +09:00
printf "%q$3 " "$item"
done)
matches=${matches% }
if [ -n "$matches" ]; then
COMPREPLY=( "$matches" )
2013-11-20 01:29:36 +09:00
else
COMPREPLY=( "$cur" )
2013-11-20 01:29:36 +09:00
fi
2013-11-27 01:00:23 +09:00
tput rc
return 0
2013-11-20 01:29:36 +09:00
fi
dir=$(dirname "$dir")
2013-11-23 20:37:53 +09:00
[[ "$dir" =~ /$ ]] || dir="$dir"/
done
else
2015-03-04 12:59:23 +09:00
shift
shift
shift
_fzf_handle_dynamic_completion "$cmd" "$@"
2013-11-20 01:29:36 +09:00
fi
}
2015-10-05 19:34:38 +09:00
_fzf_feed_fifo() (
rm -f "$fifo"
mkfifo "$fifo"
cat <&0 > "$fifo" &
)
_fzf_complete() {
local fifo cur selected trigger cmd fzf
fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
2015-10-05 19:34:38 +09:00
2014-07-14 12:48:31 +09:00
cmd=$(echo ${COMP_WORDS[0]} | sed 's/[^a-z0-9_=]/_/g')
trigger=${FZF_COMPLETION_TRIGGER-'**'}
2014-07-14 12:48:31 +09:00
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ ${cur} == *"$trigger" ]]; then
cur=${cur:0:${#cur}-${#trigger}}
2015-10-05 19:34:38 +09:00
_fzf_feed_fifo "$fifo"
2014-07-14 12:48:31 +09:00
tput sc
2015-10-05 19:34:38 +09:00
selected=$(eval "cat '$fifo' | $fzf $FZF_COMPLETION_OPTS $1 -q '$cur'" | tr '\n' ' ')
selected=${selected% } # Strip trailing space not to repeat "-o nospace"
2014-07-14 12:48:31 +09:00
tput rc
2015-10-05 19:34:38 +09:00
rm -f "$fifo"
2014-07-14 12:48:31 +09:00
if [ -n "$selected" ]; then
COMPREPLY=("$selected")
return 0
fi
else
shift
_fzf_handle_dynamic_completion "$cmd" "$@"
2014-07-14 12:48:31 +09:00
fi
}
2015-10-05 19:34:38 +09:00
_fzf_path_completion() {
__fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"
2013-11-20 12:28:41 +09:00
}
# Deprecated. No file only completion.
2013-11-20 12:28:41 +09:00
_fzf_file_completion() {
_fzf_path_completion "$@"
2013-11-20 01:29:36 +09:00
}
_fzf_dir_completion() {
__fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"
2013-11-20 01:29:36 +09:00
}
2015-10-05 19:34:38 +09:00
_fzf_complete_kill() {
2013-11-29 23:42:00 +09:00
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
2015-04-14 00:59:45 +09:00
local selected fzf
[ ${FZF_TMUX:-1} -eq 1 ] && fzf="fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%}" || fzf="fzf"
2013-11-29 17:49:48 +09:00
tput sc
2015-04-14 00:59:45 +09:00
selected=$(ps -ef | sed 1d | $fzf -m $FZF_COMPLETION_OPTS | awk '{print $2}' | tr '\n' ' ')
2013-11-29 17:49:48 +09:00
tput rc
if [ -n "$selected" ]; then
COMPREPLY=( "$selected" )
return 0
fi
}
2015-10-05 19:34:38 +09:00
_fzf_complete_telnet() {
_fzf_complete '+m' "$@" < <(
\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0' |
awk '{if (length($2) > 0) {print $2}}' | sort -u
)
}
2015-10-05 19:34:38 +09:00
_fzf_complete_ssh() {
_fzf_complete '+m' "$@" < <(
cat <(cat ~/.ssh/config /etc/ssh/ssh_config 2> /dev/null | \grep -i '^host' | \grep -v '*') \
<(\grep -v '^\s*\(#\|$\)' /etc/hosts | \grep -Fv '0.0.0.0') |
awk '{if (length($2) > 0) {print $2}}' | sort -u
)
2014-07-14 12:48:31 +09:00
}
2015-10-05 19:34:38 +09:00
_fzf_complete_unset() {
_fzf_complete '-m' "$@" < <(
declare -xp | sed 's/=.*//' | sed 's/.* //'
2015-10-05 19:34:38 +09:00
)
2014-07-14 12:48:31 +09:00
}
2015-10-05 19:34:38 +09:00
_fzf_complete_export() {
_fzf_complete '-m' "$@" < <(
declare -xp | sed 's/=.*//' | sed 's/.* //'
)
}
_fzf_complete_unalias() {
_fzf_complete '-m' "$@" < <(
alias | sed 's/=.*//' | sed 's/.* //'
2015-10-05 19:34:38 +09:00
)
}
# fzf options
2015-06-14 00:43:44 +09:00
complete -o default -F _fzf_opts_completion fzf
2013-11-20 01:29:36 +09:00
d_cmds="${FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir}"
a_cmds="
awk cat diff diff3
emacs emacsclient ex file ftp g++ gcc gvim head hg java
2015-11-06 11:22:35 -02:00
javac ld less more mvim nvim patch perl python ruby
sed sftp sort source tail tee uniq vi view vim wc xdg-open
basename bunzip2 bzip2 chmod chown curl cp dirname du
find git grep gunzip gzip hg jar
ln ls mv open rm rsync scp
svn tar unzip zip"
2014-07-14 12:48:31 +09:00
x_cmds="kill ssh telnet unset unalias export"
# Preserve existing completion
if [ "$_fzf_completion_loaded" != '0.10.8' ]; then
# Really wish I could use associative array but OSX comes with bash 3.2 :(
2014-09-14 00:53:53 -04:00
eval $(complete | \grep '\-F' | \grep -v _fzf_ |
\grep -E " ($(echo $d_cmds $a_cmds $x_cmds | sed 's/ /|/g' | sed 's/+/\\+/g'))$" | _fzf_orig_completion_filter)
export _fzf_completion_loaded=0.10.8
fi
2014-07-18 00:25:01 +09:00
if type _completion_loader > /dev/null 2>&1; then
_fzf_completion_loader=1
fi
_fzf_defc() {
local cmd func opts orig_var orig
cmd="$1"
func="$2"
opts="$3"
orig_var="_fzf_orig_completion_$cmd"
orig="${!orig_var}"
if [ -n "$orig" ]; then
eval "$(printf "$orig" "$func")"
else
complete -F "$func" $opts "$cmd"
fi
}
2013-11-20 12:28:41 +09:00
# Anything
for cmd in $a_cmds; do
_fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"
2013-11-20 12:28:41 +09:00
done
2013-11-20 01:29:36 +09:00
# Directory
for cmd in $d_cmds; do
_fzf_defc "$cmd" _fzf_dir_completion "-o nospace -o plusdirs"
done
unset _fzf_defc
2013-11-29 17:49:48 +09:00
# Kill completion
2015-10-05 19:34:38 +09:00
complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill
2013-11-29 17:49:48 +09:00
# Host completion
2015-10-05 19:34:38 +09:00
complete -F _fzf_complete_ssh -o default -o bashdefault ssh
complete -F _fzf_complete_telnet -o default -o bashdefault telnet
2014-07-14 12:48:31 +09:00
# Environment variables / Aliases
2015-10-05 19:34:38 +09:00
complete -F _fzf_complete_unset -o default -o bashdefault unset
complete -F _fzf_complete_export -o default -o bashdefault export
complete -F _fzf_complete_unalias -o default -o bashdefault unalias
2014-07-14 12:48:31 +09:00
unset cmd d_cmds a_cmds x_cmds