contrib/bash_completion
changeset 1683 063e04831a09
parent 1643 747c8d03bd29
child 1684 cf930b2452d3
equal deleted inserted replaced
1682:ca1cda9220d5 1683:063e04831a09
     1 shopt -s extglob
     1 shopt -s extglob
     2 
     2 
     3 _hg_command_list()
     3 _hg_command_list()
     4 {
     4 {
     5     hg --debug help 2>/dev/null | \
     5     "$hg" --debug help 2>/dev/null | \
     6 	awk 'function command_line(line) {
     6 	awk 'function command_line(line) {
     7 		 gsub(/,/, "", line)
     7 		 gsub(/,/, "", line)
     8 		 gsub(/:.*/, "", line)
     8 		 gsub(/:.*/, "", line)
     9 		 split(line, aliases)
     9 		 split(line, aliases)
    10 		 command = aliases[1]
    10 		 command = aliases[1]
    22 
    22 
    23 }
    23 }
    24 
    24 
    25 _hg_option_list()
    25 _hg_option_list()
    26 {
    26 {
    27     hg -v help $1 2> /dev/null | \
    27     "$hg" -v help $1 2> /dev/null | \
    28         awk '/^ *-/ {
    28         awk '/^ *-/ {
    29 		 for (i = 1; i <= NF; i ++) {
    29 		 for (i = 1; i <= NF; i ++) {
    30 		    if (index($i, "-") != 1)
    30 		    if (index($i, "-") != 1)
    31 			 break;
    31 			 break;
    32 		    print $i;
    32 		    print $i;
    54     COMPREPLY=(${COMPREPLY[@]:-} $result)
    54     COMPREPLY=(${COMPREPLY[@]:-} $result)
    55 }
    55 }
    56 
    56 
    57 _hg_paths()
    57 _hg_paths()
    58 {
    58 {
    59     local paths="$(hg paths 2> /dev/null | sed -e 's/ = .*$//')"
    59     local paths="$("$hg" paths 2> /dev/null | sed -e 's/ = .*$//')"
    60     COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$paths' -- "$cur" ))
    60     COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$paths' -- "$cur" ))
    61 }
    61 }
    62 
    62 
    63 _hg_repos()
    63 _hg_repos()
    64 {
    64 {
    68     done
    68     done
    69 }
    69 }
    70 
    70 
    71 _hg_status()
    71 _hg_status()
    72 {
    72 {
    73     local files="$( hg status -n$1 . 2> /dev/null)"
    73     local files="$( "$hg" status -n$1 . 2> /dev/null)"
    74     COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$files' -- "$cur" ))
    74     COMPREPLY=(${COMPREPLY[@]:-} $( compgen -W '$files' -- "$cur" ))
    75 }
    75 }
    76 
    76 
    77 _hg_tags()
    77 _hg_tags()
    78 {
    78 {
    79     local tags="$(hg tags 2> /dev/null |
    79     local tags="$("$hg" tags 2> /dev/null |
    80                       sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
    80                       sed -e 's/[0-9]*:[a-f0-9]\{40\}$//; s/ *$//')"
    81     COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur") )
    81     COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur") )
    82 }
    82 }
    83 
    83 
    84 # this is "kind of" ugly...
    84 # this is "kind of" ugly...
   102 _hg()
   102 _hg()
   103 {
   103 {
   104     local cur prev cmd opts i
   104     local cur prev cmd opts i
   105     # global options that receive an argument
   105     # global options that receive an argument
   106     local global_args='--cwd|-R|--repository'
   106     local global_args='--cwd|-R|--repository'
       
   107     local hg="$1"
   107 
   108 
   108     COMPREPLY=()
   109     COMPREPLY=()
   109     cur="$2"
   110     cur="$2"
   110     prev="$3"
   111     prev="$3"
   111 
   112 
   144 	_hg_commands
   145 	_hg_commands
   145 	return
   146 	return
   146     fi
   147     fi
   147 
   148 
   148     # canonicalize command name
   149     # canonicalize command name
   149     cmd=$(hg -q help "$cmd" 2> /dev/null | sed -e 's/^hg //; s/ .*//; 1q')
   150     cmd=$("$hg" -q help "$cmd" 2> /dev/null | sed -e 's/^hg //; s/ .*//; 1q')
   150 
   151 
   151     if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
   152     if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
   152 	_hg_tags
   153 	_hg_tags
   153 	return
   154 	return
   154     fi
   155     fi