Debian Codegraph @

shell/completion.bash

raw
1 # ____ ____
2 # / __/___ / __/
3 # / /_/_ / / /_
4 # / __/ / /_/ __/
5 # /_/ /___/_/ completion.bash
6 #
7 # - $FZF_COMPLETION_TRIGGER (default: '**')
8 # - $FZF_COMPLETION_OPTS (default: empty)
9 # - $FZF_COMPLETION_PATH_OPTS (default: empty)
10 # - $FZF_COMPLETION_DIR_OPTS (default: empty)
11
12 if [[ $- =~ i ]]; then
13
14
15 # To use custom commands instead of find, override _fzf_compgen_{path,dir}
16 #
17 # _fzf_compgen_path() {
18 # echo "$1"
19 # command find -L "$1" \
20 # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
21 # -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@'
22 # }
23 #
24 # _fzf_compgen_dir() {
25 # command find -L "$1" \
26 # -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \
27 # -a -not -path "$1" -print 2> /dev/null | command sed 's@^\./@@'
28 # }
29
30 ###########################################################
31
32 #----BEGIN shfmt
33 #----BEGIN INCLUDE common.sh
34 # NOTE: Do not directly edit this section, which is copied from "common.sh".
35 # To modify it, one can edit "common.sh" and run "./update.sh" to apply
36 # the changes. See code comments in "common.sh" for the implementation details.
37
38 __fzf_defaults() {
39 builtin printf '%s\n' "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
40 command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
41 builtin printf '%s\n' "${FZF_DEFAULT_OPTS-} $2"
42 }
43
44 __fzf_exec_awk() {
45 if [[ -z ${__fzf_awk-} ]]; then
46 __fzf_awk=awk
47 if [[ $OSTYPE == solaris* && -x /usr/xpg4/bin/awk ]]; then
48 __fzf_awk=/usr/xpg4/bin/awk
49 elif command -v mawk > /dev/null 2>&1; then
50 local n x y z d
51 IFS=' .' read -r n x y z d <<< $(command mawk -W version 2> /dev/null)
52 [[ $n == mawk ]] &&
53 (((x * 1000 + y) * 1000 + z >= 1003004)) 2> /dev/null &&
54 ((d >= 20230302)) 2> /dev/null &&
55 __fzf_awk=mawk
56 fi
57 fi
58 LC_ALL=C exec "$__fzf_awk" "$@"
59 }
60 #----END INCLUDE
61
62 __fzf_comprun() {
63 if [[ "$(type -t _fzf_comprun 2>&1)" == function ]]; then
64 _fzf_comprun "$@"
65 elif [[ -n ${TMUX_PANE-} ]] && { [[ ${FZF_TMUX:-0} != 0 ]] || [[ -n ${FZF_TMUX_OPTS-} ]]; }; then
66 shift
67 fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- "$@"
68 else
69 shift
70 fzf "$@"
71 fi
72 }
73
74 __fzf_orig_completion() {
75 local l comp f cmd
76 while read -r l; do
77 if [[ $l =~ ^(.*\ -F)\ *([^ ]*).*\ ([^ ]*)$ ]]; then
78 comp="${BASH_REMATCH[1]}"
79 f="${BASH_REMATCH[2]}"
80 cmd="${BASH_REMATCH[3]}"
81 [[ $f == _fzf_* ]] && continue
82 builtin printf -v "_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}" "%s" "${comp} %s ${cmd} #${f}"
83 if [[ $l == *" -o nospace "* ]] && [[ ${__fzf_nospace_commands-} != *" $cmd "* ]]; then
84 __fzf_nospace_commands="${__fzf_nospace_commands-} $cmd "
85 fi
86 fi
87 done
88 }
89
90 # @param $1 cmd - Command name for which the original completion is searched
91 # @var[out] REPLY - Original function name is returned
92 __fzf_orig_completion_get_orig_func() {
93 local cmd orig_var orig
94 cmd=$1
95 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
96 orig="${!orig_var-}"
97 REPLY="${orig##*#}"
98 [[ $REPLY ]] && type "$REPLY" &> /dev/null
99 }
100
101 # @param $1 cmd - Command name for which the original completion is searched
102 # @param $2 func - Fzf's completion function to replace the original function
103 # @var[out] REPLY - Completion setting is returned as a string to "eval"
104 __fzf_orig_completion_instantiate() {
105 local cmd func orig_var orig
106 cmd=$1
107 func=$2
108 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"
109 orig="${!orig_var-}"
110 orig="${orig%#*}"
111 [[ $orig == *' %s '* ]] || return 1
112 builtin printf -v REPLY "$orig" "$func"
113 }
114
115 _fzf_opts_completion() {
116 local cur prev opts
117 COMPREPLY=()
118 cur="${COMP_WORDS[COMP_CWORD]}"
119 prev="${COMP_WORDS[COMP_CWORD - 1]}"
120 opts="
121 +c --no-color
122 +i --no-ignore-case
123 +s --no-sort
124 +x --no-extended
125 --accept-nth
126 --ansi
127 --bash
128 --bind
129 --border
130 --border-label
131 --border-label-pos
132 --color
133 --cycle
134 --disabled
135 --ellipsis
136 --expect
137 --filepath-word
138 --fish
139 --footer
140 --footer-border
141 --footer-label
142 --footer-label-pos
143 --freeze-left
144 --freeze-right
145 --gap
146 --gap-line
147 --ghost
148 --gutter
149 --gutter-raw
150 --header
151 --header-border
152 --header-first
153 --header-label
154 --header-label-pos
155 --header-lines
156 --header-lines-border
157 --height
158 --highlight-line
159 --history
160 --history-size
161 --hscroll-off
162 --id-nth
163 --info
164 --info-command
165 --input-border
166 --input-label
167 --input-label-pos
168 --jump-labels
169 --keep-right
170 --layout
171 --listen
172 --listen-unsafe
173 --list-border
174 --list-label
175 --list-label-pos
176 --literal
177 --man
178 --margin
179 --marker
180 --marker-multi-line
181 --min-height
182 --no-bold
183 --no-hscroll
184 --no-input
185 --no-multi-line
186 --no-scrollbar
187 --no-separator
188 --padding
189 --pointer
190 --preview
191 --preview-border
192 --preview-label
193 --preview-label-pos
194 --preview-window
195 --print-query
196 --print0
197 --prompt
198 --raw
199 --read0
200 --scheme
201 --scroll-off
202 --scrollbar
203 --separator
204 --smart-case
205 --style
206 --sync
207 --tabstop
208 --tac
209 --tail
210 --tiebreak
211 --tmux
212 --track
213 --version
214 --walker
215 --walker-root
216 --walker-skip
217 --with-nth
218 --with-shell
219 --wrap
220 --wrap-sign
221 --preview-wrap-sign
222 --zsh
223 -0 --exit-0
224 -1 --select-1
225 -d --delimiter
226 -e --exact
227 -f --filter
228 -h --help
229 -i --ignore-case
230 -m --multi
231 -n --nth
232 -q --query
233 --"
234
235 case "${prev}" in
236 --scheme)
237 COMPREPLY=($(compgen -W "default path history" -- "$cur"))
238 return 0
239 ;;
240 --tiebreak)
241 COMPREPLY=($(compgen -W "length chunk pathname begin end index" -- "$cur"))
242 return 0
243 ;;
244 --color)
245 COMPREPLY=($(compgen -W "dark light base16 16 bw no" -- "$cur"))
246 return 0
247 ;;
248 --layout)
249 COMPREPLY=($(compgen -W "default reverse reverse-list" -- "$cur"))
250 return 0
251 ;;
252 --info)
253 COMPREPLY=($(compgen -W "default right hidden inline inline-right" -- "$cur"))
254 return 0
255 ;;
256 --wrap)
257 COMPREPLY=($(compgen -W "char word" -- "$cur"))
258 return 0
259 ;;
260 --style)
261 COMPREPLY=($(compgen -W "default minimal full" -- "$cur"))
262 return 0
263 ;;
264 --preview-window)
265 COMPREPLY=($(compgen -W "
266 default
267 hidden
268 nohidden
269 wrap
270 wrap-word
271 nowrap
272 cycle
273 nocycle
274 up top
275 down bottom
276 left
277 right
278 rounded border border-rounded
279 border-line
280 sharp border-sharp
281 border-bold
282 border-block
283 border-thinblock
284 border-double
285 noborder border-none
286 border-horizontal
287 border-vertical
288 border-up border-top
289 border-down border-bottom
290 border-left
291 border-right
292 follow
293 nofollow
294 info
295 noinfo" -- "$cur"))
296 return 0
297 ;;
298 --border | --list-border | --header-border | --header-lines-border | --footer-border | --input-border | --preview-border)
299 COMPREPLY=($(compgen -W "line rounded sharp bold block thinblock double horizontal vertical top bottom left right none" -- "$cur"))
300 return 0
301 ;;
302 --border-label-pos | --preview-label-pos | --list-label-pos | --header-label-pos | --footer-label-pos | --input-label-pos)
303 COMPREPLY=($(compgen -W "center bottom top" -- "$cur"))
304 return 0
305 ;;
306 esac
307
308 if [[ $cur =~ ^-|\+ ]]; then
309 COMPREPLY=($(compgen -W "${opts}" -- "$cur"))
310 return 0
311 fi
312
313 return 0
314 }
315
316 _fzf_handle_dynamic_completion() {
317 local cmd ret REPLY orig_cmd orig_complete
318 cmd="$1"
319 shift
320 orig_cmd="$1"
321 if __fzf_orig_completion_get_orig_func "$cmd"; then
322 "$REPLY" "$@"
323 elif [[ -n ${_fzf_completion_loader-} ]]; then
324 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)
325 $_fzf_completion_loader "$@"
326 ret=$?
327 # _completion_loader may not have updated completion for the command
328 if [[ "$(complete -p "$orig_cmd" 2> /dev/null)" != "$orig_complete" ]]; then
329 __fzf_orig_completion < <(complete -p "$orig_cmd" 2> /dev/null)
330 __fzf_orig_completion_get_orig_func "$cmd" || ret=1
331
332 # Update orig_complete by _fzf_orig_completion entry
333 [[ $orig_complete =~ ' -F '(_fzf_[^ ]+)' ' ]] &&
334 __fzf_orig_completion_instantiate "$cmd" "${BASH_REMATCH[1]}" &&
335 orig_complete=$REPLY
336
337 if [[ ${__fzf_nospace_commands-} == *" $orig_cmd "* ]]; then
338 eval "${orig_complete/ -F / -o nospace -F }"
339 else
340 eval "$orig_complete"
341 fi
342 fi
343 [[ $ret -eq 0 ]] && return 124
344 return $ret
345 fi
346 }
347
348 __fzf_generic_path_completion() {
349 local cur base dir leftover matches trigger cmd
350 cmd="${COMP_WORDS[0]}"
351 if [[ $cmd == \\* ]]; then
352 cmd="${cmd:1}"
353 fi
354 COMPREPLY=()
355 trigger=${FZF_COMPLETION_TRIGGER-'**'}
356 [[ $COMP_CWORD -ge 0 ]] && cur="${COMP_WORDS[COMP_CWORD]}"
357 if [[ $cur == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
358 base=${cur:0:${#cur}-${#trigger}}
359 eval "base=$base" 2> /dev/null || return
360
361 dir=
362 [[ $base == *"/"* ]] && dir="$base"
363 while true; do
364 if [[ -z $dir ]] || [[ -d $dir ]]; then
365 leftover=${base/#"$dir"/}
366 leftover=${leftover/#\//}
367 [[ -z $dir ]] && dir='.'
368 [[ $dir != "/" ]] && dir="${dir/%\//}"
369 matches=$(
370 export FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse --scheme=path" "${FZF_COMPLETION_OPTS-} $2")
371 unset FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS_FILE
372 if [[ $1 =~ dir ]]; then
373 eval "rest=(${FZF_COMPLETION_DIR_OPTS-})"
374 else
375 eval "rest=(${FZF_COMPLETION_PATH_OPTS-})"
376 fi
377 if declare -F "$1" > /dev/null; then
378 eval "$1 $(builtin printf %q "$dir")" | __fzf_comprun "$4" -q "$leftover" "${rest[@]}"
379 else
380 if [[ $1 =~ dir ]]; then
381 walker=dir,follow
382 else
383 walker=file,dir,follow,hidden
384 fi
385 __fzf_comprun "$4" -q "$leftover" --walker "$walker" --walker-root="$dir" "${rest[@]}"
386 fi | while read -r item; do
387 builtin printf "%q " "${item%$3}$3"
388 done
389 )
390 matches=${matches% }
391 [[ -z $3 ]] && [[ ${__fzf_nospace_commands-} == *" ${COMP_WORDS[0]} "* ]] && matches="$matches "
392 if [[ -n $matches ]]; then
393 COMPREPLY=("$matches")
394 else
395 COMPREPLY=("$cur")
396 fi
397 # To redraw line after fzf closes (builtin printf '\e[5n')
398 bind '"\e[0n": redraw-current-line' 2> /dev/null
399 builtin printf '\e[5n'
400 return 0
401 fi
402 dir=$(command dirname "$dir")
403 [[ $dir =~ /$ ]] || dir="$dir"/
404 done
405 else
406 shift
407 shift
408 shift
409 _fzf_handle_dynamic_completion "$cmd" "$@"
410 fi
411 }
412
413 _fzf_complete() {
414 # Split arguments around --
415 local args rest str_arg i sep
416 args=("$@")
417 sep=
418 for i in "${!args[@]}"; do
419 if [[ ${args[$i]} == -- ]]; then
420 sep=$i
421 break
422 fi
423 done
424 if [[ -n $sep ]]; then
425 str_arg=
426 rest=("${args[@]:$((sep + 1)):${#args[@]}}")
427 args=("${args[@]:0:sep}")
428 else
429 str_arg=$1
430 args=()
431 shift
432 rest=("$@")
433 fi
434
435 local cur selected trigger cmd post
436 post="$(caller 0 | __fzf_exec_awk '{print $2}')_post"
437 type -t "$post" > /dev/null 2>&1 || post='command cat'
438
439 trigger=${FZF_COMPLETION_TRIGGER-'**'}
440 cmd="${COMP_WORDS[0]}"
441 cur="${COMP_WORDS[COMP_CWORD]}"
442 if [[ $cur == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
443 cur=${cur:0:${#cur}-${#trigger}}
444
445 selected=$(
446 FZF_DEFAULT_OPTS=$(__fzf_defaults "--reverse" "${FZF_COMPLETION_OPTS-} $str_arg") \
447 FZF_DEFAULT_OPTS_FILE='' \
448 __fzf_comprun "${rest[0]}" "${args[@]}" -q "$cur" | eval "$post" | command tr '\n' ' '
449 )
450 selected=${selected% } # Strip trailing space not to repeat "-o nospace"
451 if [[ -n $selected ]]; then
452 COMPREPLY=("$selected")
453 else
454 COMPREPLY=("$cur")
455 fi
456 bind '"\e[0n": redraw-current-line' 2> /dev/null
457 builtin printf '\e[5n'
458 return 0
459 else
460 _fzf_handle_dynamic_completion "$cmd" "${rest[@]}"
461 fi
462 }
463
464 _fzf_path_completion() {
465 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"
466 }
467
468 # Deprecated. No file only completion.
469 _fzf_file_completion() {
470 _fzf_path_completion "$@"
471 }
472
473 _fzf_dir_completion() {
474 __fzf_generic_path_completion _fzf_compgen_dir "" "/" "$@"
475 }
476
477 _fzf_complete_kill() {
478 _fzf_proc_completion "$@"
479 }
480
481 _fzf_proc_completion() {
482 local transformer
483 transformer='
484 if [[ $FZF_KEY =~ ctrl|alt|shift ]] && [[ -n $FZF_NTH ]]; then
485 nths=( ${FZF_NTH//,/ } )
486 new_nths=()
487 found=0
488 for nth in ${nths[@]}; do
489 if [[ $nth = $FZF_CLICK_HEADER_NTH ]]; then
490 found=1
491 else
492 new_nths+=($nth)
493 fi
494 done
495 [[ $found = 0 ]] && new_nths+=($FZF_CLICK_HEADER_NTH)
496 new_nths=${new_nths[*]}
497 new_nths=${new_nths// /,}
498 echo "change-nth($new_nths)+change-prompt($new_nths> )"
499 else
500 if [[ $FZF_NTH = $FZF_CLICK_HEADER_NTH ]]; then
501 echo "change-nth()+change-prompt(> )"
502 else
503 echo "change-nth($FZF_CLICK_HEADER_NTH)+change-prompt($FZF_CLICK_HEADER_WORD> )"
504 fi
505 fi
506 '
507 _fzf_complete -m --header-lines=1 --no-preview --wrap --color fg:dim,nth:regular \
508 --bind "click-header:transform:$transformer" -- "$@" < <(
509 command ps -eo user,pid,ppid,start,time,command 2> /dev/null ||
510 command ps -eo user,pid,ppid,time,args 2> /dev/null || # For BusyBox
511 command ps --everyone --full --windows # For cygwin
512 )
513 }
514
515 _fzf_proc_completion_post() {
516 __fzf_exec_awk '{print $2}'
517 }
518
519 # To use custom hostname lists, override __fzf_list_hosts.
520 # The function is expected to print hostnames, one per line as well as in the
521 # desired sorting and with any duplicates removed, to standard output.
522 #
523 # e.g.
524 # # Use bash-completions’s _known_hosts_real() for getting the list of hosts
525 # __fzf_list_hosts() {
526 # # Set the local attribute for any non-local variable that is set by _known_hosts_real()
527 # local COMPREPLY=()
528 # _known_hosts_real ''
529 # builtin printf '%s\n' "${COMPREPLY[@]}" | command sort -u --version-sort
530 # }
531 if ! declare -F __fzf_list_hosts > /dev/null; then
532 __fzf_list_hosts() {
533 command sort -u \
534 <(
535 # Note: To make the pathname expansion of "~/.ssh/config.d/*" work
536 # properly, we need to adjust the related shell options. We need to
537 # unset "set -f" and "GLOBIGNORE", which disable the pathname expansion
538 # totally or partially. We need to unset "dotglob" and "nocaseglob" to
539 # avoid matching unwanted files. We need to unset "failglob" to avoid
540 # outputting the error messages to the terminal when no matching is
541 # found. We need to set "nullglob" to avoid attempting to read the
542 # literal filename '~/.ssh/config.d/*' when no matching is found.
543 set +f
544 GLOBIGNORE=
545 shopt -u dotglob nocaseglob failglob
546 shopt -s nullglob
547
548 __fzf_exec_awk '
549 # Note: mawk <= 1.3.3-20090705 does not support the POSIX brackets of
550 # the form [[:blank:]], and Ubuntu 18.04 LTS still uses this
551 # 16-year-old mawk unfortunately. We need to use [ \t] instead.
552 match(tolower($0), /^[ \t]*host(name)?[ \t]*[ \t=]/) {
553 $0 = substr($0, RLENGTH + 1) # Remove "Host(name)?=?"
554 sub(/#.*/, "")
555 for (i = 1; i <= NF; i++)
556 if ($i !~ /[*?%]/)
557 print $i
558 }
559 ' ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null
560 ) \
561 <(
562 __fzf_exec_awk -F ',' '
563 match($0, /^[][a-zA-Z0-9.,:-]+/) {
564 $0 = substr($0, 1, RLENGTH)
565 gsub(/[][]|:[^,]*/, "")
566 for (i = 1; i <= NF; i++)
567 print $i
568 }
569 ' ~/.ssh/known_hosts 2> /dev/null
570 ) \
571 <(
572 __fzf_exec_awk '
573 {
574 sub(/#.*/, "")
575 for (i = 2; i <= NF; i++)
576 if ($i != "0.0.0.0")
577 print $i
578 }
579 ' /etc/hosts 2> /dev/null
580 )
581 }
582 fi
583
584 _fzf_host_completion() {
585 _fzf_complete +m -- "$@" < <(__fzf_list_hosts)
586 }
587
588 # Values for $1 $2 $3 are described here
589 # https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
590 # > the first argument ($1) is the name of the command whose arguments are being completed,
591 # > the second argument ($2) is the word being completed,
592 # > and the third argument ($3) is the word preceding the word being completed on the current command line.
593 _fzf_complete_ssh() {
594 case $3 in
595 -i | -F | -E)
596 _fzf_path_completion "$@"
597 ;;
598 *)
599 local user=
600 [[ $2 =~ '@' ]] && user="${2%%@*}@"
601 _fzf_complete +m -- "$@" < <(__fzf_list_hosts | __fzf_exec_awk -v user="$user" '{print user $0}')
602 ;;
603 esac
604 }
605
606 _fzf_var_completion() {
607 _fzf_complete -m -- "$@" < <(
608 declare -xp | command sed -En 's|^declare [^ ]+ ([^=]+).*|\1|p'
609 )
610 }
611
612 _fzf_alias_completion() {
613 _fzf_complete -m -- "$@" < <(
614 alias | command sed -En 's|^alias ([^=]+).*|\1|p'
615 )
616 }
617
618 # fzf options
619 complete -o default -F _fzf_opts_completion fzf
620 # fzf-tmux is a thin fzf wrapper that has only a few more options than fzf
621 # itself. As a quick improvement we take fzf's completion. Adding the few extra
622 # fzf-tmux specific options (like `-w WIDTH`) are left as a future patch.
623 complete -o default -F _fzf_opts_completion fzf-tmux
624
625 # Default path completion
626 __fzf_default_completion() {
627 __fzf_generic_path_completion _fzf_compgen_path "-m" "" "$@"
628
629 # Dynamic completion loader has updated the completion for the command
630 if [[ $? -eq 124 ]]; then
631 # We trigger _fzf_setup_completion so that fuzzy completion for the command
632 # still works. However, loader can update the completion for multiple
633 # commands at once, and fuzzy completion will no longer work for those
634 # other commands. e.g. pytest -> py.test, pytest-2, pytest-3, etc
635 _fzf_setup_completion path "$1"
636 return 124
637 fi
638 }
639
640 # Set fuzzy path completion as the default completion for all commands.
641 # We can't set up default completion,
642 # 1. if it's already set up by another script
643 # 2. or if the current version of bash doesn't support -D option
644 complete | command grep -q __fzf_default_completion ||
645 complete | command grep -- '-D$' | command grep -qv _comp_complete_load ||
646 complete -D -F __fzf_default_completion -o default -o bashdefault 2> /dev/null
647
648 d_cmds="${FZF_COMPLETION_DIR_COMMANDS-cd pushd rmdir}"
649
650 # NOTE: $FZF_COMPLETION_PATH_COMMANDS and $FZF_COMPLETION_VAR_COMMANDS are
651 # undocumented and subject to change in the future.
652 #
653 # NOTE: Although we have default completion, we still need to set up completion
654 # for each command in case they already have completion set up by another script.
655 a_cmds="${FZF_COMPLETION_PATH_COMMANDS-"
656 awk bat cat code diff diff3
657 emacs emacsclient ex file ftp g++ gcc gvim head hg hx java
658 javac ld less more mvim nvim patch perl python ruby
659 sed sftp sort source tail tee uniq vi view vim wc xdg-open
660 basename bunzip2 bzip2 chmod chown curl cp dirname du
661 find git grep gunzip gzip hg jar
662 ln ls mv open rm rsync scp
663 svn tar unzip zip"}"
664 v_cmds="${FZF_COMPLETION_VAR_COMMANDS-export unset printenv}"
665
666 # Preserve existing completion
667 __fzf_orig_completion < <(complete -p $d_cmds $a_cmds $v_cmds unalias kill ssh 2> /dev/null)
668
669 if type _comp_load > /dev/null 2>&1; then
670 # _comp_load was added in bash-completion 2.12 to replace _completion_loader.
671 # We use it without -D option so that it does not use _comp_complete_minimal as the fallback.
672 _fzf_completion_loader=_comp_load
673 elif type __load_completion > /dev/null 2>&1; then
674 # In bash-completion 2.11, _completion_loader internally calls __load_completion
675 # and if it returns a non-zero status, it sets the default 'minimal' completion.
676 _fzf_completion_loader=__load_completion
677 elif type _completion_loader > /dev/null 2>&1; then
678 _fzf_completion_loader=_completion_loader
679 fi
680
681 __fzf_defc() {
682 local cmd func opts REPLY
683 cmd="$1"
684 func="$2"
685 opts="$3"
686 if __fzf_orig_completion_instantiate "$cmd" "$func"; then
687 eval "$REPLY"
688 else
689 eval "complete -F \"$func\" $opts \"$cmd\""
690 fi
691 }
692
693 # Anything
694 for cmd in $a_cmds; do
695 __fzf_defc "$cmd" _fzf_path_completion "-o default -o bashdefault"
696 done
697
698 # Directory
699 for cmd in $d_cmds; do
700 __fzf_defc "$cmd" _fzf_dir_completion "-o bashdefault -o nospace -o dirnames"
701 done
702
703 # Variables
704 for cmd in $v_cmds; do
705 __fzf_defc "$cmd" _fzf_var_completion "-o default -o nospace -v"
706 done
707
708 # Aliases
709 __fzf_defc unalias _fzf_alias_completion "-a"
710
711 # Processes
712 __fzf_defc kill _fzf_proc_completion "-o default -o bashdefault"
713
714 # ssh
715 __fzf_defc ssh _fzf_complete_ssh "-o default -o bashdefault"
716
717 unset cmd d_cmds a_cmds v_cmds
718
719 _fzf_setup_completion() {
720 local kind fn cmd
721 kind=$1
722 fn=_fzf_${1}_completion
723 if [[ $# -lt 2 ]] || ! type -t "$fn" > /dev/null; then
724 echo "usage: ${FUNCNAME[0]} path|dir|var|alias|host|proc COMMANDS..."
725 return 1
726 fi
727 shift
728 __fzf_orig_completion < <(complete -p "$@" 2> /dev/null)
729 for cmd in "$@"; do
730 case "$kind" in
731 dir) __fzf_defc "$cmd" "$fn" "-o nospace -o dirnames" ;;
732 var) __fzf_defc "$cmd" "$fn" "-o default -o nospace -v" ;;
733 alias) __fzf_defc "$cmd" "$fn" "-a" ;;
734 *) __fzf_defc "$cmd" "$fn" "-o default -o bashdefault" ;;
735 esac
736 done
737 }
738 #----END shfmt
739
740 fi