Skip to content
Myr

completion.zsh

This file

Path

shell/completion.zsh

329 lines · 10.5 KiB · Shell

Last changed by

[completion] Make host completion handle source files without EOLElvan Owenbefore adoption2986e64

History of this file

Take it

The walk stopped after 200 commits: lines older than a1bcdc2 point at it.

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 1–4why, one step back
1# ____ ____
2# / __/___ / __/3# / /_/_ / / /_4# / __/ / /_/ __/

[fzf-tmux] Add option to start fzf in tmux popup windowJunegunn Choibefore adoptionbf65e8c

line 5why, one step back
5# /_/ /___/_/ completion.zsh

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 6–7why, one step back
6#
7# - $FZF_TMUX (default: 0)

[fzf-tmux] Add option to start fzf in tmux popup windowJunegunn Choibefore adoptionbf65e8c

line 8why, one step back
8# - $FZF_TMUX_OPTS (default: '-d 40%')

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 9–11why, one step back
9# - $FZF_COMPLETION_TRIGGER (default: '**')
10# - $FZF_COMPLETION_OPTS (default: empty)11

[zsh] Ensure that fzf code always parses the same way (#1944)Roman Perepelitsabefore adoptionb1b916c

lines 12–59why, one step back
12# Both branches of the following `if` do the same thing -- define
13# __fzf_completion_options such that `eval $__fzf_completion_options` sets14# all options to the same values they currently have. We'll do just that at15# the bottom of the file after changing options to what we prefer.16#17# IMPORTANT: Until we get to the `emulate` line, all words that *can* be quoted18# *must* be quoted in order to prevent alias expansion. In addition, code must19# be written in a way works with any set of zsh options. This is very tricky, so20# careful when you change it.21#22# Start by loading the builtin zsh/parameter module. It provides `options`23# associative array that stores current shell options.24if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then25 # This is the fast branch and it gets taken on virtually all Zsh installations.26 #27 # ${(kv)options[@]} expands to array of keys (option names) and values ("on"28 # or "off"). The subsequent expansion# with (j: :) flag joins all elements29 # together separated by spaces. __fzf_completion_options ends up with a value30 # like this: "options=(shwordsplit off aliases on ...)".31 __fzf_completion_options="options=(${(j: :)${(kv)options[@]}})"32else33 # This branch is much slower because it forks to get the names of all34 # zsh options. It's possible to eliminate this fork but it's not worth the35 # trouble because this branch gets taken only on very ancient or broken36 # zsh installations.37 () {38 # That `()` above defines an anonymous function. This is essentially a scope39 # for local parameters. We use it to avoid polluting global scope.40 'local' '__fzf_opt'41 __fzf_completion_options="setopt"42 # `set -o` prints one line for every zsh option. Each line contains option43 # name, some spaces, and then either "on" or "off". We just want option names.44 # Expansion with (@f) flag splits a string into lines. The outer expansion45 # removes spaces and everything that follow them on every line. __fzf_opt46 # ends up iterating over option names: shwordsplit, aliases, etc.47 for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do48 if [[ -o "$__fzf_opt" ]]; then49 # Option $__fzf_opt is currently on, so remember to set it back on.50 __fzf_completion_options+=" -o $__fzf_opt"51 else52 # Option $__fzf_opt is currently off, so remember to set it back off.53 __fzf_completion_options+=" +o $__fzf_opt"54 fi55 done56 # The value of __fzf_completion_options here looks like this:57 # "setopt +o shwordsplit -o aliases ..."58 }59fi

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 60why, one step back
60

[zsh] Ensure that fzf code always parses the same way (#1944)Roman Perepelitsabefore adoptionb1b916c

lines 61–78why, one step back
61# Enable the default zsh options (those marked with <Z> in `man zshoptions`)
62# but without `aliases`. Aliases in functions are expanded when functions are63# defined, so if we disable aliases here, we'll be sure to have no pesky64# aliases in any of our functions. This way we won't need prefix every65# command with `command` or to quote every word to defend against global66# aliases. Note that `aliases` is not the only option that's important to67# control. There are several others that could wreck havoc if they are set68# to values we don't expect. With the following `emulate` command we69# sidestep this issue entirely.70'emulate' 'zsh' '-o' 'no_aliases'7172# This brace is the start of try-always block. The `always` part is like73# `finally` in lesser languages. We use it to *always* restore user options.74{7576# Bail out if not interactive shell.77[[ -o interactive ]] || return 078

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 79–98why, one step back
79# To use custom commands instead of find, override _fzf_compgen_{path,dir}
80if ! declare -f _fzf_compgen_path > /dev/null; then81 _fzf_compgen_path() {82 echo "$1"83 command find -L "$1" \84 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \85 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'86 }87fi8889if ! declare -f _fzf_compgen_dir > /dev/null; then90 _fzf_compgen_dir() {91 command find -L "$1" \92 -name .git -prune -o -name .hg -prune -o -name .svn -prune -o -type d \93 -a -not -path "$1" -print 2> /dev/null | sed 's@^\./@@'94 }95fi9697###########################################################98

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

lines 99–101why, one step back
99__fzf_comprun() {
100 if [[ "$(type _fzf_comprun 2>&1)" =~ function ]]; then101 _fzf_comprun "$@"

[fzf-tmux] Fall back to plain fzf when split failedJunegunn Choibefore adoption540bfd7

line 102why, one step back
102 elif [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; }; then

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 103why, one step back
103 shift

[fzf-tmux] Split zsh variable expansion for old zshJunegunn Choibefore adoption21f94ee

lines 104–108why, one step back
104 if [ -n "$FZF_TMUX_OPTS" ]; then
105 fzf-tmux ${(Q)${(Z+n+)FZF_TMUX_OPTS}} -- "$@"106 else107 fzf-tmux -d ${FZF_TMUX_HEIGHT:-40%} -- "$@"108 fi

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

lines 109–112why, one step back
109 else
110 shift111 fzf "$@"112 fi

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 113–114why, one step back
113}
114

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

lines 115–119why, one step back
115# Extract the name of the command. e.g. foo=1 bar baz**<tab>
116__fzf_extract_command() {117 local token tokens118 tokens=(${(z)1})119 for token in $tokens; do

[zsh-completion] Fix error with backslash-prefixed commandsJunegunn Choibefore adoptiondea206b

line 120why, one step back
120 token=${(Q)token}

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

lines 121–128why, one step back
121 if [[ "$token" =~ [[:alnum:]] && ! "$token" =~ "=" ]]; then
122 echo "$token"123 return124 fi125 done126 echo "${tokens[1]}"127}128

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 129why, one step back
129__fzf_generic_path_completion() {

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 130why, one step back
130 local base lbuf cmd compgen fzf_opts suffix tail dir leftover matches

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 131–132why, one step back
131 base=$1
132 lbuf=$2

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 133why, one step back
133 cmd=$(__fzf_extract_command "$lbuf")

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 134–147why, one step back
134 compgen=$3
135 fzf_opts=$4136 suffix=$5137 tail=$6138139 setopt localoptions nonomatch140 eval "base=$base"141 [[ $base = *"/"* ]] && dir="$base"142 while [ 1 ]; do143 if [[ -z "$dir" || -d ${dir} ]]; then144 leftover=${base/#"$dir"}145 leftover=${leftover/#\/}146 [ -z "$dir" ] && dir='.'147 [ "$dir" != "/" ] && dir="${dir/%\//}"

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 148why, one step back
148 matches=$(eval "$compgen $(printf %q "$dir")" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS" __fzf_comprun "$cmd" ${(Q)${(Z+n+)fzf_opts}} -q "$leftover" | while read item; do

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 149–179why, one step back
149 echo -n "${(q)item}$suffix "
150 done)151 matches=${matches% }152 if [ -n "$matches" ]; then153 LBUFFER="$lbuf$matches$tail"154 fi155 zle reset-prompt156 break157 fi158 dir=$(dirname "$dir")159 dir=${dir%/}/160 done161}162163_fzf_path_completion() {164 __fzf_generic_path_completion "$1" "$2" _fzf_compgen_path \165 "-m" "" " "166}167168_fzf_dir_completion() {169 __fzf_generic_path_completion "$1" "$2" _fzf_compgen_dir \170 "" "/" ""171}172173_fzf_feed_fifo() (174 command rm -f "$1"175 mkfifo "$1"176 cat <&0 > "$1" &177)178179_fzf_complete() {

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

lines 180–184why, one step back
180 setopt localoptions ksh_arrays
181 # Split arguments around --182 local args rest str_arg i sep183 args=("$@")184 sep=

[zsh-completion] Fix a bug where _fzf_complete did not iterate through args (#1936)Chitokubefore adoption0790468

line 185why, one step back
185 for i in {0..${#args[@]}}; do

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

lines 186–202why, one step back
186 if [[ "${args[$i]}" = -- ]]; then
187 sep=$i188 break189 fi190 done191 if [[ -n "$sep" ]]; then192 str_arg=193 rest=("${args[@]:$((sep + 1)):${#args[@]}}")194 args=("${args[@]:0:$sep}")195 else196 str_arg=$1197 args=()198 shift199 rest=("$@")200 fi201202 local fifo lbuf cmd matches post

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 203why, one step back
203 fifo="${TMPDIR:-/tmp}/fzf-complete-fifo-$$"

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 204why, one step back
204 lbuf=${rest[0]}

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 205why, one step back
205 cmd=$(__fzf_extract_command "$lbuf")

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 206why, one step back
206 post="${funcstack[1]}_post"

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 207–209why, one step back
207 type $post > /dev/null 2>&1 || post=cat
208209 _fzf_feed_fifo "$fifo"

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 210why, one step back
210 matches=$(FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS $str_arg" __fzf_comprun "$cmd" "${args[@]}" -q "${(Q)prefix}" < "$fifo" | $post | tr '\n' ' ')

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 211–218why, one step back
211 if [ -n "$matches" ]; then
212 LBUFFER="$lbuf$matches"213 fi214 zle reset-prompt215 command rm -f "$fifo"216}217218_fzf_complete_telnet() {

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 219why, one step back
219 _fzf_complete +m -- "$@" < <(

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 220–225why, one step back
220 command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' |
221 awk '{if (length($2) > 0) {print $2}}' | sort -u222 )223}224225_fzf_complete_ssh() {

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 226why, one step back
226 _fzf_complete +m -- "$@" < <(

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 227why, one step back
227 setopt localoptions nonomatch

[completion] Make host completion handle source files without EOLElvan Owenbefore adoption2986e64

line 228why, one step back
228 command cat <(command tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2> /dev/null | command grep -i '^\s*host\(name\)\? ' | awk '{for (i = 2; i <= NF; i++) print $1 " " $i}' | command grep -v '[*?]') \

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 229–235why, one step back
229 <(command grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print $1 " " $1 }') \
230 <(command grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0') |231 awk '{if (length($2) > 0) {print $2}}' | sort -u232 )233}234235_fzf_complete_export() {

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 236why, one step back
236 _fzf_complete -m -- "$@" < <(

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 237–241why, one step back
237 declare -xp | sed 's/=.*//' | sed 's/.* //'
238 )239}240241_fzf_complete_unset() {

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 242why, one step back
242 _fzf_complete -m -- "$@" < <(

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 243–247why, one step back
243 declare -xp | sed 's/=.*//' | sed 's/.* //'
244 )245}246247_fzf_complete_unalias() {

Change custom fuzzy completion APIJunegunn Choibefore adoption50b7608

line 248why, one step back
248 _fzf_complete +m -- "$@" < <(

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 249–252why, one step back
249 alias | sed 's/=.*//'
250 )251}252

[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279

lines 253–262why, one step back
253_fzf_complete_kill() {
254 _fzf_complete -m --preview 'echo {}' --preview-window down:3:wrap --min-height 15 -- "$@" < <(255 command ps -ef | sed 1d256 )257}258259_fzf_complete_kill_post() {260 awk '{print $2}'261}262

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 263why, one step back
263fzf-completion() {

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 264why, one step back
264 local tokens cmd prefix trigger tail matches lbuf d_cmds

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 265–274why, one step back
265 setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
266267 # http://zsh.sourceforge.net/FAQ/zshfaq03.html268 # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags269 tokens=(${(z)LBUFFER})270 if [ ${#tokens} -lt 1 ]; then271 zle ${fzf_default_completion:-expand-or-complete}272 return273 fi274

[completion] Allow users to customize fzf options via _fzf_comprunJunegunn Choibefore adoption5215415

line 275why, one step back
275 cmd=$(__fzf_extract_command "$LBUFFER")

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 276–286why, one step back
276
277 # Explicitly allow for empty trigger.278 trigger=${FZF_COMPLETION_TRIGGER-'**'}279 [ -z "$trigger" -a ${LBUFFER[-1]} = ' ' ] && tokens+=("")280281 # When the trigger starts with ';', it becomes a separate token282 if [[ ${LBUFFER} = *"${tokens[-2]}${tokens[-1]}" ]]; then283 tokens[-2]="${tokens[-2]}${tokens[-1]}"284 tokens=(${tokens[0,-2]})285 fi286

[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279

line 287why, one step back
287 lbuf=$LBUFFER

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 288–289why, one step back
288 tail=${LBUFFER:$(( ${#LBUFFER} - ${#trigger} ))}
289 # Kill completion (do not require trigger sequence)

[zsh-completion] Fix error with backslash-prefixed commandsJunegunn Choibefore adoptiondea206b

line 290why, one step back
290 if [ "$cmd" = kill -a ${LBUFFER[-1]} = ' ' ]; then

[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279

lines 291–295why, one step back
291 tail=$trigger
292 tokens+=$trigger293 lbuf="$lbuf$trigger"294 fi295

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

line 296why, one step back
296 # Trigger sequence given

[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279

line 297why, one step back
297 if [ ${#tokens} -gt 1 -a "$tail" = "$trigger" ]; then

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 298–300why, one step back
298 d_cmds=(${=FZF_COMPLETION_DIR_COMMANDS:-cd pushd rmdir})
299300 [ -z "$trigger" ] && prefix=${tokens[-1]} || prefix=${tokens[-1]:0:-${#trigger}}

[completion] Make kill completion more consistent with the othersJunegunn Choibefore adoptionc329279

line 301why, one step back
301 [ -n "${tokens[-1]}" ] && lbuf=${lbuf:0:-${#tokens[-1]}}

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 302–303why, one step back
302
303 if eval "type _fzf_complete_${cmd} > /dev/null"; then

[zsh-completion] Fix quoting/splitting issues (#1820)Chitokubefore adoptionf7b26b3

line 304why, one step back
304 prefix="$prefix" eval _fzf_complete_${cmd} ${(q)lbuf}

Add pledge(2) support (OpenBSD only) via a 'protector' package. (#1297)Aaron Bieberbefore adoptiona1bcdc2

lines 305–324why, one step back
305 elif [ ${d_cmds[(i)$cmd]} -le ${#d_cmds} ]; then
306 _fzf_dir_completion "$prefix" "$lbuf"307 else308 _fzf_path_completion "$prefix" "$lbuf"309 fi310 # Fall back to default completion311 else312 zle ${fzf_default_completion:-expand-or-complete}313 fi314}315316[ -z "$fzf_default_completion" ] && {317 binding=$(bindkey '^I')318 [[ $binding =~ 'undefined-key' ]] || fzf_default_completion=$binding[(s: :w)2]319 unset binding320}321322zle -N fzf-completion323bindkey '^I' fzf-completion324

[zsh] Ensure that fzf code always parses the same way (#1944)Roman Perepelitsabefore adoptionb1b916c

lines 325–329why, one step back
325} always {
326 # Restore the original options.327 eval $__fzf_completion_options328 'unset' '__fzf_completion_options'329}