+# Walks over $words, sets $curtype to the string:
+#
+# 'option' if $cur is an option or flag like "-a" or "--abc".
+# 'optionval' if $cur is the value of an option
+# (which will be set in $option).
+# 'element' if $cur is a GStreamer element name.
+# '!' if $cur is '!'.
+# 'property' if $cur is the name of a property of a GStreamer element
+# (which will be set in $element).
+# 'propertyval' if $cur is the value of an element's property
+# (which will be set in $element and $property, respectively).
+#
+# ($cur is the word currently being completed.)
+#
+# Before calling this function make sure that $curtype, $option, $element and
+# $property are local, and that $cur, $cword and $words have been initialised.
+#
+# See test cases in tests/misc/test-gstreamer-completion.sh in the
+# gstreamer source repository.
+#
+_gst_launch_parse() {
+ local i next state
+ curtype= i=1 state=start
+ while [[ $i -le $cword ]]; do
+ next="${words[i]}"
+ # Note that COMP_WORDBREAKS by default includes "=" and ":".
+ case "$state,$next" in
+ start,-*=*) curtype=optionval option="${next%%=*}" state=start;;
+ start,-*) curtype=option option="$next" state=option;;
+ start,) curtype=option-or-element;;
+ start,*) curtype=element element="$next" state=element;;
+ option,=) curtype=optionval state=option=;;
+ option,*) _gst_takes_arg "$option" &&
+ curtype=optionval state=start ||
+ # re-evaluate without incrementing i:
+ { curtype= state=start; continue; }
+ ;;
+ option=,*) curtype=optionval state=start;;
+ element,\!) curtype='!' state='!';;
+ \!,*) curtype=element element="$next" state=element;;
+ element,*=)
+ curtype=propertyval property="${next%=}" state=property=;;
+ element,*=*)
+ curtype=propertyval property="${next%%=*}" state=element;;
+ element,*) curtype=property property="$next" state=property;;
+ property,=) curtype=propertyval state=property=;;
+ property=,*) curtype=propertyval state=element;;
+ esac
+ i=$((i + 1))
+ done
+ cur="${cur#*=}"
+}
+
+_gst_takes_arg() {
+ case "$1" in
+ -o|--output|--gst-debug-level|--gst-debug) true;;
+ --gst-plugin-path|--gst-plugin-load|--exclude) true;;
+ *) false;;
+ esac
+}
+
+_gst_parse_help() {
+ $1 --help-all 2>&1 | grep -Eo -e '--[a-z-]+'
+}
+
+_gst_init_completion() {
+ if type _get_comp_words_by_ref &>/dev/null; then
+ # Available since bash-completion 1.2
+ _get_comp_words_by_ref cur cword prev words
+ else
+ # bash-completion not installed or too old. Use bash's raw facilities.
+ # This won't complete properly if the cursor is in the middle of a
+ # word.
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
+ cword=$COMP_CWORD
+ words=("${COMP_WORDS[@]}")
+ fi
+}