diff options
author | aurelien <aurelien@xload.io> | 2014-05-30 15:39:50 +0200 |
---|---|---|
committer | aurelien <aurelien@xload.io> | 2014-05-30 15:39:50 +0200 |
commit | 8e7731d4cbf47f3ec349eef842f1386b1616e3e2 (patch) | |
tree | 17a58a1f7fab4e3e7364dd85de026c6e089d3414 /pcr/bash-dynamic-completion/dynamic_completion.sh | |
parent | 7bab1c0a4b638b4aa966d22a996362b07681bc22 (diff) |
bash-dynamic-completion
Diffstat (limited to 'pcr/bash-dynamic-completion/dynamic_completion.sh')
-rw-r--r-- | pcr/bash-dynamic-completion/dynamic_completion.sh | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/pcr/bash-dynamic-completion/dynamic_completion.sh b/pcr/bash-dynamic-completion/dynamic_completion.sh new file mode 100644 index 000000000..bde058af4 --- /dev/null +++ b/pcr/bash-dynamic-completion/dynamic_completion.sh @@ -0,0 +1,60 @@ +#! /bin/sh + +# This script automatically moves the completion +# functions from /etc/bash_completion and /etc/bash_completion.d +# to a separate directory and creates a drop-in replacement for +# /etc/bash_completion - which will load any of those completion +# functions when it is requested for the first time. By this, we +# greatly reduce the loading time (the replacement script is ~30kb) +# and also the memory footprint (as we rarely need every completion +# function in every bash session). +# +# The generator script is intended to be run once the base script +# /etc/bash_completion is installed and then, every time something +# is added to /etc/bash_completion.d. I'd like to propose it for +# the bash package -- maybe as an optional part of installation. +# +# Taken from http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=467231 +# Author: Andrei Paskevich +# andrei@capet.iut-fbleau.fr + +BASH_COMPLETION=/etc/bash_completion +BASH_DYNCOMPLETION=/etc/bash_dyncompletion +BASH_DYNCOMPLETION_DIR=/etc/bash_dyncompletion.d + +echo "Read static completions from $BASH_COMPLETION" +. "$BASH_COMPLETION" || exit 1 + +echo "Create dynamic completion file $BASH_DYNCOMPLETION" +touch "$BASH_DYNCOMPLETION" || exit 1 + +echo "Create dynamic completion directory $BASH_DYNCOMPLETION_DIR" +mkdir -p "$BASH_DYNCOMPLETION_DIR" || exit 1 + +echo -n "Populate dynamic completion directory:" +FUNCS=$(declare -f | grep '^[a-zA-Z_][a-zA-Z0-9_]* () $' | cut -f 1 -d ' ') +for FN in $FUNCS ; do + echo -n " $FN" + declare -f $FN > "$BASH_DYNCOMPLETION_DIR/$FN" + eval "$FN () { . '$BASH_DYNCOMPLETION_DIR/$FN' ; $FN \"\$@\" ; }" +done +echo + +echo "Write dynamic completion file $BASH_DYNCOMPLETION" +exec > "$BASH_DYNCOMPLETION" +cat <<EOF +# $(basename $BASH_DYNCOMPLETION) - load-by-need bash completions +# actual completion functions are stored in $BASH_DYNCOMPLETION_DIR +# this file and directory are generated from $BASH_COMPLETION + +shopt -s extglob progcomp +EOF + +declare -p BASH_COMPLETION 2>/dev/null +declare -p bash205 2>/dev/null +declare -p bash205b 2>/dev/null +declare -p bash3 2>/dev/null +declare -f +complete -p + +exit 0 |