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

complete -C "$1" -F comp/kill

function comp/kill {
	typeset signame=false pid args

	shift
	if [ $# -eq 1 ]; then
		case "$1" in
		-|--)
			complete \
				-D "specify a signal to send" \
				-- -s
			complete \
				-D "print signal names" \
				-- -l
			complete \
				-D "print signal names and description" \
				-- -v
			complete -- --help
			return;;
		-[lv]*)
			return;;  # we can complete nothing!
		-[ns]*)
			complete -W "${1#-[ns]}"
			signame=true;;
		-*)
			complete -W "${1#-}"
			signame=true;;
		esac
	elif [ $# -eq 2 ]; then
		case "$1" in
			-[ns])
				signame=true;
		esac
	fi

	if $signame; then
		# complete a signal name
		complete --signal
	else
		case "${@[-1]}" in
		-*)
			# complete a process group ID
			while read pid args; do
				if kill -n 0 -$pid; then
					complete -D "$args" -- "-$pid"
				fi
			done 2>/dev/null <(ps -A -o pgid= -o args=);;
		*)
			# complete a process ID
			while read pid args; do
				if kill -n 0 $pid; then
					complete -D "$args" -- "$pid"
				fi
			done 2>/dev/null <(ps -A -o pid= -o args=);;
		esac
	fi
}
