From b6c540ded9a527f19b8bff7888e330ba2786f552 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 1 Dec 2018 13:15:28 +1300 Subject: Use the positional parameter aliases for words The current word is available in $2, and the previous word in $3. That's easier (and maybe a bit less expensive) to dig out, so let's use it. --- bash/bash_completion.d/_abook_addresses.bash | 2 +- bash/bash_completion.d/_ssh_config_hosts.bash | 4 ++-- bash/bash_completion.d/_text_filenames.bash | 2 +- bash/bash_completion.d/chgrp.bash | 2 +- bash/bash_completion.d/eds.bash | 2 +- bash/bash_completion.d/find.bash | 14 +++++++------- bash/bash_completion.d/ftp.bash | 2 +- bash/bash_completion.d/git.bash | 16 ++++++++-------- bash/bash_completion.d/gpg.bash | 4 ++-- bash/bash_completion.d/keep.bash | 4 ++-- bash/bash_completion.d/kill.bash | 2 +- bash/bash_completion.d/make.bash | 2 +- bash/bash_completion.d/man.bash | 14 +++++--------- bash/bash_completion.d/mysql.bash | 2 +- bash/bash_completion.d/openssl.bash | 2 +- bash/bash_completion.d/pass.bash | 4 ++-- bash/bash_completion.d/path.bash | 6 +++--- bash/bash_completion.d/sd.bash | 2 +- bash/bash_completion.d/td.bash | 2 +- bash/bash_completion.d/ud.bash | 2 +- 20 files changed, 43 insertions(+), 47 deletions(-) (limited to 'bash') diff --git a/bash/bash_completion.d/_abook_addresses.bash b/bash/bash_completion.d/_abook_addresses.bash index e79eef42..e1a94bc7 100644 --- a/bash/bash_completion.d/_abook_addresses.bash +++ b/bash/bash_completion.d/_abook_addresses.bash @@ -24,7 +24,7 @@ _abook_addresses() { # Generate list of email addresses from abook(1) while IFS=$'\t' read -r address _ ; do case $address in - ("${COMP_WORDS[COMP_CWORD]}"*) + ("$2"*) printf '%s\n' "$address" ;; esac diff --git a/bash/bash_completion.d/_ssh_config_hosts.bash b/bash/bash_completion.d/_ssh_config_hosts.bash index dc75d4bc..0c1eb379 100644 --- a/bash/bash_completion.d/_ssh_config_hosts.bash +++ b/bash/bash_completion.d/_ssh_config_hosts.bash @@ -2,7 +2,7 @@ _ssh_config_hosts() { # Don't complete anything that wouldn't be in a valid hostname - case ${COMP_WORDS[COMP_CWORD]} in + case $2 in *[!a-zA-Z0-9.-]*) return 1 ;; esac @@ -37,7 +37,7 @@ _ssh_config_hosts() { (*'*'*) ;; # Found a match; print it - ("${COMP_WORDS[COMP_CWORD]}"*) + ("$2"*) printf '%s\n' "$value" ;; esac diff --git a/bash/bash_completion.d/_text_filenames.bash b/bash/bash_completion.d/_text_filenames.bash index 4d576374..caf05e8d 100644 --- a/bash/bash_completion.d/_text_filenames.bash +++ b/bash/bash_completion.d/_text_filenames.bash @@ -155,5 +155,5 @@ _text_filenames() { # Complete everything else; some of it will still be binary COMPREPLY[${#COMPREPLY[@]}]=$item - done < <(compgen -A file -- "${COMP_WORDS[COMP_CWORD]}") + done < <(compgen -A file -- "$2") } diff --git a/bash/bash_completion.d/chgrp.bash b/bash/bash_completion.d/chgrp.bash index 5e93ccee..e63651c5 100644 --- a/bash/bash_completion.d/chgrp.bash +++ b/bash/bash_completion.d/chgrp.bash @@ -9,6 +9,6 @@ _chgrp() { done while read -r group ; do COMPREPLY[${#COMPREPLY[@]}]=$group - done < <(compgen -A group -- "${COMP_WORDS[COMP_CWORD]}") + done < <(compgen -A group -- "$2") } complete -F _chgrp -o bashdefault -o default chgrp diff --git a/bash/bash_completion.d/eds.bash b/bash/bash_completion.d/eds.bash index 1c5b2aa2..da6bb879 100644 --- a/bash/bash_completion.d/eds.bash +++ b/bash/bash_completion.d/eds.bash @@ -22,7 +22,7 @@ _eds() { done < <(bind -v) declare -a files - files=("${EDSPATH:-"$HOME"/.local/bin}"/"${COMP_WORDS[COMP_CWORD]}"*) + files=("${EDSPATH:-"$HOME"/.local/bin}"/"$2"*) declare -a executables for file in "${files[@]}" ; do ! [[ -d $file ]] || continue diff --git a/bash/bash_completion.d/find.bash b/bash/bash_completion.d/find.bash index 7e2ae9c3..7593c594 100644 --- a/bash/bash_completion.d/find.bash +++ b/bash/bash_completion.d/find.bash @@ -36,7 +36,7 @@ _find() { # If the word being completed starts with a dash, just complete it as # an option; crude, but simple, and will be right the vast majority of # the time - case ${COMP_WORDS[COMP_CWORD]} in + case $2 in (-*) compgen -W ' -atime @@ -58,32 +58,32 @@ _find() { -type -user -xdev - ' -- "${COMP_WORDS[COMP_CWORD]}" + ' -- "$2" ;; esac # Otherwise, look at the word *before* this one to figure out what to # complete - case ${COMP_WORDS[COMP_CWORD-1]} in + case $3 in # Args to -exec and -execdir should be commands (-exec|-execdir) - compgen -A command -- "${COMP_WORDS[COMP_CWORD]}" + compgen -A command -- "$2" ;; # Args to -group should complete group names (-group) - compgen -A group -- "${COMP_WORDS[COMP_CWORD]}" + compgen -A group -- "$2" ;; # Legal POSIX flags for -type (-type) - compgen -W 'b c d f l p s' -- "${COMP_WORDS[COMP_CWORD]}" + compgen -W 'b c d f l p s' -- "$2" ;; # Args to -user should complete usernames (-user) - compgen -A user -- "${COMP_WORDS[COMP_CWORD]}" + compgen -A user -- "$2" ;; esac ) diff --git a/bash/bash_completion.d/ftp.bash b/bash/bash_completion.d/ftp.bash index a584dd81..f174b5ff 100644 --- a/bash/bash_completion.d/ftp.bash +++ b/bash/bash_completion.d/ftp.bash @@ -26,7 +26,7 @@ _ftp() { # Generate completion reply local machine for machine in "${machines[@]}" ; do - [[ $machine == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue + [[ $machine == "$2"* ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$machine done } diff --git a/bash/bash_completion.d/git.bash b/bash/bash_completion.d/git.bash index a2edb468..a05655c3 100644 --- a/bash/bash_completion.d/git.bash +++ b/bash/bash_completion.d/git.bash @@ -15,7 +15,7 @@ _git() { ref=${ref#refs/*/} case $ref in '') continue ;; - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$ref ;; esac @@ -29,7 +29,7 @@ _git() { while IFS= read -r remote ; do case $remote in '') continue ;; - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$remote ;; esac @@ -45,7 +45,7 @@ _git() { alias=${alias%% *} case $alias in '') continue ;; - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$alias ;; esac @@ -58,7 +58,7 @@ _git() { local execpath execpath=$(git --exec-path) || return local path - for path in "$execpath"/git-"${COMP_WORDS[COMP_CWORD]}"* ; do + for path in "$execpath"/git-"$2"* ; do ! [[ -d $path ]] || continue [[ -e $path ]] || continue [[ -x $path ]] || continue @@ -121,7 +121,7 @@ _git() { set-url show update - ' -- "${COMP_WORDS[COMP_CWORD]}") + ' -- "$2") else "${FUNCNAME[0]}" remotes fi @@ -146,7 +146,7 @@ _git() { save show store - ' -- "${COMP_WORDS[COMP_CWORD]}") + ' -- "$2") return ;; @@ -166,7 +166,7 @@ _git() { summary sync update - ' -- "${COMP_WORDS[COMP_CWORD]}") + ' -- "$2") return ;; @@ -187,7 +187,7 @@ _git() { # I normally only want a refspec for "reset" if I'm using the --hard or # --soft option; otherwise, files are fine reset) - case ${COMP_WORDS[COMP_CWORD-1]} in + case $3 in --hard|--soft) "${FUNCNAME[0]}" refs ;; diff --git a/bash/bash_completion.d/gpg.bash b/bash/bash_completion.d/gpg.bash index a47b23d7..bfa2d1a9 100644 --- a/bash/bash_completion.d/gpg.bash +++ b/bash/bash_completion.d/gpg.bash @@ -5,7 +5,7 @@ _gpg() { hash gpg 2>/dev/null || return # Bail if not completing an option - case ${COMP_WORDS[COMP_CWORD]} in + case $2 in --*) ;; *) return 1 ;; esac @@ -14,7 +14,7 @@ _gpg() { local option while read -r option ; do case $option in - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$option ;; esac diff --git a/bash/bash_completion.d/keep.bash b/bash/bash_completion.d/keep.bash index 00b1469e..6829db9c 100644 --- a/bash/bash_completion.d/keep.bash +++ b/bash/bash_completion.d/keep.bash @@ -25,7 +25,7 @@ _keep() { # Keepable names: all functions and variables (keep) compgen -A function -A variable \ - -- "${COMP_WORDS[COMP_CWORD]}" + -- "$2" ;; # Kept names: .bash-suffixed names in keep dir @@ -43,7 +43,7 @@ _keep() { # Build list of kept names dir=${BASHKEEP:-"$HOME"/.bashkeep.d} - cword=${COMP_WORDS[COMP_CWORD]} + cword=$2 kept=("$dir"/"$cword"*.bash) kept=("${kept[@]##*/}") kept=("${kept[@]%.bash}") diff --git a/bash/bash_completion.d/kill.bash b/bash/bash_completion.d/kill.bash index dccc926b..bdb42ec1 100644 --- a/bash/bash_completion.d/kill.bash +++ b/bash/bash_completion.d/kill.bash @@ -4,7 +4,7 @@ _kill() { local pid while read -r pid ; do case $pid in - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$pid ;; esac diff --git a/bash/bash_completion.d/make.bash b/bash/bash_completion.d/make.bash index 0f39ef4b..8ca36529 100644 --- a/bash/bash_completion.d/make.bash +++ b/bash/bash_completion.d/make.bash @@ -40,7 +40,7 @@ _make() { *[^[:word:]./-]*) ;; # Add targets that match what we're completing - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$target ;; esac diff --git a/bash/bash_completion.d/man.bash b/bash/bash_completion.d/man.bash index ffef48ec..b5ecaa3e 100644 --- a/bash/bash_completion.d/man.bash +++ b/bash/bash_completion.d/man.bash @@ -4,13 +4,9 @@ _man() { # Don't even bother if we don't have manpath(1) hash manpath 2>/dev/null || return - # Snarf the word - local word - word=${COMP_WORDS[COMP_CWORD]} - # Don't bother if the word has slashes in it, the user is probably trying # to complete an actual path - case $word in + case $2 in */*) return 1 ;; esac @@ -18,9 +14,9 @@ _man() { # we'll assume that's the section to search local section subdir if ((COMP_CWORD > 1)) ; then - case ${COMP_WORDS[COMP_CWORD-1]} in + case $3 in [0-9]*) - section=${COMP_WORDS[COMP_CWORD-1]} + section=$3 subdir=man${section%%[^0-9]*} ;; esac @@ -59,12 +55,12 @@ _man() { [[ -n $manpath ]] || continue if [[ -n $section ]] ; then for page in \ - "$manpath"/"$subdir"/"$word"*."$section"?(.[glx]z|.bz2|.lzma|.Z) + "$manpath"/"$subdir"/"$2"*."$section"?(.[glx]z|.bz2|.lzma|.Z) do pages[${#pages[@]}]=$page done else - for page in "$manpath"/man[0-9]*/"$word"*.* ; do + for page in "$manpath"/man[0-9]*/"$2"*.* ; do pages[${#pages[@]}]=$page done fi diff --git a/bash/bash_completion.d/mysql.bash b/bash/bash_completion.d/mysql.bash index 3ff97090..b6c3ce93 100644 --- a/bash/bash_completion.d/mysql.bash +++ b/bash/bash_completion.d/mysql.bash @@ -31,7 +31,7 @@ _mysql() { # Collect all the config file names, strip off leading path and .cnf local -a cnfs - cnfs=("$dirname"/"${COMP_WORDS[COMP_CWORD]}"*.cnf) + cnfs=("$dirname"/"$2"*.cnf) cnfs=("${cnfs[@]#"$dirname"/}") cnfs=("${cnfs[@]%.cnf}") diff --git a/bash/bash_completion.d/openssl.bash b/bash/bash_completion.d/openssl.bash index 86650770..3396eb9f 100644 --- a/bash/bash_completion.d/openssl.bash +++ b/bash/bash_completion.d/openssl.bash @@ -7,7 +7,7 @@ _openssl() { while read -r subcmd ; do case $subcmd in '') ;; - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$subcmd ;; esac diff --git a/bash/bash_completion.d/pass.bash b/bash/bash_completion.d/pass.bash index 176886dc..c1246757 100644 --- a/bash/bash_completion.d/pass.bash +++ b/bash/bash_completion.d/pass.bash @@ -35,8 +35,8 @@ _pass() # Gather the entries and remove their .gpg suffix declare -a entries - entries=("$passdir"/"${COMP_WORDS[COMP_CWORD]}"*/**/*.gpg \ - "$passdir"/"${COMP_WORDS[COMP_CWORD]}"*.gpg) + entries=("$passdir"/"$2"*/**/*.gpg \ + "$passdir"/"$2"*.gpg) entries=("${entries[@]#"$passdir"/}") entries=("${entries[@]%.gpg}") diff --git a/bash/bash_completion.d/path.bash b/bash/bash_completion.d/path.bash index 7143b448..8db6a74a 100644 --- a/bash/bash_completion.d/path.bash +++ b/bash/bash_completion.d/path.bash @@ -17,7 +17,7 @@ _path() { pop remove shift - ' -- "${COMP_WORDS[COMP_CWORD]}") + ' -- "$2") # Complete with either directories or $PATH entries as all other words else @@ -46,7 +46,7 @@ _path() { # Collect directory names, strip trailing slash local -a dirnames - dirnames=("${COMP_WORDS[COMP_CWORD]}"*/) + dirnames=("$2"*/) dirnames=("${dirnames[@]%/}") # Print quoted entries, null-delimited @@ -62,7 +62,7 @@ _path() { local part for part in "${promptarr[@]}" ; do case $part in - "${COMP_WORDS[COMP_CWORD]}"*) + "$2"*) COMPREPLY[${#COMPREPLY[@]}]=$(printf '%q' "$part") ;; esac diff --git a/bash/bash_completion.d/sd.bash b/bash/bash_completion.d/sd.bash index e7e82f80..d6e93c78 100644 --- a/bash/bash_completion.d/sd.bash +++ b/bash/bash_completion.d/sd.bash @@ -31,7 +31,7 @@ _sd() { # Collect directory names, strip leading ../ and trailing / local -a dirnames - dirnames=(../"${COMP_WORDS[COMP_CWORD]}"*/) + dirnames=(../"$2"*/) dirnames=("${dirnames[@]#../}") dirnames=("${dirnames[@]%/}") diff --git a/bash/bash_completion.d/td.bash b/bash/bash_completion.d/td.bash index 92927c28..38dc51a3 100644 --- a/bash/bash_completion.d/td.bash +++ b/bash/bash_completion.d/td.bash @@ -26,7 +26,7 @@ _td() { done < <(bind -v) declare -a fns - fns=("$dir"/"${COMP_WORDS[COMP_CWORD]}"*) + fns=("$dir"/"$2"*) fns=("${fns[@]#"$dir"/}") # Print quoted entries, null-delimited diff --git a/bash/bash_completion.d/ud.bash b/bash/bash_completion.d/ud.bash index c7dee582..a9912f5a 100644 --- a/bash/bash_completion.d/ud.bash +++ b/bash/bash_completion.d/ud.bash @@ -25,7 +25,7 @@ _ud() { done < <(bind -v) # Collect directory names, strip trailing slashes - dirnames=("${COMP_WORDS[COMP_CWORD]}"*/) + dirnames=("$2"*/) dirnames=("${dirnames[@]%/}") # Print results null-delimited -- cgit v1.2.3