_ripgrep 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #compdef rg
  2. ##
  3. # zsh completion function for ripgrep
  4. #
  5. # Originally based on code from the zsh-users project — see copyright notice
  6. # below.
  7. _rg() {
  8. local curcontext=$curcontext no='!' descr ret=1
  9. local -a context line state state_descr args tmp suf
  10. local -A opt_args
  11. # ripgrep has many options which negate the effect of a more common one — for
  12. # example, `--no-column` to negate `--column`, and `--messages` to negate
  13. # `--no-messages`. There are so many of these, and they're so infrequently
  14. # used, that some users will probably find it irritating if they're completed
  15. # indiscriminately, so let's not do that unless either the current prefix
  16. # matches one of those negation options or the user has the `complete-all`
  17. # style set. Note that this prefix check has to be updated manually to account
  18. # for all of the potential negation options listed below!
  19. if
  20. # We also want to list all of these options during testing
  21. [[ $_RG_COMPLETE_LIST_ARGS == (1|t*|y*) ]] ||
  22. # (--[imnp]* => --ignore*, --messages, --no-*, --pcre2-unicode)
  23. [[ $PREFIX$SUFFIX == --[imnp]* ]] ||
  24. zstyle -t ":complete:$curcontext:*" complete-all
  25. then
  26. no=
  27. fi
  28. # We make heavy use of argument groups here to prevent the option specs from
  29. # growing unwieldy. These aren't supported in zsh <5.4, though, so we'll strip
  30. # them out below if necessary. This makes the exclusions inaccurate on those
  31. # older versions, but oh well — it's not that big a deal
  32. args=(
  33. + '(exclusive)' # Misc. fully exclusive options
  34. '(: * -)'{-h,--help}'[display help information]'
  35. '(: * -)'{-V,--version}'[display version information]'
  36. + '(buffered)' # buffering options
  37. '--line-buffered[force line buffering]'
  38. $no"--no-line-buffered[don't force line buffering]"
  39. '--block-buffered[force block buffering]'
  40. $no"--no-block-buffered[don't force block buffering]"
  41. + '(case)' # Case-sensitivity options
  42. {-i,--ignore-case}'[search case-insensitively]'
  43. {-s,--case-sensitive}'[search case-sensitively]'
  44. {-S,--smart-case}'[search case-insensitively if pattern is all lowercase]'
  45. + '(context-a)' # Context (after) options
  46. '(context-c)'{-A+,--after-context=}'[specify lines to show after each match]:number of lines'
  47. + '(context-b)' # Context (before) options
  48. '(context-c)'{-B+,--before-context=}'[specify lines to show before each match]:number of lines'
  49. + '(context-c)' # Context (combined) options
  50. '(context-a context-b)'{-C+,--context=}'[specify lines to show before and after each match]:number of lines'
  51. + '(column)' # Column options
  52. '--column[show column numbers for matches]'
  53. $no"--no-column[don't show column numbers for matches]"
  54. + '(count)' # Counting options
  55. {-c,--count}'[only show count of matching lines for each file]'
  56. '--count-matches[only show count of individual matches for each file]'
  57. + '(encoding)' # Encoding options
  58. {-E+,--encoding=}'[specify text encoding of files to search]: :_rg_encodings'
  59. $no'--no-encoding[use default text encoding]'
  60. + file # File-input options
  61. '(1)*'{-f+,--file=}'[specify file containing patterns to search for]: :_files'
  62. + '(file-match)' # Files with/without match options
  63. '(stats)'{-l,--files-with-matches}'[only show names of files with matches]'
  64. '(stats)--files-without-match[only show names of files without matches]'
  65. + '(file-name)' # File-name options
  66. {-H,--with-filename}'[show file name for matches]'
  67. "--no-filename[don't show file name for matches]"
  68. + '(file-system)' # File system options
  69. "--one-file-system[don't descend into directories on other file systems]"
  70. $no'--no-one-file-system[descend into directories on other file systems]'
  71. + '(fixed)' # Fixed-string options
  72. {-F,--fixed-strings}'[treat pattern as literal string instead of regular expression]'
  73. $no"--no-fixed-strings[don't treat pattern as literal string]"
  74. + '(follow)' # Symlink-following options
  75. {-L,--follow}'[follow symlinks]'
  76. $no"--no-follow[don't follow symlinks]"
  77. + glob # File-glob options
  78. '*'{-g+,--glob=}'[include/exclude files matching specified glob]:glob'
  79. '*--iglob=[include/exclude files matching specified case-insensitive glob]:glob'
  80. + '(heading)' # Heading options
  81. '(pretty-vimgrep)--heading[show matches grouped by file name]'
  82. "(pretty-vimgrep)--no-heading[don't show matches grouped by file name]"
  83. + '(hidden)' # Hidden-file options
  84. '--hidden[search hidden files and directories]'
  85. $no"--no-hidden[don't search hidden files and directories]"
  86. + '(ignore)' # Ignore-file options
  87. "(--no-ignore-global --no-ignore-parent --no-ignore-vcs)--no-ignore[don't respect ignore files]"
  88. $no'(--ignore-global --ignore-parent --ignore-vcs)--ignore[respect ignore files]'
  89. + '(ignore-global)' # Global ignore-file options
  90. "--no-ignore-global[don't respect global ignore files]"
  91. $no'--ignore-global[respect global ignore files]'
  92. + '(ignore-parent)' # Parent ignore-file options
  93. "--no-ignore-parent[don't respect ignore files in parent directories]"
  94. $no'--ignore-parent[respect ignore files in parent directories]'
  95. + '(ignore-vcs)' # VCS ignore-file options
  96. "--no-ignore-vcs[don't respect version control ignore files]"
  97. $no'--ignore-vcs[respect version control ignore files]'
  98. + '(json)' # JSON options
  99. '--json[output results in JSON Lines format]'
  100. $no"--no-json[don't output results in JSON Lines format]"
  101. + '(line-number)' # Line-number options
  102. {-n,--line-number}'[show line numbers for matches]'
  103. {-N,--no-line-number}"[don't show line numbers for matches]"
  104. + '(line-terminator)' # Line-terminator options
  105. '--crlf[use CRLF as line terminator]'
  106. $no"--no-crlf[don't use CRLF as line terminator]"
  107. '(text)--null-data[use NUL as line terminator]'
  108. + '(max-depth)' # Directory-depth options
  109. '--max-depth=[specify max number of directories to descend]:number of directories'
  110. '!--maxdepth=:number of directories'
  111. + '(messages)' # Error-message options
  112. '(--no-ignore-messages)--no-messages[suppress some error messages]'
  113. $no"--messages[don't suppress error messages affected by --no-messages]"
  114. + '(messages-ignore)' # Ignore-error message options
  115. "--no-ignore-messages[don't show ignore-file parse error messages]"
  116. $no'--ignore-messages[show ignore-file parse error messages]'
  117. + '(mmap)' # mmap options
  118. '--mmap[search using memory maps when possible]'
  119. "--no-mmap[don't search using memory maps]"
  120. + '(multiline)' # Multiline options
  121. {-U,--multiline}'[permit matching across multiple lines]'
  122. $no'(multiline-dotall)--no-multiline[restrict matches to at most one line each]'
  123. + '(multiline-dotall)' # Multiline DOTALL options
  124. '(--no-multiline)--multiline-dotall[allow "." to match newline (with -U)]'
  125. $no"(--no-multiline)--no-multiline-dotall[don't allow \".\" to match newline (with -U)]"
  126. + '(only)' # Only-match options
  127. {-o,--only-matching}'[show only matching part of each line]'
  128. + '(passthru)' # Pass-through options
  129. '(--vimgrep)--passthru[show both matching and non-matching lines]'
  130. '!(--vimgrep)--passthrough'
  131. + '(pcre2)' # PCRE2 options
  132. {-P,--pcre2}'[enable matching with PCRE2]'
  133. $no'(pcre2-unicode)--no-pcre2[disable matching with PCRE2]'
  134. + '(pcre2-unicode)' # PCRE2 Unicode options
  135. $no'(--no-pcre2 --no-pcre2-unicode)--pcre2-unicode[enable PCRE2 Unicode mode (with -P)]'
  136. '(--no-pcre2 --pcre2-unicode)--no-pcre2-unicode[disable PCRE2 Unicode mode (with -P)]'
  137. + '(pre)' # Preprocessing options
  138. '(-z --search-zip)--pre=[specify preprocessor utility]:preprocessor utility:_command_names -e'
  139. $no'--no-pre[disable preprocessor utility]'
  140. + pre-glob # Preprocessing glob options
  141. '*--pre-glob[include/exclude files for preprocessing with --pre]'
  142. + '(pretty-vimgrep)' # Pretty/vimgrep display options
  143. '(heading)'{-p,--pretty}'[alias for --color=always --heading -n]'
  144. '(heading passthru)--vimgrep[show results in vim-compatible format]'
  145. + regexp # Explicit pattern options
  146. '(1 file)*'{-e+,--regexp=}'[specify pattern]:pattern'
  147. + '(replace)' # Replacement options
  148. {-r+,--replace=}'[specify string used to replace matches]:replace string'
  149. + '(sort)' # File-sorting options
  150. '(threads)--sort=[sort results in ascending order (disables parallelism)]:sort method:((
  151. none\:"no sorting"
  152. path\:"sort by file path"
  153. modified\:"sort by last modified time"
  154. accessed\:"sort by last accessed time"
  155. created\:"sort by creation time"
  156. ))'
  157. '(threads)--sortr=[sort results in descending order (disables parallelism)]:sort method:((
  158. none\:"no sorting"
  159. path\:"sort by file path"
  160. modified\:"sort by last modified time"
  161. accessed\:"sort by last accessed time"
  162. created\:"sort by creation time"
  163. ))'
  164. '!(threads)--sort-files[sort results by file path (disables parallelism)]'
  165. + '(stats)' # Statistics options
  166. '(--files file-match)--stats[show search statistics]'
  167. $no"--no-stats[don't show search statistics]"
  168. + '(text)' # Binary-search options
  169. {-a,--text}'[search binary files as if they were text]'
  170. $no"(--null-data)--no-text[don't search binary files as if they were text]"
  171. + '(threads)' # Thread-count options
  172. '(sort)'{-j+,--threads=}'[specify approximate number of threads to use]:number of threads'
  173. + '(trim)' # Trim options
  174. '--trim[trim any ASCII whitespace prefix from each line]'
  175. $no"--no-trim[don't trim ASCII whitespace prefix from each line]"
  176. + type # Type options
  177. '*'{-t+,--type=}'[only search files matching specified type]: :_rg_types'
  178. '*--type-add=[add new glob for specified file type]: :->typespec'
  179. '*--type-clear=[clear globs previously defined for specified file type]: :_rg_types'
  180. # This should actually be exclusive with everything but other type options
  181. '(: *)--type-list[show all supported file types and their associated globs]'
  182. '*'{-T+,--type-not=}"[don't search files matching specified file type]: :_rg_types"
  183. + '(word-line)' # Whole-word/line match options
  184. {-w,--word-regexp}'[only show matches surrounded by word boundaries]'
  185. {-x,--line-regexp}'[only show matches surrounded by line boundaries]'
  186. + '(zip)' # Compression options
  187. '(--pre)'{-z,--search-zip}'[search in compressed files]'
  188. $no"--no-search-zip[don't search in compressed files]"
  189. + misc # Other options — no need to separate these at the moment
  190. '(-b --byte-offset)'{-b,--byte-offset}'[show 0-based byte offset for each matching line]'
  191. '--color=[specify when to use colors in output]:when:((
  192. never\:"never use colors"
  193. auto\:"use colors or not based on stdout, TERM, etc."
  194. always\:"always use colors"
  195. ansi\:"always use ANSI colors (even on Windows)"
  196. ))'
  197. '*--colors=[specify color and style settings]: :->colorspec'
  198. '--context-separator=[specify string used to separate non-continuous context lines in output]:separator'
  199. '--debug[show debug messages]'
  200. '--dfa-size-limit=[specify upper size limit of generated DFA]:DFA size (bytes)'
  201. "(1 stats)--files[show each file that would be searched (but don't search)]"
  202. '*--ignore-file=[specify additional ignore file]:ignore file:_files'
  203. '(-v --invert-match)'{-v,--invert-match}'[invert matching]'
  204. '(-M --max-columns)'{-M+,--max-columns=}'[specify max length of lines to print]:number of bytes'
  205. '(-m --max-count)'{-m+,--max-count=}'[specify max number of matches per file]:number of matches'
  206. '--max-filesize=[specify size above which files should be ignored]:file size (bytes)'
  207. "--no-config[don't load configuration files]"
  208. '(-0 --null)'{-0,--null}'[print NUL byte after file names]'
  209. '--path-separator=[specify path separator to use when printing file names]:separator'
  210. '(-q --quiet)'{-q,--quiet}'[suppress normal output]'
  211. '--regex-size-limit=[specify upper size limit of compiled regex]:regex size (bytes)'
  212. '*'{-u,--unrestricted}'[reduce level of "smart" searching]'
  213. + operand # Operands
  214. '(--files --type-list file regexp)1: :_guard "^-*" pattern'
  215. '(--type-list)*: :_files'
  216. )
  217. # This is used with test_complete.sh to verify that there are no options
  218. # listed in the help output that aren't also defined here
  219. [[ $_RG_COMPLETE_LIST_ARGS == (1|t*|y*) ]] && {
  220. print -rl - $args
  221. return 0
  222. }
  223. # Strip out argument groups where unsupported (see above)
  224. [[ $ZSH_VERSION == (4|5.<0-3>)(.*)# ]] &&
  225. args=( ${(@)args:#(#i)(+|[a-z0-9][a-z0-9_-]#|\([a-z0-9][a-z0-9_-]#\))} )
  226. _arguments -C -s -S : $args && ret=0
  227. case $state in
  228. colorspec)
  229. if [[ ${IPREFIX#--*=}$PREFIX == [^:]# ]]; then
  230. suf=( -qS: )
  231. tmp=(
  232. 'column:specify coloring for column numbers'
  233. 'line:specify coloring for line numbers'
  234. 'match:specify coloring for match text'
  235. 'path:specify coloring for file names'
  236. )
  237. descr='color/style type'
  238. elif [[ ${IPREFIX#--*=}$PREFIX == (column|line|match|path):[^:]# ]]; then
  239. suf=( -qS: )
  240. tmp=(
  241. 'none:clear color/style for type'
  242. 'bg:specify background color'
  243. 'fg:specify foreground color'
  244. 'style:specify text style'
  245. )
  246. descr='color/style attribute'
  247. elif [[ ${IPREFIX#--*=}$PREFIX == [^:]##:(bg|fg):[^:]# ]]; then
  248. tmp=( black blue green red cyan magenta yellow white )
  249. descr='color name or r,g,b'
  250. elif [[ ${IPREFIX#--*=}$PREFIX == [^:]##:style:[^:]# ]]; then
  251. tmp=( {,no}bold {,no}intense {,no}underline )
  252. descr='style name'
  253. else
  254. _message -e colorspec 'no more arguments'
  255. fi
  256. (( $#tmp )) && {
  257. compset -P '*:'
  258. _describe -t colorspec $descr tmp $suf && ret=0
  259. }
  260. ;;
  261. typespec)
  262. if compset -P '[^:]##:include:'; then
  263. _sequence -s , _rg_types && ret=0
  264. # @todo This bit in particular could be better, but it's a little
  265. # complex, and attempting to solve it seems to run us up against a crash
  266. # bug — zsh # 40362
  267. elif compset -P '[^:]##:'; then
  268. _message 'glob or include directive' && ret=1
  269. elif [[ ! -prefix *:* ]]; then
  270. _rg_types -qS : && ret=0
  271. fi
  272. ;;
  273. esac
  274. return ret
  275. }
  276. # Complete encodings
  277. _rg_encodings() {
  278. local -a expl
  279. local -aU _encodings
  280. # This is impossible to read, but these encodings rarely if ever change, so it
  281. # probably doesn't matter. They are derived from the list given here:
  282. # https://encoding.spec.whatwg.org/#concept-encoding-get
  283. _encodings=(
  284. {{,us-}ascii,arabic,chinese,cyrillic,greek{,8},hebrew,korean}
  285. logical visual mac {,cs}macintosh x-mac-{cyrillic,roman,ukrainian}
  286. 866 ibm{819,866} csibm866
  287. big5{,-hkscs} {cn-,cs}big5 x-x-big5
  288. cp{819,866,125{0..8}} x-cp125{0..8}
  289. csiso2022{jp,kr} csiso8859{6,8}{e,i}
  290. csisolatin{{1..6},9} csisolatin{arabic,cyrillic,greek,hebrew}
  291. ecma-{114,118} asmo-708 elot_928 sun_eu_greek
  292. euc-{jp,kr} x-euc-jp cseuckr cseucpkdfmtjapanese
  293. {,x-}gbk csiso58gb231280 gb18030 {,cs}gb2312 gb_2312{,-80} hz-gb-2312
  294. iso-2022-{cn,cn-ext,jp,kr}
  295. iso8859{,-}{{1..11},13,14,15}
  296. iso-8859-{{1..11},{6,8}-{e,i},13,14,15,16} iso_8859-{{1..9},15}
  297. iso_8859-{1,2,6,7}:1987 iso_8859-{3,4,5,8}:1988 iso_8859-9:1989
  298. iso-ir-{58,100,101,109,110,126,127,138,144,148,149,157}
  299. koi{,8,8-r,8-ru,8-u,8_r} cskoi8r
  300. ks_c_5601-{1987,1989} ksc{,_}5691 csksc56011987
  301. latin{1..6} l{{1..6},9}
  302. shift{-,_}jis csshiftjis {,x-}sjis ms_kanji ms932
  303. utf{,-}8 utf-16{,be,le} unicode-1-1-utf-8
  304. windows-{31j,874,949,125{0..8}} dos-874 tis-620 ansi_x3.4-1968
  305. x-user-defined auto
  306. )
  307. _wanted encodings expl encoding compadd -a "$@" - _encodings
  308. }
  309. # Complete file types
  310. _rg_types() {
  311. local -a expl
  312. local -aU _types
  313. _types=( ${(@)${(f)"$( _call_program types rg --type-list )"}%%:*} )
  314. _wanted types expl 'file type' compadd -a "$@" - _types
  315. }
  316. _rg "$@"
  317. # ------------------------------------------------------------------------------
  318. # Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users
  319. # All rights reserved.
  320. #
  321. # Redistribution and use in source and binary forms, with or without
  322. # modification, are permitted provided that the following conditions are met:
  323. # * Redistributions of source code must retain the above copyright
  324. # notice, this list of conditions and the following disclaimer.
  325. # * Redistributions in binary form must reproduce the above copyright
  326. # notice, this list of conditions and the following disclaimer in the
  327. # documentation and/or other materials provided with the distribution.
  328. # * Neither the name of the zsh-users nor the
  329. # names of its contributors may be used to endorse or promote products
  330. # derived from this software without specific prior written permission.
  331. #
  332. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  333. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  334. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  335. # DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
  336. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  337. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  338. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  339. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  340. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  341. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  342. # ------------------------------------------------------------------------------
  343. # Description
  344. # -----------
  345. #
  346. # Completion script for ripgrep
  347. #
  348. # ------------------------------------------------------------------------------
  349. # Authors
  350. # -------
  351. #
  352. # * arcizan <ghostrevery@gmail.com>
  353. # * MaskRay <i@maskray.me>
  354. #
  355. # ------------------------------------------------------------------------------
  356. # Local Variables:
  357. # mode: shell-script
  358. # coding: utf-8-unix
  359. # indent-tabs-mode: nil
  360. # sh-indentation: 2
  361. # sh-basic-offset: 2
  362. # End:
  363. # vim: ft=zsh sw=2 ts=2 et