PageRenderTime 44ms CodeModel.GetById 33ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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