[shell] Make bash/zsh completion and bindings work with 'set -u' (#2999)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Fred Fadrigalan · committed by GitHub · parent f9d5330 · Browse files · 5 files·+30 added−27 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
Diff stats
5 files+30 added−27 removed
- tests:+3 added−2 removed
- other:+27 added−25 removed
Provenance
- Commit
4603d54- Tree
2b6211d- Author
John Fred Fadrigalan <johnfred.fadrigalan@hotmail.com>- Committer
GitHub <noreply@github.com>- Parent
f9d5330
5 files changed+30 added−27 removed
modified:shell/completion.bash+12 added-10 removed
35 unmodified lines36 37 __fzf_comprun() {38 if [[ "$(type -t _fzf_comprun 2>&1)" = function ]]; then39 _fzf_comprun "$@"40- elif [[ -n "$TMUX_PANE" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "$FZF_TMUX_OPTS" ]]; }; then40+ elif [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; }; then41 shift42 fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"43 else44 shift9 unmodified lines54 f="${BASH_REMATCH[2]}"55 cmd="${BASH_REMATCH[3]}"56 [[ "$f" = _fzf_* ]] && continue57 printf -v "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}" "%s" "${comp} %s ${cmd} #${f}"58- if [[ "$l" = *" -o nospace "* ]] && [[ ! "$__fzf_nospace_commands" = *" $cmd "* ]]; then59- __fzf_nospace_commands="$__fzf_nospace_commands $cmd "58+ if [[ "$l" = *" -o nospace "* ]] && [[ ! "${__fzf_nospace_commands-}" = *" $cmd "* ]]; then59+ __fzf_nospace_commands="${__fzf_nospace_commands-} $cmd "60 fi61 fi62 done63 }74 unmodified lines138 cmd="$1"139 shift140 orig_cmd="$1"141 orig_var="_fzf_orig_completion_$cmd"142- orig="${!orig_var##*#}"142+ orig="${!orig_var-}"143+ orig="${orig##*#}"144 if [[ -n "$orig" ]] && type "$orig" > /dev/null 2>&1; then145 $orig "$@"145- elif [[ -n "$_fzf_completion_loader" ]]; then146+ elif [[ -n "${_fzf_completion_loader-}" ]]; then147 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)148 _completion_loader "$@"149 ret=$?150 # _completion_loader may not have updated completion for the command151 if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then152 __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)152- if [[ "$__fzf_nospace_commands" = *" $orig_cmd "* ]]; then153+ if [[ "${__fzf_nospace_commands-}" = *" $orig_cmd "* ]]; then154 eval "${orig_complete/ -F / -o nospace -F }"155 else156 eval "$orig_complete"157 fi15 unmodified lines173 if [[ "$cur" == *"$trigger" ]]; then174 base=${cur:0:${#cur}-${#trigger}}175 eval "base=$base"176 177+ dir=178 [[ $base = *"/"* ]] && dir="$base"179 while true; do180 if [[ -z "$dir" ]] || [[ -d "$dir" ]]; then181 leftover=${base/#"$dir"}182 leftover=${leftover/#\/}183 [[ -z "$dir" ]] && dir='.'184 [[ "$dir" != "/" ]] && dir="${dir/%\//}"183- matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $2" __fzf_comprun "$4" -q "$leftover" | while read -r item; do185+ matches=$(eval "$1 $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_COMPLETION_OPTS-} $2" __fzf_comprun "$4" -q "$leftover" | while read -r item; do186 printf "%q " "${item%$3}$3"187 done)188 matches=${matches% }187- [[ -z "$3" ]] && [[ "$__fzf_nospace_commands" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "189+ [[ -z "$3" ]] && [[ "${__fzf_nospace_commands-}" = *" ${COMP_WORDS[0]} "* ]] && matches="$matches "190 if [[ -n "$matches" ]]; then191 COMPREPLY=( "$matches" )192 else193 COMPREPLY=( "$cur" )43 unmodified lines237 cur="${COMP_WORDS[COMP_CWORD]}"238 if [[ "$cur" == *"$trigger" ]]; then239 cur=${cur:0:${#cur}-${#trigger}}240 239- selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $str_arg" __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | tr '\n' ' ')241+ selected=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_COMPLETION_OPTS-} $str_arg" __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | $post | tr '\n' ' ')242 selected=${selected% } # Strip trailing space not to repeat "-o nospace"243 if [[ -n "$selected" ]]; then244 COMPREPLY=("$selected")245 else84 unmodified lines330 cmd="$1"331 func="$2"332 opts="$3"333 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"332- orig="${!orig_var}"334+ orig="${!orig_var-}"335 if [[ -n "$orig" ]]; then336 printf -v def "$orig" "$func"337 eval "$def"338 else43 unmodified linesmodified:shell/completion.zsh+7 added-7 removed
97 unmodified lines98 99 __fzf_comprun() {100 if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then101 _fzf_comprun "$@"102- elif [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; }; then102+ elif [ -n "${TMUX_PANE-}" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "${FZF_TMUX_OPTS-}" ]; }; then103 shift104- if [ -n "$FZF_TMUX_OPTS" ]; then104+ if [ -n "${FZF_TMUX_OPTS-}" ]; then105 fzf-tmux ${(Q)${(Z+n+)FZF_TMUX_OPTS}} -- "$@"106 else107 fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%} -- "$@"108 fi35 unmodified lines144 leftover=${base/#"$dir"}145 leftover=${leftover/#\/}146 [ -z "$dir" ] && dir='.'147 [ "$dir" != "/" ] && dir="${dir/%\//}"148- matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" | while read item; do148+ matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_COMPLETION_OPTS-}" __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" | while read item; do149 item="${item%$suffix}$suffix"150 echo -n "${(q)item} "151 done)152 matches=${matches% }30 unmodified lines183 local args rest str_arg i sep184 args=("$@")185 sep=186 for i in {0..${#args[@]}}; do187- if [[ "${args[$i]}" = -- ]]; then187+ if [[ "${args[$i]-}" = -- ]]; then188 sep=$i189 break190 fi191 done15 unmodified lines207 post="${funcstack[1]}_post"208 type $post > /dev/null 2>&1 || post=cat209 210 _fzf_feed_fifo "$fifo"211- matches=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $str_arg" __fzf_comprun "$cmd" "${args[@]}" -q "${(Q)prefix}" < "$fifo" | $post | tr '\n' ' ')211+ matches=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_COMPLETION_OPTS-} $str_arg" __fzf_comprun "$cmd" "${args[@]}" -q "${(Q)prefix}" < "$fifo" | $post | tr '\n' ' ')212 if [ -n "$matches" ]; then213 LBUFFER="$lbuf$matches"214 fi215 command rm -f "$fifo"62 unmodified lines278 trigger=${FZF_COMPLETION_TRIGGER-'**'}279 [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("")280 281 # When the trigger starts with ';', it becomes a separate token282- if [[ ${LBUFFER} = *"${tokens[-2]}${tokens[-1]}" ]]; then283- tokens[-2]="${tokens[-2]}${tokens[-1]}"282+ if [[ ${LBUFFER} = *"${tokens[-2]-}${tokens[-1]}" ]]; then283+ tokens[-2]="${tokens[-2]-}${tokens[-1]}"284 tokens=(${tokens[0,-2]})285 fi286 287 lbuf=$LBUFFER37 unmodified linesmodified:shell/key-bindings.bash+4 added-4 removed
17 unmodified lines181920212222232425262 unmodified lines293031323333343536375 unmodified lines434445464747484950515253535455565745 unmodified lines17 unmodified lines cmd="${FZF_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-"}" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS -m" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse ${FZF_DEFAULT_OPTS-} ${FZF_CTRL_T_OPTS-} -m" eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd) "$@" | while read -r item; do printf '%q ' "$item" # escape special chars2 unmodified lines
if [[ $- =~ i ]]; then
__fzfcmd() { [[ -n "$TMUX_PANE" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "$FZF_TMUX_OPTS" ]]; } && [[ -n "${TMUX_PANE-}" ]] && { [[ "${FZF_TMUX:-0}" != 0 ]] || [[ -n "${FZF_TMUX_OPTS-}" ]]; } && echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"}
fzf-file-widget() {5 unmodified lines__fzf_cd__() { local cmd opts dir cmd="${FZF_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-"}" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS +m" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore --reverse ${FZF_DEFAULT_OPTS-} ${FZF_ALT_C_OPTS-} +m" dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="$opts" $(__fzfcmd)) && printf 'builtin cd -- %q' "$dir"}
__fzf_history__() { local output opts script opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m --read0" opts="--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort ${FZF_CTRL_R_OPTS-} +m --read0" script='BEGIN { getc; $/ = "\n\t"; $HISTCOUNT = $ENV{last_hist} + 1 } s/^[ *]//; print $HISTCOUNT - $. . "\t$_" if !$seen{$_}++' output=$( builtin fc -lnr -2147483648 | last_hist=$(HISTTIMEFORMAT='' builtin history 1) perl -n -l0 -e "$script" |45 unmodified linesmodified:shell/key-bindings.zsh+4 added-4 removed
44 unmodified lines45 -o -type d -print \46 -o -type l -print 2> /dev/null | cut -b3-"}"47 setopt localoptions pipefail no_aliases 2> /dev/null48 local item49- eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do49+ eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_CTRL_T_OPTS-}" $(__fzfcmd) -m "$@" | while read item; do50 echo -n "${(q)item} "51 done52 local ret=$?53 echo54 return $ret55 }56 57 __fzfcmd() {58- [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; } &&58+ [ -n "${TMUX_PANE-}" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "${FZF_TMUX_OPTS-}" ]; } &&59 echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf"60 }61 62 fzf-file-widget() {11 unmodified lines74 fzf-cd-widget() {75 local cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \76 -o -type d -print 2> /dev/null | cut -b3-"}"77 setopt localoptions pipefail no_aliases 2> /dev/null78- local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m)"78+ local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore ${FZF_DEFAULT_OPTS-} ${FZF_ALT_C_OPTS-}" $(__fzfcmd) +m)"79 if [[ -z "$dir" ]]; then80 zle redisplay81 return 082 fi14 unmodified lines97 fzf-history-widget() {98 local selected num99 setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null100 selected=( $(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' |101- FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )101+ FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} ${FZF_DEFAULT_OPTS-} -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )102 local ret=$?103 if [ -n "$selected" ]; then104 num=$selected[1]105 if [ -n "$num" ]; then15 unmodified linesmodified:test/test_go.rb+3 added-2 removed
2756 unmodified lines2757 end2758 end2759 2760 __END__2761+set -u2762 PS1= PROMPT_COMMAND= HISTFILE= HISTSIZE=1002763 unset <%= UNSETS.join(' ') %>2764 unset $(env | sed -n /^_fzf_orig/s/=.*//p)2765 unset $(declare -F | sed -n "/_fzf/s/.*-f //p")35 unmodified lines2801 _fzf_complete_g_post() {2802 awk '{print "g" $0 $0}'2803 }2804 2804-[ -n "$BASH" ] && complete -F _fzf_complete_f -o default -o bashdefault f2805-[ -n "$BASH" ] && complete -F _fzf_complete_g -o default -o bashdefault g2805+[ -n "${BASH-}" ] && complete -F _fzf_complete_f -o default -o bashdefault f2806+[ -n "${BASH-}" ] && complete -F _fzf_complete_g -o default -o bashdefault g2807 2808 _comprun() {2809 local command=$12810 shift7 unmodified lines