summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2011-11-26 23:28:08 +0200
committerVille Skyttä <ville.skytta@iki.fi>2011-11-26 23:32:30 +0200
commitafb2fdd010c34283314fc44ec3e7c8dbfea98a47 (patch)
treefa4bf34ee8d9d9e49a35125d86b949b197e43054
parent3a7f1d3de38aac9613a3647c3d1db367f34d2084 (diff)
downloadbash-completion-afb2fdd010c34283314fc44ec3e7c8dbfea98a47.tar.gz
export: Fix many use cases, add option completion.
-rw-r--r--completions/export60
-rw-r--r--test/completion/export.exp1
-rw-r--r--test/lib/completions/export.exp55
3 files changed, 104 insertions, 12 deletions
diff --git a/completions/export b/completions/export
index 557c52cb..42273a9e 100644
--- a/completions/export
+++ b/completions/export
@@ -3,23 +3,59 @@
_export()
{
local cur prev words cword
- _init_completion || return
+ _init_completion -n = || return
- case ${words[@]} in
- *=\$*)
- COMPREPLY=( $( compgen -v -P '$' -- "${cur#*=\$}" ) )
+ local i action=variable remove=false
+ for (( i=1; i < cword; i++ )); do
+ case ${words[i]} in
+ -p)
+ return
+ ;;
+ -*f*)
+ action=function
+ ;;&
+ -*n*)
+ remove=true
+ ;;
+ -*)
+ continue
+ ;;
+ esac
+ break
+ done
+
+ [[ $cur == *=\$* ]] && { cur=${cur#*=}; _variables; } && return
+
+ case $cur in
+ *=)
+ local pval=$( quote "$( eval printf %s \"\$${cur%=}\" )" )
+ # Complete previous value if it's not empty.
+ if [[ $pval != \'\' ]]; then
+ COMPREPLY=( "$pval" )
+ else
+ cur=${cur#*=}
+ _filedir
+ fi
;;
- *[^=])
- COMPREPLY=( $( compgen -v -S '=' -- "$cur" ) )
+ *=*)
+ cur=${cur#*=}
+ _filedir
;;
- *=)
- COMPREPLY=( "$( eval echo -n \"$`echo ${cur%=}`\" |
- ( echo -n \'
- sed -e 's/'\''/'\''\\\'\'''\''/g'
- echo -n \' ) )" )
+ *)
+ if [[ $cword -eq 1 && $cur == -* ]]; then
+ COMPREPLY=( $( compgen -W \
+ '-p $( _parse_usage "$1" )' -- "$cur" ) )
+ return
+ fi
+ local suffix
+ if ! $remove; then
+ suffix+==
+ compopt -o nospace
+ fi
+ COMPREPLY=( $( compgen -A $action -S "$suffix" -- "$cur" ) )
;;
esac
} &&
-complete -F _export -o default -o nospace export
+complete -F _export export
# ex: ts=4 sw=4 et filetype=sh
diff --git a/test/completion/export.exp b/test/completion/export.exp
new file mode 100644
index 00000000..bab517a9
--- /dev/null
+++ b/test/completion/export.exp
@@ -0,0 +1 @@
+assert_source_completions export
diff --git a/test/lib/completions/export.exp b/test/lib/completions/export.exp
new file mode 100644
index 00000000..84cb9170
--- /dev/null
+++ b/test/lib/completions/export.exp
@@ -0,0 +1,55 @@
+proc setup {} {
+ save_env
+}
+
+
+proc teardown {} {
+ assert_env_unmodified {
+ /OLDPWD=/d
+ }
+}
+
+
+setup
+
+
+assert_complete_any "export BASH"
+sync_after_int
+
+
+assert_complete_any "export -n BASH"
+sync_after_int
+
+
+assert_no_complete "export -p "
+sync_after_int
+
+
+assert_complete_dir {bar "bar bar.d/" foo foo.d/} "export FOO=" \
+ fixtures/shared/default
+sync_after_int
+
+
+assert_complete_dir {foo foo.d/} "export FOO=f" fixtures/shared/default "" \
+ -expect-cmd-minus f
+sync_after_int
+
+
+# Functions: _export, _expand, ...
+assert_complete_any "export -fn _ex"
+sync_after_int
+
+
+assert_complete_any "export -f -n _ex"
+sync_after_int
+
+
+assert_complete_any "export FOO=\$BASH"
+sync_after_int
+
+
+assert_complete_any "export -"
+sync_after_int
+
+
+teardown