bash-completion.sh
author František Kučera <franta-hg@frantovo.cz>
Wed, 30 Dec 2020 17:09:15 +0100
branchv_0
changeset 7 7f2d09c3b1de
parent 4 c58c7f2d4785
permissions -rw-r--r--
add --xml-attribute support (both input and output attributes)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
# Relational pipes
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# This program is free software: you can redistribute it and/or modify
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
# it under the terms of the GNU General Public License as published by
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# the Free Software Foundation, version 3 of the License.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
#
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# This program is distributed in the hope that it will be useful,
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# GNU General Public License for more details.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
#
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# You should have received a copy of the GNU General Public License
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
_relpipe_tr_xpath_completion() {
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
	local w0 w1 w2 w3
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
	COMPREPLY=()
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
	w0=${COMP_WORDS[COMP_CWORD]}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
	w1=${COMP_WORDS[COMP_CWORD-1]}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
	w2=${COMP_WORDS[COMP_CWORD-2]}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
	w3=${COMP_WORDS[COMP_CWORD-3]}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
	DATA_TYPE=(
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
		"string"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
		"integer"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
		"boolean"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
	)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
	INPUT_ATTRIBUTE_POLICY=(
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
		"append"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
		"prepend"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		"auto"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	# FIXME: user must type " and then press TAB otherwise the completion is broken due to the : colon
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	#
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	# can be fixed by global modification of environment variable:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	#	COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	# but it will affect other completions (where : is a separator)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	#
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	# these functions should help:
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	#	_get_comp_words_by_ref -n : cur
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	#	__ltrim_colon_completions "$cur"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	# but was not working (despite w0 renamed to cur)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	XMLNS=(
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		"tag:globalcode.info,2018:relpipe"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		"http://www.w3.org/1999/xhtml"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
		"http://www.w3.org/2000/svg"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		"http://www.w3.org/2005/Atom"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
		"http://docbook.org/ns/docbook"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
4
c58c7f2d4785 add current relation name: ../relation-name
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    56
	if   [[ "$w1" == "--relation"                      && "x$w0" == "x" ]];    then COMPREPLY=("'.*'")
0
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	elif [[ "$w1" == "--where"                         && "x$w0" == "x" ]];    then COMPREPLY=("''")
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	elif [[ "$w1" == "--xml-attribute"                 && "x$w0" == "x" ]];    then COMPREPLY=("''")
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	elif [[ "$w1" == "--output-attribute"              && "x$w0" == "x" ]];    then COMPREPLY=("''")
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	elif [[ "$w2" == "--output-attribute"                               ]];    then COMPREPLY=($(compgen -W "${DATA_TYPE[*]}" -- "$w0"))
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	elif [[ "$w3" == "--output-attribute"              && "x$w0" == "x" ]];    then COMPREPLY=("''")
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	elif [[ "$w1" == "--namespace"                     && "x$w0" == "x" ]];    then COMPREPLY=("''")
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	elif [[ "$w2" == "--namespace"                                      ]];    then COMPREPLY=($(compgen -W "${XMLNS[*]}" -- "$w0"))
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	elif [[ "$w1" == "--input-attributes"                               ]];    then COMPREPLY=($(compgen -W "${INPUT_ATTRIBUTE_POLICY[*]}" -- "$w0"))
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	else
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		OPTIONS=(
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			"--namespace"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			"--relation"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
			"--where"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
			"--output-attribute"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
			"--input-attributes"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
			"--xml-attribute"
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		)
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	fi
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
}
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
73e60c77be23 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
complete -F _relpipe_tr_xpath_completion relpipe-tr-xpath