home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / bash / completion-contrib / hg < prev    next >
Encoding:
Text File  |  2006-08-28  |  2.9 KB  |  135 lines

  1. # Mercurial completion by Alexis S. L. Carvalho <alexis@cecm.usp.br>
  2. #
  3. # See: http://www.selenic.com/pipermail/mercurial/2005-August/003378.html
  4. #
  5. # $Id: hg,v 1.1 2006/02/26 00:25:41 ianmacd Exp $
  6. #
  7. _hg_commands()
  8. {
  9.     local commands="$(hg -v help | sed -e '1,/^list of commands:/d' \
  10.                        -e '/^global options:/Q' \
  11.                        -e '/^ [^ ]/!d; s/[,:]//g;')"
  12.     
  13.     # hide debug commands from users, but complete them if 
  14.     # specifically asked for
  15.     if [[ "$cur" == de* ]]; then
  16.     commands="$commands debugcheckstate debugstate debugindex"
  17.     commands="$commands debugindexdot debugwalk"
  18.     fi
  19.     COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$commands" -- "$cur") )
  20. }
  21.  
  22. _hg_paths()
  23. {
  24.     local paths="$(hg paths | sed -e 's/ = .*$//')"
  25.     COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W "$paths" -- "$cur" ))
  26. }
  27.  
  28. _hg_tags()
  29. {
  30.     local tags="$(hg tags | sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
  31.     COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$tags" -- "$cur") )
  32. }
  33.  
  34. # this is "kind of" ugly...
  35. _hg_count_non_option()
  36. {
  37.     local i count=0
  38.     local filters="$1"
  39.  
  40.     for (( i=1; $i<=$COMP_CWORD; i++ )); do
  41.     if [[ "${COMP_WORDS[i]}" != -* ]]; then
  42.         for f in $filters; do
  43.         if [[ ${COMP_WORDS[i-1]} == $f ]]; then
  44.             continue 2
  45.         fi
  46.         done
  47.         count=$(($count + 1))
  48.     fi
  49.     done
  50.  
  51.     echo $(($count - 1))
  52. }
  53.  
  54. _hg()
  55. {
  56.     local cur prev cmd opts i
  57.  
  58.     COMPREPLY=()
  59.     cur="$2"
  60.     prev="$3"
  61.  
  62.     # searching for the command 
  63.     # (first non-option argument that doesn't follow -R/--repository)
  64.     for (( i=1; $i<=$COMP_CWORD; i++ )); do
  65.     if [[ ${COMP_WORDS[i]} != -* ]] \
  66.        && [ "${COMP_WORDS[i-1]}" != -R ] \
  67.        && [ "${COMP_WORDS[i-1]}" != --repository ]; then
  68.         cmd="${COMP_WORDS[i]}"
  69.         break
  70.     fi
  71.     done
  72.  
  73.     if [[ "$cur" == -* ]]; then
  74.     opts="$(hg -v help | sed -e '1,/^global options/d; /^ -/!d')"
  75.  
  76.     if [ -n "$cmd" ]; then
  77.         opts="$opts $(hg help "$cmd" | sed -e '/^ -/!d; s/ [^-][^ ]*//')"
  78.     fi
  79.  
  80.     COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W "$opts" -- "$cur") )
  81.     return
  82.     fi
  83.  
  84.     if [ "$prev" = -R ] || [ "$prev" = --repository ]; then
  85.     COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
  86.     return
  87.     fi
  88.  
  89.     if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
  90.     _hg_commands
  91.     return
  92.     fi
  93.  
  94.     if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
  95.     _hg_tags
  96.     return
  97.     fi
  98.  
  99.     case "$cmd" in
  100.     help)
  101.         _hg_commands
  102.     ;;
  103.     export|manifest|update|checkout|up|co)
  104.         _hg_tags
  105.     ;;
  106.     pull|push)
  107.         _hg_paths
  108.         COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
  109.     ;;
  110.     paths)
  111.         _hg_paths
  112.     ;;
  113.     clone)
  114.         local count=$(_hg_count_non_option)
  115.         if [ $count = 1 ]; then
  116.         _hg_paths
  117.         fi
  118.         COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
  119.     ;;
  120.     cat)
  121.         local count=$(_hg_count_non_option -o --output)
  122.         if [ $count = 2 ]; then
  123.         _hg_tags
  124.         else
  125.         COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
  126.         fi
  127.     ;;
  128.     *) 
  129.         COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -- "$cur" ))
  130.     ;;
  131.     esac
  132.  
  133. }
  134. complete -o filenames -F _hg hg
  135.