2011-07-20 18:34:27 +00:00
#compdef ack ack-grep
# ------------------------------------------------------------------------------
2011-08-21 12:06:04 +00:00
# Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users
2011-07-20 18:34:27 +00:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the zsh-users nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
2011-07-21 22:35:27 +00:00
# Description
# -----------
2011-07-20 18:34:27 +00:00
#
2013-04-29 02:42:40 +00:00
# Completion script for ack 1.94 and 2.04 (http://betterthangrep.com).
2011-07-20 18:34:27 +00:00
#
# ------------------------------------------------------------------------------
2011-07-21 22:35:27 +00:00
# Authors
# -------
2011-07-20 18:34:27 +00:00
#
2013-04-29 02:42:40 +00:00
# * Julien Nicoulaud <julien.nicoulaud@gmail.com> (version 1.94)
# * Zhao Cai <caizhaoff@gmail.com> (version 2.04)
2011-07-20 18:34:27 +00:00
#
# ------------------------------------------------------------------------------
2013-04-29 02:42:40 +00:00
_ack_version() {
local version
version=(${(f)"$(_call_program version $words[1] --version)"})
2013-05-10 11:05:24 +00:00
version=${${(z)${version[1]}}[2]}
2013-04-29 02:42:40 +00:00
printf $version
}
2011-07-20 18:34:27 +00:00
_ack() {
2012-08-23 01:10:52 +00:00
local context curcontext="$curcontext" state line cmds update_policy ret=1
integer NORMARG
typeset -A opt_args
2011-07-31 17:32:00 +00:00
2011-08-05 09:04:25 +00:00
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
[[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _ack_types_caching_policy
unset _ack_raw_types
if ( [[ ${+_ack_raw_types} -eq 0 ]] || _cache_invalid "ack-grep" ) && ! _retrieve_cache "ack-grep"; then
2013-04-29 02:42:40 +00:00
_ack_raw_types=(${(S)${(S)${(f)${${"$(_call_program types $words[1] --help=types)"}#*--\[no\]}}%; first line matches \/*\/}#*no\]})
2011-08-05 09:04:25 +00:00
[[ $#_ack_raw_types -gt 0 ]] && _store_cache "ack-grep" _ack_raw_types
fi
2011-07-20 18:34:27 +00:00
2013-04-29 02:42:40 +00:00
ack_20_options=(
'(- 1 *)--dump[Writes the list of options loaded and where they came from to standard output]'
'(- 1 *)--create-ackrc[create custom ackrc files based on the default settings loaded by ackr]'
'(- 1 *)--bar[consult Admiral Ackbar]'
'*--ignore-file=[ignore file]:ignore file filter: _describe "ignore file filter" ignore_filter_opts'
'*--file-from=[Read the list of files to search from FILE]:file:_files'
'-s[Suppress error messages about nonexistent or unreadable files]'
'--ignore-ack-defaults[ignore ack default options in favor of their own]'
)
ack_19_options=( '(-a --all -u --unrestricted)'{-a,--all}'[operate on all files, regardless of type (but still skip directories like blib, CVS, etc.)]'
'(-u --unrestricted -a --all)'{-u,--unrestricted}'[all files and directories (including blib/, core.*, ...) are searched, nothing is skipped]'
'-G+[only paths matching the given regex are included in the search]:regex'
'--invert-file-match[print/search handle files that do not match -g/-G]'
)
if (( $(_ack_version) > 2.0 )); then
ack_version_options=(${ack_20_options})
else
ack_version_options=(${ack_19_options})
fi
2012-08-23 01:10:52 +00:00
_arguments -C -s -S -n \
2011-08-05 09:04:25 +00:00
'(- 1 *)--version[display version and copyright information]' \
'(- 1 *)--help[print a short help statement]' \
'(- 1 *)--man[print the manual page]' \
2013-04-29 02:42:40 +00:00
$ack_version_options \
2012-08-23 01:27:28 +00:00
'(-A --after-context -C --context)'{-A+,--after-context=}'[print N lines of trailing context after matching lines]:number' \
'(-B --before-context -C --context)'{-B+,--before-context=}'[print N lines of leading context before matching lines]:number' \
'(-C --context -A --after-context -B --before-context)'{-C-,--context=}'[print N lines (default 2) of context around matching lines]:number' \
2011-08-05 09:04:25 +00:00
'(-c --count)'{-c,--count}'[suppress normal output; instead print a count of matching lines for each input file]' \
'(--nocolor)--color[highlight the matching text]' \
'(--color --color-filename --color-match --color-lineno)--nocolor[supress the color]' \
'(--nocolor --color)--color-filename[sets the color to be used for filenames]:color:->colors' \
'(--nocolor --color)--color-match[sets the color to be used for matches]:color:->colors' \
'(--nocolor --color)--color-lineno[sets the color to be used for line numbers]:color:->colors' \
'--column[show the column number of the first match]' \
'(--noenv)--env[enable environment processing]' \
'(--env)--noenv[disable all environment processing, no .ackrc is read and all environment variables are ignored]' \
'--flush[flush output immediately]' \
'-f[only print the files that would be searched, without actually doing any searching]' \
'(--nofollow)--follow[follow symlinks]' \
'(--follow)--nofollow[don'\''t follow symlinks]' \
2012-08-23 01:27:28 +00:00
'-g+[print files where the relative path + filename matches the given regex]:regex' \
2011-08-05 09:04:25 +00:00
'(--nogroup)--group[group matches by file name]' \
'(--group)--nogroup[do not group matches by file name]' \
'(-H --with-filename -h --no-filename)'{-H,--with-filename}'[print the filename for each match]' \
'(-h --no-filename -H --with-filename)'{-h,--no-filename}'[suppress the prefixing of filenames on output when multiple files are searched]' \
'(-i --ignore-case)'{-i,--ignore-case}'[ignore case in the search strings]' \
2013-04-29 02:42:40 +00:00
'*--ignore-dir=[ignore directory]:directory:_files' \
'*--noignore-dir=[ignore directory]:directory:_files' \
2012-08-23 01:27:28 +00:00
'--line=[only print given line of each file]:number' \
2011-08-05 09:04:25 +00:00
'(-l --files-with-matches -L --files-without-matches)'{-l,--files-with-matches}'[only print the filenames of matching files, instead of the matching text]' \
'(-L --files-without-matches -l --files-with-matches)'{-L,--files-without-matches}'[only print the filenames of files that do NOT match]' \
2012-08-23 01:27:28 +00:00
'--match=[specify the regular expression explicitly]:regex' \
'(-m --max-count)'{-m+,--max-count=}'[stop reading a file after N matches]:number' \
2011-08-05 09:04:25 +00:00
'(-r -R --recurse -n --no-recurse)'{-r,-R,--recurse}'[recurse into sub-directories]' \
'(-n --no-recurse -r -R --recurse)'{-n,--no-recurse}'[no descending into subdirectories]' \
'-o[show only the part of each line matching PATTERN (turns off text highlighting)]:pattern' \
2012-08-23 01:27:28 +00:00
'--output=[output the evaluation of expr for each line (turns off text highlighting)]:expression' \
'--pager=[direct ack'\''s output through program]:pager program:_command_names' \
2011-08-05 09:04:25 +00:00
'--passthru[prints all lines, whether or not they match the expression]' \
'--print0[the filenames are output separated with a null byte instead of the usual newline]' \
'(-Q --literal)'{-Q,--literal}'[quote all metacharacters in the pattern, it is treated as a literal]' \
'(--no-smart-case)--smart-case[ignore case in the search strings if pattern contains no uppercase characters]' \
'(--smart-case)--no-smart-case[disable --smart-case option]' \
'--sort-files[sorts the found files lexically]' \
'--show-types[outputs the filetypes that ack associates with each file]' \
'--thpppt[display the all-important Bill The Cat logo]' \
2012-08-23 01:27:28 +00:00
'*--type=[specify the types of files to include or exclude from a search]:type:->types' \
2011-08-05 09:04:25 +00:00
'*--type-add[files with the given extensions are recognized as being of the given type]:type-def:->type-defs' \
'*--type-set[files with the given extensions are recognized as being of the given type]:type-def:->type-defs' \
'(-v --invert-match)'{-v,--invert-match}'[invert match: select non-matching lines]' \
'(-w --word-regexp)'{-w,--word-regexp}'[force the given pattern to match only whole words]' \
'-1[stops after reporting first match of any kind]' \
{'--','--no'}${_ack_raw_types/ ##/\[}']' \
2012-08-23 01:10:52 +00:00
'*: :->args' \
2011-07-20 18:34:27 +00:00
&& ret=0
case $state in
2012-08-23 01:10:52 +00:00
args)
if [[ CURRENT -eq NORMARG && ${+opt_args[--match]} -eq 0 ]]
then
# If the current argument is the first non-option argument
# and --match isn't present then a pattern is expected
_message -e patterns 'pattern' && ret=0
else
_files
fi
2011-08-05 09:04:25 +00:00
;;
colors)
local colors; colors=(
'black' 'on_black'
'red' 'on_red'
'green' 'on_green'
'yellow' 'on_yellow'
'blue' 'on_blue'
'magenta' 'on_magenta'
'cyan' 'on_cyan'
'white' 'on_white'
'clear'
'reset'
'dark'
'bold'
'underline'
'underscore'
'blink'
'reverse'
'concealed'
)
_describe -t 'colors' 'color' colors && ret=0
;;
type-defs)
if compset -P '*='; then
local extensions; extensions=(*.*(:e))
_values -s ',' 'file extension' '.'$extensions && ret=0
else
_message -e type-name 'type name' && ret=0
fi
;;
types)
local types; types=({'','no'}${_ack_raw_types/ ##/:})
_describe -t 'types' 'type' types
2011-07-20 18:34:27 +00:00
;;
esac
return ret
}
2013-04-29 02:42:40 +00:00
ignore_filter_opts=(
'is\::FILENAME'
'ext\::[EXTENSION,EXTENSION2,...]'
'match\::PATTERN'
'firstlinematch\::PATTERN'
)
2011-08-05 09:04:25 +00:00
_ack_types_caching_policy() {
2011-07-20 18:34:27 +00:00
2011-08-05 09:04:25 +00:00
# Rebuild if ackrc more recent than cache.
2013-02-21 02:45:42 +00:00
[[ -f ${ACKRC:-$HOME/.ackrc} && ${ACKRC:-$HOME/.ackrc} -nt "$1" ]] && return 0
2011-07-20 18:34:27 +00:00
2011-08-05 09:04:25 +00:00
# Rebuild if cache is older than one week.
local -a oldp
oldp=( "$1"(Nmw+1) )
(( $#oldp )) && return 0
2011-07-20 18:34:27 +00:00
2011-08-05 09:04:25 +00:00
return 1
2011-07-20 18:34:27 +00:00
}
_ack "$@"
2012-08-23 15:18:49 +00:00
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et
2013-04-29 02:42:40 +00:00