_ack 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #compdef ack ack2 ack-grep ack-standalone
  2. # ------------------------------------------------------------------------------
  3. # Description
  4. # -----------
  5. #
  6. # Completion script for ack 1.96 and 2.14 (http://betterthangrep.com).
  7. #
  8. # ------------------------------------------------------------------------------
  9. # Authors
  10. # -------
  11. #
  12. # * Julien Nicoulaud <julien.nicoulaud@gmail.com> (version 1.94)
  13. # * Zhao Cai <caizhaoff@gmail.com> (version 2.04)
  14. #
  15. # ------------------------------------------------------------------------------
  16. _ack_version() {
  17. local version
  18. version=(${(f)"$(_call_program version $words[1] --version)"})
  19. version=${${(z)${version[1]}}[2]}
  20. echo $version
  21. }
  22. _ack() {
  23. local context curcontext="$curcontext" state line cmds update_policy ret=1
  24. integer NORMARG
  25. typeset -A opt_args
  26. # Don't complete if command doesn't exist
  27. [[ ${+commands[${words[1]}]} -eq 0 ]] && return 0
  28. zstyle -s ":completion:${curcontext}:" cache-policy update_policy
  29. [[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" cache-policy _ack_types_caching_policy
  30. unset _ack_raw_types
  31. if ( [[ ${+_ack_raw_types} -eq 0 ]] || _cache_invalid "ack-grep" ) && ! _retrieve_cache "ack-grep"; then
  32. _ack_raw_types=(${(S)${(S)${(f)${${"$(_call_program types $words[1] --help=types)"}#*--\[no\]}}%; first line matches \/*\/}#*no\]})
  33. [[ $#_ack_raw_types -gt 0 ]] && _store_cache "ack-grep" _ack_raw_types
  34. fi
  35. ack_20_options=(
  36. '--ackrc[specify an ackrc file to use]:files:_files'
  37. '(- 1 *)--bar[consult Admiral Ackbar]'
  38. '(--nobreak --break)'{--nobreak,--break}'[print a break between results from different files, default on]'
  39. '(- 1 *)--cathy[chocolate chocolate chocolate]'
  40. '(- 1 *)--create-ackrc[create custom ackrc files based on the default settings loaded by ackrc]'
  41. '(- 1 *)--dump[writes the list of options loaded and where they came from to standard output]'
  42. '(--files-from -x)--files-from=[read the list of files to search from FILE]:files:_files'
  43. '(--filter --nofilter)--filter[force ack to treat input as pipe]'
  44. '(--filter --nofilter)--nofilter[force ack to treat input as tty]'
  45. '(--noheading --heading)'{--noheading,--heading}'[print a filename heading above results, default on]'
  46. '(- 1 *)--help-types[display all known types]'
  47. '--ignore-ack-defaults[ignore default definitions included with ack]'
  48. '*--ignore-file=[ignore file]:ignore file filter: _describe "ignore file filter" ignore_filter_opts'
  49. '(-k --known-types)'{-k,--known-types}'[include only files of types that ack recognizes]'
  50. '--lines=[only print line(s) NUM of each file]:number'
  51. '--nopager[do not send output through a pager, overrides ackrc, ACK_PAGER & ACK_PAGER_COLOR]'
  52. '-s[suppress error messages about nonexistent or unreadable files]'
  53. '(- 1 *)--thpppt[bill the cat]'
  54. '*--type-del[remove all filters associated with TYPE]' \
  55. '(-x --files-from)-x[read the list of files to search from STDIN]'
  56. )
  57. ack_19_options=(
  58. '(-a --all -u --unrestricted)'{-a,--all}'[operate on all files, regardless of type (but still skip directories like blib, CVS, etc.)]'
  59. '-G+[only paths matching the given regex are included in the search]:regex'
  60. '--invert-file-match[print/search handle files that do not match -g/-G]'
  61. '--line=[only print given line of each file]:number' \
  62. '(-u --unrestricted -a --all)'{-u,--unrestricted}'[all files and directories (including blib/, core.*, ...) are searched, nothing is skipped]'
  63. )
  64. if (( $(_ack_version) > 2.0 )); then
  65. ack_version_options=(${ack_20_options})
  66. else
  67. ack_version_options=(${ack_19_options})
  68. fi
  69. _arguments -C -s -S -n \
  70. '(- 1 *)--version[display version and copyright information]' \
  71. '(- 1 *)--help[print a short help statement]' \
  72. '(- 1 *)--man[print the manual page]' \
  73. $ack_version_options \
  74. '(-A --after-context -C --context)'{-A+,--after-context=}'[print N lines of trailing context after matching lines]:number' \
  75. '(-B --before-context -C --context)'{-B+,--before-context=}'[print N lines of leading context before matching lines]:number' \
  76. '(-C --context -A --after-context -B --before-context)'{-C-,--context=}'[print N lines (default 2) of context around matching lines]:number' \
  77. '(-c --count)'{-c,--count}'[suppress normal output; instead print a count of matching lines for each input file]' \
  78. '(--nocolor)--color[highlight the matching text]' \
  79. '(--color --color-filename --color-match --color-lineno)--nocolor[suppress the color]' \
  80. '(--nocolor --color)--color-filename[sets the color to be used for filenames]:color:->colors' \
  81. '(--nocolor --color)--color-match[sets the color to be used for matches]:color:->colors' \
  82. '(--nocolor --color)--color-lineno[sets the color to be used for line numbers]:color:->colors' \
  83. '--column[show the column number of the first match]' \
  84. '(--noenv)--env[enable environment processing]' \
  85. '(--env)--noenv[disable all environment processing, no .ackrc is read and all environment variables are ignored]' \
  86. '--flush[flush output immediately]' \
  87. '-f[only print the files that would be searched, without actually doing any searching]' \
  88. '(--nofollow)--follow[follow symlinks]' \
  89. '(--follow)--nofollow[don'\''t follow symlinks]' \
  90. '-g+[print files where the relative path + filename matches the given regex]:regex' \
  91. '(--nogroup)--group[group matches by file name]' \
  92. '(--group)--nogroup[do not group matches by file name]' \
  93. '(-H --with-filename -h --no-filename)'{-H,--with-filename}'[print the filename for each match]' \
  94. '(-h --no-filename -H --with-filename)'{-h,--no-filename}'[suppress the prefixing of filenames on output when multiple files are searched]' \
  95. '(-i --ignore-case)'{-i,--ignore-case}'[ignore case in the search strings]' \
  96. '*--ignore-dir=[ignore directory]:directory:_files' \
  97. '*--noignore-dir=[ignore directory]:directory:_files' \
  98. '(-l --files-with-matches -L --files-without-matches)'{-l,--files-with-matches}'[only print the filenames of matching files, instead of the matching text]' \
  99. '(-L --files-without-matches -l --files-with-matches)'{-L,--files-without-matches}'[only print the filenames of files that do NOT match]' \
  100. '--match=[specify the regular expression explicitly]:regex' \
  101. '(-m --max-count)'{-m+,--max-count=}'[stop reading a file after N matches]:number' \
  102. '(-r -R --recurse -n --no-recurse)'{-r,-R,--recurse}'[recurse into sub-directories]' \
  103. '(-n --no-recurse -r -R --recurse)'{-n,--no-recurse}'[no descending into subdirectories]' \
  104. '-o[show only the part of each line matching PATTERN (turns off text highlighting)]:pattern' \
  105. '--output=[output the evaluation of expr for each line (turns off text highlighting)]:expression' \
  106. '--pager=[direct ack'\''s output through program]:pager program:_command_names' \
  107. '--passthru[prints all lines, whether or not they match the expression]' \
  108. '--print0[the filenames are output separated with a null byte instead of the usual newline]' \
  109. '(-Q --literal)'{-Q,--literal}'[quote all metacharacters in the pattern, it is treated as a literal]' \
  110. '(--no-smart-case)--smart-case[ignore case in the search strings if pattern contains no uppercase characters]' \
  111. '(--smart-case)--no-smart-case[disable --smart-case option]' \
  112. '--sort-files[sorts the found files lexically]' \
  113. '--show-types[outputs the filetypes that ack associates with each file]' \
  114. '--thpppt[display the all-important Bill The Cat logo]' \
  115. '*--type=[specify the types of files to include or exclude from a search]:type:->types' \
  116. '*--type-add[files with the given extensions are recognized as being of the given type]:type-def:->type-defs' \
  117. '*--type-set[files with the given extensions are recognized as being of the given type]:type-def:->type-defs' \
  118. '(-v --invert-match)'{-v,--invert-match}'[invert match: select non-matching lines]' \
  119. '(-w --word-regexp)'{-w,--word-regexp}'[force the given pattern to match only whole words]' \
  120. '-1[stops after reporting first match of any kind]' \
  121. {'--','--no'}${_ack_raw_types/ ##/\[}']' \
  122. '*: :->args' \
  123. && ret=0
  124. case $state in
  125. args)
  126. if [[ CURRENT -eq NORMARG && ${+opt_args[--match]} -eq 0 ]]; then
  127. # If the current argument is the first non-option argument
  128. # and --match isn't present then a pattern is expected
  129. _message -e patterns 'pattern' && ret=0
  130. else
  131. _files
  132. fi
  133. ;;
  134. colors)
  135. local colors; colors=(
  136. 'black' 'on_black'
  137. 'red' 'on_red'
  138. 'green' 'on_green'
  139. 'yellow' 'on_yellow'
  140. 'blue' 'on_blue'
  141. 'magenta' 'on_magenta'
  142. 'cyan' 'on_cyan'
  143. 'white' 'on_white'
  144. 'clear'
  145. 'reset'
  146. 'dark'
  147. 'bold'
  148. 'underline'
  149. 'underscore'
  150. 'blink'
  151. 'reverse'
  152. 'concealed'
  153. )
  154. _describe -t 'colors' 'color' colors && ret=0
  155. ;;
  156. type-defs)
  157. if compset -P '*='; then
  158. local extensions; extensions=(*.*(:e))
  159. _values -s ',' 'file extension' '.'$extensions && ret=0
  160. else
  161. _message -e type-name 'type name' && ret=0
  162. fi
  163. ;;
  164. types)
  165. local types; types=({'','no'}${_ack_raw_types/ ##/:})
  166. _describe -t 'types' 'type' types
  167. ;;
  168. esac
  169. return ret
  170. }
  171. ignore_filter_opts=(
  172. 'is\::FILENAME'
  173. 'ext\::[EXTENSION,EXTENSION2,...]'
  174. 'match\::PATTERN'
  175. 'firstlinematch\::PATTERN'
  176. )
  177. _ack_types_caching_policy() {
  178. # Rebuild if ackrc more recent than cache.
  179. [[ -f ${ACKRC:-$HOME/.ackrc} && ${ACKRC:-$HOME/.ackrc} -nt "$1" ]] && return 0
  180. # Rebuild if cache is older than one week.
  181. local -a oldp
  182. oldp=( "$1"(Nmw+1) )
  183. (( $#oldp )) && return 0
  184. return 1
  185. }
  186. _ack "$@"
  187. # Local Variables:
  188. # mode: Shell-Script
  189. # sh-indentation: 2
  190. # indent-tabs-mode: nil
  191. # sh-basic-offset: 2
  192. # End:
  193. # vim: ft=zsh sw=2 ts=2 et