/contrib/_hgtk

https://bitbucket.org/tortoisehg/hgtk/ · #! · 402 lines · 344 code · 58 blank · 0 comment · 0 complexity · e4e33ff1a566febb78f1df292bedb3cc MD5 · raw file

  1. #compdef hgtk
  2. # Zsh completion script for hgtk.
  3. # copy it into your zsh function path (/usr/share/zsh/site-functions)
  4. #
  5. # Copyright (C) 2009-2010 Steve Borho
  6. #
  7. # This is free software; you can redistribute it and/or modify it under
  8. # the terms of the GNU General Public License as published by the Free
  9. # Software Foundation; either version 2 of the License, or (at your
  10. # option) any later version.
  11. emulate -LR zsh
  12. setopt extendedglob
  13. local curcontext="$curcontext" state line
  14. typeset -A _hg_cmd_globals
  15. _hgtk() {
  16. local cmd _hg_root
  17. integer i=2
  18. _hg_cmd_globals=()
  19. while (( i < $#words ))
  20. do
  21. case "$words[$i]" in
  22. -R|--repository)
  23. eval _hg_root="$words[$i+1]"
  24. _hg_cmd_globals+=("$words[$i]" "$_hg_root")
  25. (( i += 2 ))
  26. continue
  27. ;;
  28. -R*)
  29. _hg_cmd_globals+="$words[$i]"
  30. eval _hg_root="${words[$i]#-R}"
  31. (( i++ ))
  32. continue
  33. ;;
  34. --cwd|--config)
  35. # pass along arguments to hg completer
  36. _hg_cmd_globals+=("$words[$i]" "$words[$i+1]")
  37. (( i += 2 ))
  38. continue
  39. ;;
  40. -*)
  41. # skip option
  42. (( i++ ))
  43. continue
  44. ;;
  45. esac
  46. if [[ -z "$cmd" ]]
  47. then
  48. cmd="$words[$i]"
  49. words[$i]=()
  50. (( CURRENT-- ))
  51. fi
  52. (( i++ ))
  53. done
  54. if [[ -z "$cmd" ]]
  55. then
  56. _arguments -s -w : $_hgtk_global_opts \
  57. ':hgtk command:_hgtk_commands'
  58. return
  59. fi
  60. # resolve abbreviations and aliases
  61. if ! (( $+functions[_hgtk_cmd_${cmd}] ))
  62. then
  63. local cmdexp
  64. (( $#_hgtk_cmd_list )) || _hgtk_get_commands
  65. cmdexp=$_hgtk_cmd_list[(r)${cmd}*]
  66. if [[ $cmdexp == $_hgtk_cmd_list[(R)${cmd}*] ]]
  67. then
  68. # might be nice to rewrite the command line with the expansion
  69. cmd="$cmdexp"
  70. fi
  71. if [[ -n $_hgtk_alias_list[$cmd] ]]
  72. then
  73. cmd=$_hgtk_alias_list[$cmd]
  74. fi
  75. fi
  76. curcontext="${curcontext%:*:*}:hgtk-${cmd}:"
  77. zstyle -s ":completion:$curcontext:" cache-policy update_policy
  78. if [[ -z "$update_policy" ]]
  79. then
  80. zstyle ":completion:$curcontext:" cache-policy _hgtk_cache_policy
  81. fi
  82. if (( $+functions[_hgtk_cmd_${cmd}] ))
  83. then
  84. _hgtk_cmd_${cmd}
  85. else
  86. # complete unknown commands normally
  87. _arguments -s -w : $_hgtk_global_opts \
  88. '*:files:_hg_files'
  89. fi
  90. }
  91. _hgtk_cache_policy() {
  92. typeset -a old
  93. # cache for a minute
  94. old=( "$1"(mm+10) )
  95. (( $#old )) && return 0
  96. return 1
  97. }
  98. _hgtk_get_commands() {
  99. typeset -ga _hgtk_cmd_list
  100. typeset -gA _hgtk_alias_list
  101. local hline cmd cmdalias
  102. _call_program hgtk hgtk debugcomplete -v 2>/dev/null | while read -A hline
  103. do
  104. cmd=$hline[1]
  105. _hgtk_cmd_list+=($cmd)
  106. for cmdalias in $hline[2,-1]
  107. do
  108. _hgtk_cmd_list+=($cmdalias)
  109. _hgtk_alias_list+=($cmdalias $cmd)
  110. done
  111. done
  112. }
  113. _hgtk_commands() {
  114. (( $#_hgtk_cmd_list )) || _hgtk_get_commands
  115. _describe -t commands 'hgtk command' _hgtk_cmd_list
  116. }
  117. _hg_revrange() {
  118. compset -P 1 '*:'
  119. _hg_tags "$@"
  120. }
  121. _hg_tags() {
  122. typeset -a tags
  123. local tag rev
  124. _hg_cmd tags 2> /dev/null | while read tag
  125. do
  126. tags+=(${tag/ # [0-9]#:*})
  127. done
  128. (( $#tags )) && _describe -t tags 'tags' tags
  129. }
  130. _hg_files() {
  131. if [[ -n "$_hg_root" ]]
  132. then
  133. [[ -d "$_hg_root/.hg" ]] || return
  134. case "$_hg_root" in
  135. /*)
  136. _files -W $_hg_root
  137. ;;
  138. *)
  139. _files -W $PWD/$_hg_root
  140. ;;
  141. esac
  142. else
  143. _files
  144. fi
  145. }
  146. _hg_status() {
  147. [[ -d $PREFIX ]] || PREFIX=$PREFIX:h
  148. status_files=(${(ps:\0:)"$(_hg_cmd status -0n$1 ./$PREFIX 2>/dev/null)"})
  149. }
  150. _hg_unknown() {
  151. typeset -a status_files
  152. _hg_status u
  153. _wanted files expl 'unknown files' _multi_parts / status_files
  154. }
  155. _hg_missing() {
  156. typeset -a status_files
  157. _hg_status d
  158. _wanted files expl 'missing files' _multi_parts / status_files
  159. }
  160. _hg_modified() {
  161. typeset -a status_files
  162. _hg_status m
  163. _wanted files expl 'modified files' _multi_parts / status_files
  164. }
  165. _hg_resolve() {
  166. local rstate rpah
  167. [[ -d $PREFIX ]] || PREFIX=$PREFIX:h
  168. _hg_cmd resolve -l ./$PREFIX 2> /dev/null | while read rstate rpath
  169. do
  170. [[ $rstate == 'R' ]] && resolved_files+=($rpath)
  171. [[ $rstate == 'U' ]] && unresolved_files+=($rpath)
  172. done
  173. }
  174. _hg_resolved() {
  175. typeset -a resolved_files unresolved_files
  176. _hg_resolve
  177. _wanted files expl 'resolved files' _multi_parts / resolved_files
  178. }
  179. _hg_unresolved() {
  180. typeset -a resolved_files unresolved_files
  181. _hg_resolve
  182. _wanted files expl 'unresolved files' _multi_parts / unresolved_files
  183. }
  184. _hg_config() {
  185. typeset -a items
  186. items=(${${(%f)"$(_call_program hg hg showconfig)"}%%\=*})
  187. (( $#items )) && _describe -t config 'config item' items
  188. }
  189. _hg_addremove() {
  190. _alternative 'files:unknown files:_hg_unknown' \
  191. 'files:missing files:_hg_missing'
  192. }
  193. _hg_ssh_urls() {
  194. if [[ -prefix */ ]]
  195. then
  196. if zstyle -T ":completion:${curcontext}:files" remote-access
  197. then
  198. local host=${PREFIX%%/*}
  199. typeset -a remdirs
  200. compset -p $(( $#host + 1 ))
  201. local rempath=${(M)PREFIX##*/}
  202. local cacheid="hg:${host}-${rempath//\//_}"
  203. cacheid=${cacheid%[-_]}
  204. compset -P '*/'
  205. if _cache_invalid "$cacheid" || ! _retrieve_cache "$cacheid"
  206. then
  207. remdirs=(${${(M)${(f)"$(_call_program files ssh -a -x $host ls -1FL "${(q)rempath}" 2> /dev/null)"}##*/}%/})
  208. _store_cache "$cacheid" remdirs
  209. fi
  210. _describe -t directories 'remote directory' remdirs -S/
  211. else
  212. _message 'remote directory'
  213. fi
  214. else
  215. if compset -P '*@'
  216. then
  217. _hosts -S/
  218. else
  219. _alternative 'hosts:remote host name:_hosts -S/' \
  220. 'users:user:_users -S@'
  221. fi
  222. fi
  223. }
  224. _hg_urls() {
  225. if compset -P bundle://
  226. then
  227. _files
  228. elif compset -P ssh://
  229. then
  230. _hg_ssh_urls
  231. elif [[ -prefix *: ]]
  232. then
  233. _urls
  234. else
  235. local expl
  236. compset -S '[^:]*'
  237. _wanted url-schemas expl 'URL schema' compadd -S '' - \
  238. http:// https:// ssh:// bundle://
  239. fi
  240. }
  241. _hg_paths() {
  242. typeset -a paths pnames
  243. _hg_cmd paths 2> /dev/null | while read -A pnames
  244. do
  245. paths+=($pnames[1])
  246. done
  247. (( $#paths )) && _describe -t path-aliases 'repository alias' paths
  248. }
  249. _hg_remote() {
  250. _alternative 'path-aliases:repository alias:_hg_paths' \
  251. 'directories:directory:_files -/' \
  252. 'urls:URL:_hg_urls'
  253. }
  254. _hg_clone_dest() {
  255. _alternative 'directories:directory:_files -/' \
  256. 'urls:URL:_hg_urls'
  257. }
  258. # Common options
  259. _hgtk_global_opts=(
  260. '(--repository -R)'{-R+,--repository}'[repository root directory]:repository:_files -/'
  261. '(--verbose -v)'{-v,--verbose}'[enable additional output]'
  262. '(--quiet -q)'{-q,--quiet}'[suppress output]'
  263. '(--help -h)'{-h,--help}'[display help and exit]'
  264. '--debugger[start debugger]'
  265. )
  266. _hg_cmd() {
  267. _call_program hg hg "$_hg_cmd_globals[@]" "$@"
  268. }
  269. _hgtk_cmd_add() {
  270. _arguments -s -w : $_hgtk_global_opts \
  271. '*:unknown files:_hg_unknown'
  272. }
  273. _hgtk_cmd_annotate() {
  274. _arguments -s -w : $_hgtk_global_opts \
  275. '*:files:_hg_files'
  276. }
  277. _hgtk_cmd_clone() {
  278. _arguments -s -w : $_hgtk_global_opts \
  279. ':source repository:_hg_remote' \
  280. ':destination:_hg_clone_dest'
  281. }
  282. _hgtk_cmd_commit() {
  283. _arguments -s -w : $_hgtk_global_opts \
  284. '(--date -d)'{-d+,--date}'[record datecode as commit date]:date code:' \
  285. '(--user -u)'{-u+,--user}'[record user as commiter]:user:' \
  286. '*:file:_hg_files'
  287. }
  288. _hgtk_cmd_vdiff() {
  289. typeset -A opt_args
  290. _arguments -s -w : $_hgtk_global_opts \
  291. '*'{-r,--rev}'+[revision]:revision:_hg_revrange' \
  292. '*:file:->diff_files'
  293. if [[ $state == 'diff_files' ]]
  294. then
  295. if [[ -n $opt_args[-r] ]]
  296. then
  297. _hg_files
  298. else
  299. _hg_modified
  300. fi
  301. fi
  302. }
  303. _hgtk_cmd_grep() {
  304. _arguments -s -w : $_hgtk_global_opts \
  305. '*:files:_hg_files'
  306. }
  307. _hgtk_cmd_help() {
  308. _arguments -s -w : $_hgtk_global_opts \
  309. '*:hgtk command:_hgtk_commands'
  310. }
  311. _hgtk_cmd_init() {
  312. _arguments -s -w : $_hgtk_global_opts \
  313. ':dir:_files -/'
  314. }
  315. _hgtk_cmd_log() {
  316. _arguments -s -w : $_hgtk_global_opts \
  317. '(--limit -l)'{-l+,--limit}'[limit number of changes displayed]:' \
  318. '*'{-r,--rev}'[show the specified revision or range]:revision:_hg_revrange' \
  319. '*:files:_hg_files'
  320. }
  321. _hgtk_cmd_pull() {
  322. _arguments -s -w : $_hgtk_global_opts
  323. }
  324. _hgtk_cmd_push() {
  325. _arguments -s -w : $_hgtk_global_opts
  326. }
  327. _hgtk_cmd_rename() {
  328. _arguments -s -w : $_hgtk_global_opts \
  329. '*:file:_hg_files'
  330. }
  331. _hgtk_cmd_serve() {
  332. _arguments -s -w : $_hgtk_global_opts
  333. }
  334. _hgtk_cmd_status() {
  335. _arguments -s -w : $_hgtk_global_opts \
  336. '--rev[show difference from revision]:revision:_hg_tags' \
  337. '*:files:_files'
  338. }
  339. _hgtk_cmd_update() {
  340. _arguments -s -w : $_hgtk_global_opts \
  341. '(--rev -r)'{-r+,--rev}'[revision]:revision:_hg_tags' \
  342. ':revision:_hg_tags'
  343. }
  344. _hgtk "$@"