# (C) 2010 magicant

# Completion script for the "renice" command.
# Supports POSIX 2008 and the traditional syntax.

function completion/renice {

	typeset OPTIONS ARGOPT PREFIX
	OPTIONS=( #>#
	"g; treat operands as process group IDs"
	"n:; specify the nice value increment"
	"p; treat operands as process IDs"
	"u; treat operands as user names"
	) #<#

	command -f completion//parseoptions -e
	case $ARGOPT in
	(-)
		command -f completion//completeoptions
		;;
	('')
		typeset type=pid arg
		for arg in "${WORDS[2,-1]}"; do
			case $arg in
				(-p|--pid)  type=pid ;;
				(-g|--pgrp) type=pgrp ;;
				(-u|--user) type=user ;;
			esac
		done
		case $type in
		(pid)
			# complete a process ID
			typeset pid args
			while read -r pid args; do
				if kill -n 0 $pid; then
					complete -D "$args" -- "$pid"
				fi
			done 2>/dev/null <(ps -A -o pid= -o args=)
			;;
		(pgrp)
			# complete a process group ID
			typeset pid args
			while read -r pid args; do
				if kill -n 0 -$pid; then
					complete -D "$args" -- "$pid"
				fi
			done 2>/dev/null <(ps -A -o pgid= -o args=)
			;;
		(user)
			# complete a user name
			complete -u
			;;
		esac
		;;
	esac

}


# vim: set ft=sh ts=8 sts=8 sw=8 noet:
