nimgrep.bash-completion 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # bash completion for nimgrep -*- shell-script -*-
  2. __is_short_or_long()
  3. {
  4. local actual short long
  5. actual="$1"
  6. short="$2"
  7. long="$3"
  8. [[ ! -z $short && $actual == $short ]] && return 0
  9. [[ ! -z $long && $actual == $long ]] && return 0
  10. return 1
  11. }
  12. __ask_for_subcmd_or_subopts()
  13. {
  14. local args cmd subcmd words sub_words word_first word_last word_lastlast
  15. local len ilast ilastlast i ele sub_len n_nopts
  16. args=("$@")
  17. ask_for_what="${args[0]}"
  18. cmd="${args[1]}"
  19. subcmd="${args[2]}"
  20. ilast="${args[3]}"
  21. words=("${args[@]:4}")
  22. len=${#words[@]}
  23. ilastlast=$((ilast - 1))
  24. sub_words=("${words[@]:0:ilast}")
  25. sub_len=${#sub_words[@]}
  26. word_first=${words[0]}
  27. word_last=${words[ilast]}
  28. word_lastlast=${words[ilastlast]}
  29. n_nopts=0
  30. # printf "\n[DBUG] word_first:${word_first}|ilast:${ilast}|words(${len}):${words[*]}|sub_words(${sub_len}):${sub_words[*]}\n"
  31. if [[ $word_first != $cmd ]]
  32. then
  33. return 1
  34. fi
  35. i=0
  36. while [[ $i -lt $len ]]
  37. do
  38. ele=${words[i]}
  39. if [[ ! $ele =~ ^- ]]
  40. then
  41. if [[ $ele == $cmd || $ele == $subcmd ]]
  42. then
  43. ((n_nopts+=1))
  44. elif [[ $i -eq $ilast && $ele =~ ^[a-zA-Z] ]]
  45. then
  46. ((i=i))
  47. elif [[ -z $ele ]]
  48. then
  49. ((i=i))
  50. elif [[ $ele =~ ^: ]]
  51. then
  52. ((i+=1))
  53. else
  54. return 1
  55. fi
  56. fi
  57. ((i+=1))
  58. done
  59. case $ask_for_what in
  60. 1)
  61. if [[ n_nopts -eq 1 ]]
  62. then
  63. if [[ -z $word_last || $word_last =~ ^[a-zA-Z] ]] && [[ $word_lastlast != : ]]
  64. then
  65. return 0
  66. fi
  67. fi
  68. ;;
  69. 2)
  70. if [[ n_nopts -eq 2 ]]
  71. then
  72. if [[ -z $word_last ]] || [[ $word_last =~ ^[-:] ]]
  73. then
  74. return 0
  75. fi
  76. fi
  77. esac
  78. return 1
  79. }
  80. __ask_for_subcmd()
  81. {
  82. __ask_for_subcmd_or_subopts 1 "$@"
  83. }
  84. __ask_for_subcmd_opts()
  85. {
  86. __ask_for_subcmd_or_subopts 2 "$@"
  87. }
  88. _nimgrep()
  89. {
  90. local curr prev prevprev words
  91. local i_curr n_words i_prev i_prevprev
  92. COMPREPLY=()
  93. i_curr=$COMP_CWORD
  94. n_words=$((i_curr+1))
  95. i_prev=$((i_curr-1))
  96. i_prevprev=$((i_curr-2))
  97. curr="${COMP_WORDS[i_curr]}"
  98. prev="${COMP_WORDS[i_prev]}"
  99. prevprev="${COMP_WORDS[i_prevprev]}"
  100. words=("${COMP_WORDS[@]:0:n_words}")
  101. local subcmds opts candids
  102. # printf "\n[DBUG] curr:$curr|prev:$prev|words(${#words[*]}):${words[*]}\n"
  103. # Asking for a subcommand
  104. if false && __ask_for_subcmd nimgrep nimgrep $i_curr "${words[@]}"
  105. then
  106. subcmds=""
  107. return 0
  108. fi
  109. # Prioritize subcmd over opt
  110. if false
  111. then
  112. return 124
  113. elif false && __ask_for_subcmd_opts nimgrep compileToC $i_curr "${words[@]}"
  114. then
  115. opts=() \
  116. && candids=()
  117. else
  118. opts=() \
  119. && candids=()
  120. opts+=("-f" "--find" "") \
  121. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  122. opts+=("" "--replace" "") \
  123. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  124. opts+=("" "--confirm" "") \
  125. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  126. opts+=("" "--filenames" "") \
  127. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  128. opts+=("" "--peg" "") \
  129. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  130. opts+=("" "--re" "") \
  131. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  132. opts+=("-x" "--rex" "") \
  133. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  134. opts+=("-w" "--word" "") \
  135. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  136. opts+=("-i" "--ignoreCase" "") \
  137. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  138. opts+=("-y" "--ignoreStyle" "") \
  139. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  140. opts+=("-r" "--recursive" "") \
  141. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  142. opts+=("" "--follow" "") \
  143. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  144. opts+=("-s" "--sortTime" "asc desc") \
  145. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  146. opts+=("" "--extensions" "EX") \
  147. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  148. opts+=("" "--notextensions" "EX") \
  149. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  150. opts+=("" "--filename" "PAT") \
  151. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  152. opts+=("" "--notfilename" "PAT") \
  153. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  154. opts+=("" "--dirname" "PAT") \
  155. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  156. opts+=("" "--notdirname" "PAT") \
  157. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  158. opts+=("" "--dirpath" "PAT") \
  159. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  160. opts+=("" "--notdirpath" "PAT") \
  161. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  162. opts+=("" "--inFile" "PAT") \
  163. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  164. opts+=("" "--notinFile" "PAT") \
  165. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  166. opts+=("" "--bin" "on off only") \
  167. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  168. opts+=("-t" "--text" "") \
  169. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  170. opts+=("" "--inContext" "PAT") \
  171. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  172. opts+=("" "--notinContext" "PAT") \
  173. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  174. opts+=("" "--nocolor" "") \
  175. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  176. opts+=("" "--colorTheme" "THEME") \
  177. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  178. opts+=("" "--color" "") \
  179. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  180. opts+=("" "--count" "") \
  181. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  182. opts+=("-c" "--context" "N") \
  183. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  184. opts+=("-a" "--afterContext" "N") \
  185. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  186. opts+=("-b" "--beforeContext" "N") \
  187. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  188. opts+=("-g" "--group" "") \
  189. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  190. opts+=("-l" "--newLine" "") \
  191. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  192. opts+=("" "--cols" "N auto") \
  193. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  194. opts+=("" "--onlyAscii" "") \
  195. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  196. opts+=("-j" "--threads" "N") \
  197. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  198. opts+=("" "--stdin" "") \
  199. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  200. opts+=("" "--verbose" "") \
  201. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  202. opts+=("-h" "--help" "") \
  203. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  204. opts+=("-v" "--version" "") \
  205. && candids+=(${opts[$((${#opts[@]}-3))]} ${opts[$((${#opts[@]}-2))]})
  206. fi
  207. local c_short c_long c_accvals
  208. local len i idx0 idx1 idx2
  209. case $curr in
  210. # Asking for accepted optvalues, e.g., `out:`
  211. :)
  212. len=${#opts[@]}
  213. i=0
  214. while [[ $i -lt $len ]]
  215. do
  216. idx0=$((i / 3 * 3))
  217. idx1=$((idx0 + 1))
  218. idx2=$((idx1 + 1))
  219. c_short=${opts[idx0]}
  220. c_long=${opts[idx1]}
  221. c_accvals=${opts[idx2]}
  222. (false \
  223. || __is_short_or_long $prev ${c_short} ${c_long} \
  224. || false) \
  225. && COMPREPLY=( $(compgen -W "${c_accvals}" --) ) \
  226. && return 0
  227. ((i+=3))
  228. done
  229. return 124
  230. ;;
  231. *)
  232. # When in a incomplete opt value, e.g., `--check:of`
  233. if [[ $prev == : ]]
  234. then
  235. len=${#opts[@]}
  236. i=0
  237. while [[ $i -lt $len ]]
  238. do
  239. idx0=$((i / 3 * 3))
  240. idx1=$((idx0 + 1))
  241. idx2=$((idx1 + 1))
  242. c_short=${opts[idx0]}
  243. c_long=${opts[idx1]}
  244. c_accvals=${opts[idx2]}
  245. (false \
  246. || __is_short_or_long $prevprev ${c_short} ${c_long} \
  247. || false) \
  248. && COMPREPLY=( $(compgen -W "${c_accvals}" -- ${curr}) ) \
  249. && return 0
  250. ((i+=3))
  251. done
  252. return 124
  253. fi
  254. # When in a complete optname, might need optvalue, e.g., `--check`
  255. if [[ $curr =~ ^--?[:()a-zA-Z]+$ ]]
  256. then
  257. len=${#opts[@]}
  258. i=0
  259. while [[ $i -lt $len ]]
  260. do
  261. idx0=$(((i / 3 * 3)))
  262. idx1=$((idx0 + 1))
  263. idx2=$((idx1 + 1))
  264. c_short=${opts[idx0]}
  265. c_long=${opts[idx1]}
  266. c_accvals=${opts[idx2]}
  267. if __is_short_or_long $curr ${c_short} ${c_long}
  268. then
  269. if [[ ! -z $c_accvals ]]
  270. then
  271. COMPREPLY=( $(compgen -W "${curr}:" -- ${curr}) ) \
  272. && compopt -o nospace \
  273. && return 0
  274. else
  275. COMPREPLY=( $(compgen -W "${curr}" -- ${curr}) ) \
  276. && return 0
  277. fi
  278. fi
  279. ((i+=3))
  280. done # while
  281. if true
  282. then
  283. COMPREPLY=( $(compgen -W "${candids[*]}" -- "$curr") )
  284. compopt -o nospace
  285. return 0
  286. fi
  287. # When in an incomplete optname, e.g., `--chec`
  288. elif [[ $curr =~ ^--?[^:]* ]]
  289. then
  290. if true
  291. then
  292. COMPREPLY=( $(compgen -W "${candids[*]}" -- "$curr") )
  293. compopt -o nospace
  294. return 0
  295. fi
  296. fi
  297. if true
  298. then
  299. compopt -o filenames
  300. COMPREPLY=( $(compgen -f -- "$curr") )
  301. compopt -o nospace
  302. return 0
  303. fi
  304. ;;
  305. esac
  306. return 0
  307. } &&
  308. complete -F _nimgrep nimgrep
  309. # ex: filetype=sh