# (C) 2010 magicant

# Completion script for the "grep" command.
# Supports POSIX 2008, GNU grep 2.6.3, SunOS 5.10, HP-UX 11i v3.

function completion/grep {

	case $("${WORDS[1]}" --version 2>/dev/null) in
		(*'GNU grep'*) typeset type=GNU ;;
		(*)            typeset type="$(uname 2>/dev/null)" ;;
	esac
	case $type in
		(GNU) typeset long=true ;;
		(*)   typeset long= ;;
	esac

	typeset OPTIONS POSIXOPTIONS ADDOPTIONS ARGOPT PREFIX
	POSIXOPTIONS=( #>#
	"c ${long:+--count}; print only the count of selected lines"
	"E ${long:+--extended-regexp}; use extended regular expression"
	"e: ${long:+--regexp:}; specify a pattern to match"
	"F ${long:+--fixed-strings}; perform simple string matching rather than regular expression"
	"f: ${long:+--file:}; specify a file containing patterns to match"
	"i ${long:+--ignore-case}; case-insensitive matching"
	"l ${long:+--files-with-matches}; print filenames only"
	"n ${long:+--line-number}; print line numbers"
	"q ${long:+--quiet --silent}; don't print anything to the standard output"
	"s ${long:+--no-messages}; suppress error messages"
	"v ${long:+--invert-match}; select non-matching lines"
	"x ${long:+--line-regexp}; force the pattern to match whole lines only"
	) #<#

	ADDOPTIONS=()
	case $type in (GNU|SunOS|HP-UX)
		ADDOPTIONS=("$ADDOPTIONS" #>#
		"h ${long:+--no-filename}; never print filenames in results"
		"w ${long:+--word-regexp}; force the pattern to match whole words only"
		) #<#
	esac
	case $type in
	(GNU)
		ADDOPTIONS=("$ADDOPTIONS" #>#
		"A: --after-context:; pri                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   