# (C) 2013 magicant

# Completion script for the "git-grep" command.
# Supports Git 1.8.2.2.

function completion/git-grep {
	WORDS=(git grep "${WORDS[2,-1]}")
	command -f completion//reexecute
}

function completion/git::grep:arg {

	OPTIONS=( #>#
	"A: --after-context:; print specified number of lines after each line printed"
	"--all-match; require all patterns to match in each file"
	"--and; join two patterns"
	"a --text; treat binary files as test files"
	"B: --before-context:; print specified number of lines before each line printed"
	"--break; insert an empty line between matches from different files"
	"--cached; search files in the index"
	"--color::; specify when to print colored output"
	"C: --context:; print specified number of lines before and after each line printed"
	"c --count; print only the count of selected lines"
	"E --extended-regexp; use extended regular expression"
	"e:; specify a pattern to match"
	"--exclude-standard; don't search ignored files"
	"F --fixed-strings; perform simple string matching rather than regular expression"
	"f: --file:; specify a file containing patterns to match"
	"--full-name; print filenames relative to the working tree's root directory"
	"G --basic-regexp; use basic regular expression"
	"H; always print the filename for each line printed"
	"h; never print filenames in results"
	"--heading; print filenames in separate lines"
	"I; assume binary files don't match anything"
	"i --ignore-case; case-insensitive matching"
	"L --files-without-match; print only the names of files containing no selected lines"
	"l --files-with-matches --name-only; print filenames only"
	"--max-depth:; specify directory depth to limit search"
	"n --line-number; print line numbers"
	"--no-color; like --color=never"
	"--no-exclude-standard; search ignored files"
	"--no-index; search files outside a working tree"
	"--not; negate a pattern"
	"O:: --open-files-in-pager::; open matching files in a pager"
	"--or; join two patterns"
	"p --show-function; print the name of the function containing the match"
	"P --perl-regexp; use Perl's regular expression"
	"q --quiet; don't print anything to the standard output"
	"--untracked; search untracked files as well"
	"v --invert-match; select non-matching lines"
	"W --function-context; print the whole functions containing matches"
	"w --word-regexp; force the pattern to match whole words only"
	"z --null; print a null byte after each filename"
	) #<#

	command -f completion/git::grep:removeparen
	command -f completion//parseoptions -n
	case $ARGOPT in
		(-)
			command -f completion//completeoptions
			;;
		([ABC]|--*context|--max*)
			;;
		(--color)
			command -f completion/git::--color:arg
			;;
		(O|--open-files-in-pager)
			complete -P "$PREFIX" --external-command
			;;
		('')
			if command -f completion/git::grep:shouldcompleteuntracked; then
				complete -P "$PREFIX" -f
			fi
			command -f completion/git::completerefpath
			;;
		(*)
			complete -P "$PREFIX" -f
			;;
	esac

}

function completion/git::grep:removeparen {
	typeset i=2
	while [ "$i" -le "${WORDS[#]}" ]; do
		case ${WORDS[i]} in
			([\(\)])
				# remove this parenthesis
				WORDS=("${WORDS[1,i-1]}" "${WORDS[i+1,-1]}") ;;
			(--)
				break ;;
			(*)
				: $((i++)) ;;
		esac
	done
	: words = "$WORDS"
}

function completion/git::grep:shouldcompleteuntracked {
	typeset i=2
	while [ "$i" -le "${WORDS[#]}" ]; do
		case ${WORDS[i]} in
			(--no-index|--untracked)
				return 0;;
			(--)
				return 1;;
		esac
		: $((i++))
	done
	return 1
}


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