aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-21 15:43:33 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-21 15:59:37 +1200
commit04d93adfe06bb12a63463dfc9a2d5734ed556c36 (patch)
tree7cdcfdf81dbcdcacd53d1854d35b74ae126162a3
parentAnticipate Bath versions > 4 (diff)
downloaddotfiles-04d93adfe06bb12a63463dfc9a2d5734ed556c36.tar.gz
dotfiles-04d93adfe06bb12a63463dfc9a2d5734ed556c36.zip
Use dynamic completion if available
-rw-r--r--bash/bash_completion.d/bash.bash38
-rw-r--r--bash/bashrc21
-rw-r--r--bash/bashrc.d/completion.bash57
3 files changed, 75 insertions, 41 deletions
diff --git a/bash/bash_completion.d/bash.bash b/bash/bash_completion.d/bash.bash
deleted file mode 100644
index 5d944b9b..00000000
--- a/bash/bash_completion.d/bash.bash
+++ /dev/null
@@ -1,38 +0,0 @@
-# Various easy completions
-
-# Bash builtins
-complete -A builtin builtin
-
-# Bash options
-complete -A setopt set
-
-# Commands
-complete -A command command complete coproc exec hash type
-
-# Directories
-complete -A directory cd pushd mkdir rmdir
-
-# Functions
-complete -A function function
-
-# Help topics
-complete -A helptopic help
-
-# Jobspecs
-complete -A job disown fg jobs
-complete -A stopped bg
-
-# Readline bindings
-complete -A binding bind
-
-# Shell options
-complete -A shopt shopt
-
-# Signal names
-complete -A signal trap
-
-# Variables
-complete -A variable declare export readonly typeset
-
-# Both functions and variables
-complete -A function -A variable unset
diff --git a/bash/bashrc b/bash/bashrc
index a24a34fe..e7973562 100644
--- a/bash/bashrc
+++ b/bash/bashrc
@@ -108,9 +108,24 @@ fi
# Load POSIX shell functions, Bash-specific scripts, and Bash completion files,
# in that order
-for sh in "$ENV" \
- "$HOME"/.bashrc.d/*.bash \
- "$HOME"/.bash_completion.d/*.bash ; do
+for sh in "$ENV" "$HOME"/.bashrc.d/*.bash ; do
[[ -e $sh ]] && source "$sh"
done
unset -v sh
+
+# If we have dynamic completion loading (Bash>=4.0), use it
+if ((BASH_VERSINFO[0] >= 4)) ; then
+ _completion_loader() {
+ [[ -n $1 ]] || return
+ compspec=$HOME/.bash_completion.d/$1.bash
+ [[ -f $compspec ]] || return
+ source "$compspec" >/dev/null 2>&1 && return 124
+ }
+
+# If not, load all of the completions up now
+else
+ for sh in "$HOME"/.bash_completion.d/*.bash ; do
+ [[ -e $sh ]] && source "$sh"
+ done
+ unset -v sh
+fi
diff --git a/bash/bashrc.d/completion.bash b/bash/bashrc.d/completion.bash
new file mode 100644
index 00000000..c120878d
--- /dev/null
+++ b/bash/bashrc.d/completion.bash
@@ -0,0 +1,57 @@
+# Various easy completions for Bash builtins; more specific stuff goes in
+# ~/.bash_completion.d
+
+# Bash builtins
+complete -A builtin builtin
+
+# Bash options
+complete -A setopt set
+
+# Commands
+complete -A command command complete coproc exec hash type
+
+# Directories
+complete -A directory cd pushd mkdir rmdir
+
+# Functions
+complete -A function function
+
+# Help topics
+complete -A helptopic help
+
+# Jobspecs
+complete -A job disown fg jobs
+complete -A stopped bg
+
+# Readline bindings
+complete -A binding bind
+
+# Shell options
+complete -A shopt shopt
+
+# Signal names
+complete -A signal trap
+
+# Variables
+complete -A variable declare export readonly typeset
+
+# Both functions and variables
+complete -A function -A variable unset
+
+# If we have dynamic completion loading (Bash>=4.0), use it
+if ((BASH_VERSINFO[0] >= 4)) ; then
+ _completion_loader() {
+ [[ -n $1 ]] || return
+ compspec=$HOME/.bash_completion.d/$1.bash
+ [[ -f $compspec ]] || return
+ source "$compspec" >/dev/null 2>&1 && return 124
+ }
+ complete -D -F _completion_loader -o bashdefault -o default
+
+# If not, load all of the completions up now
+else
+ for sh in "$HOME"/.bash_completion.d/*.bash ; do
+ [[ -e $sh ]] && source "$sh"
+ done
+ unset -v sh
+fi