My personal dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_ack 11KB

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