aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-09-30 14:21:12 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-09-30 14:30:15 +1300
commitcd05e5f4cd6d8a06486543cee97b8b4517ee82a8 (patch)
treec8f62c768e602129921b4d1c140963242bdd8a37 /bash/bash_completion.d
parentStop `keep` writing empty files for undef vars (diff)
downloaddotfiles-cd05e5f4cd6d8a06486543cee97b8b4517ee82a8.tar.gz
dotfiles-cd05e5f4cd6d8a06486543cee97b8b4517ee82a8.zip
Add -d completion for keep()
Diffstat (limited to 'bash/bash_completion.d')
-rw-r--r--bash/bash_completion.d/keep.bash56
1 files changed, 54 insertions, 2 deletions
diff --git a/bash/bash_completion.d/keep.bash b/bash/bash_completion.d/keep.bash
index 3a418bb8..41423af0 100644
--- a/bash/bash_completion.d/keep.bash
+++ b/bash/bash_completion.d/keep.bash
@@ -1,2 +1,54 @@
-# Complete calls to keep() with existing function names and variable names
-complete -A function -A variable keep
+# Complete calls to keep() with variables and functions, or if -d is given with
+# stuff that's already kept
+_keep() {
+
+ # Default is to complete with function and variable names
+ local mode
+ mode=names
+
+ # Iterate through the words up to the previous word to figure out how to
+ # complete this one
+ local i
+ for ((i = 0; i < COMP_CWORD; i++)) ; do
+ case ${COMP_WORDS[i]} in
+ --)
+ mode=names
+ break
+ ;;
+ -d)
+ mode=kept
+ break
+ ;;
+ esac
+ done
+
+ # Complete with appropriate mode
+ case $mode in
+ names)
+ while IFS= read -r word ; do
+ [[ -n $word ]] || continue
+ COMPREPLY[${#COMPREPLY[@]}]=$word
+ done < <(compgen -A function -A variable \
+ -- "${COMP_WORDS[COMP_CWORD]}")
+ ;;
+ kept)
+ while IFS= read -r word ; do
+ [[ -n $word ]] || continue
+ COMPREPLY[${#COMPREPLY[@]}]=$word
+ done < <(
+ shopt -s dotglob nullglob
+ keep=${BASHKEEP:-"$HOME"/.bashkeep.d}
+ declare -a keeps
+ keeps=("$keep"/"${COMP_WORDS[COMP_CWORD]}"*.bash)
+ keeps=("${keeps[@]##*/}")
+ keeps=("${keeps[@]%.bash}")
+ if ((${#keeps[@]})) ; then
+ printf '%s\n' "${keeps[@]}"
+ else
+ printf '\n'
+ fi
+ )
+ ;;
+ esac
+}
+complete -F _keep keep