# completion settings for the printf built-in
# written by magicant
# vim: set ft=sh ts=8 sts=8 sw=8 noet:

complete -C "$1" -X -F comp/printf

complete -C "$1" -O --help

function comp/printf {
	shift
	if [ $# -gt 1 ] && [ "$1" = "--" ]; then
		shift
	fi
	if [ $# -ne 1 ]; then
		complete -f
		return
	fi

	typeset word="$1"
	word=${word//\\\\/}
	case "$word" in (*\\)
		complete -W \\
		complete '\a' \
			-D "alert (bell)"
		complete '\b' \
			-D "backspace"
		complete '\f' \
			-D "form feed"
		complete '\n' \
			-D "newline"
		complete '\r' \
			-D "carriage return"
		complete '\t' \
			-D "tab"
		complete '\v' \
			-D "vertical tab"
		complete '\\' \
			-D "backslash"
		complete '\"' \
			-D "double-quote"
		complete '\'"'" \
			-D "single-quote"
		return
	esac
	word=${word//%%/}
	case "$word" in (*%)
		complete -W %
		complete '%d' \
			-D "signed decimal integer"
		complete '%i' \
			-D "signed decimal integer"
		complete '%u' \
			-D "unsigned decimal integer"
		complete '%o' \
			-D "unsigned octal integer"
		complete '%x' \
			-D "unsigned hexadecimal integer (lowercase)"
		complete '%X' \
			-D "unsigned hexadecimal integer (uppercase)"
		complete '%f' \
			-D "floating-point number (lowercase)"
		complete '%F' \
			-D "floating-point number (uppercase)"
		complete '%e' \
			-D "floating-point number with exponent (lowercase)"
		complete '%E' \
			-D "floating-point number with exponent (uppercase)"
		complete '%g' \
			-D "%f or %e (automatically selected)"
		complete '%G' \
			-D "%F or %E (automatically selected)"
		complete '%c' \
			-D "first character of a string"
		complete '%s' \
			-D "string"
		complete '%b' \
			-D "string (escape sequences allowed)"
		complete '%%' \
			-D "%"
	esac

	complete -f  # last resort
}
