contrib/bash_completion
changeset 1684 cf930b2452d3
parent 1683 063e04831a09
child 1805 2af98c4b2587
equal deleted inserted replaced
1683:063e04831a09 1684:cf930b2452d3
     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]
    11 		 delete aliases[1]
    11 		delete aliases[1]
    12 		 print command
    12 		print command
    13 		 for (i in aliases)
    13 		for (i in aliases)
    14 		     if (index(command, aliases[i]) != 1)
    14 		    if (index(command, aliases[i]) != 1)
    15 			 print aliases[i]
    15 			print aliases[i]
    16 	     }
    16 	    }
    17 	     /^list of commands:/ {commands=1}
    17 	    /^list of commands:/ {commands=1}
    18 	     commands && /^ debug/ {a[i++] = $0; next;}
    18 	    commands && /^ debug/ {a[i++] = $0; next;}
    19 	     commands && /^ [^ ]/ {command_line($0)}
    19 	    commands && /^ [^ ]/ {command_line($0)}
    20 	     /^global options:/ {exit 0}
    20 	    /^global options:/ {exit 0}
    21 	     END {for (i in a) command_line(a[i])}'
    21 	    END {for (i in a) command_line(a[i])}'
    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;
    33 		 }
    33 		}
    34 	     }'
    34 	    }'
    35 }
    35 }
    36 
    36 
    37 
    37 
    38 _hg_commands()
    38 _hg_commands()
    39 {
    39 {
    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 {
    65     local i
    65     local i
    66     for i in $( compgen -d -- "$cur" ); do
    66     for i in $(compgen -d -- "$cur"); do
    67         test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
    67 	test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
    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...
    85 _hg_count_non_option()
    85 _hg_count_non_option()
    86 {
    86 {
    87     local i count=0
    87     local i count=0
    88     local filters="$1"
    88     local filters="$1"
    89 
    89 
    90     for (( i=1; $i<=$COMP_CWORD; i++ )); do
    90     for ((i=1; $i<=$COMP_CWORD; i++)); do
    91 	if [[ "${COMP_WORDS[i]}" != -* ]]; then
    91 	if [[ "${COMP_WORDS[i]}" != -* ]]; then
    92 	    if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
    92 	    if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
    93 		continue
    93 		continue
    94 	    fi
    94 	    fi
    95 	    count=$(($count + 1))
    95 	    count=$(($count + 1))
   111     prev="$3"
   111     prev="$3"
   112 
   112 
   113     # searching for the command
   113     # searching for the command
   114     # (first non-option argument that doesn't follow a global option that
   114     # (first non-option argument that doesn't follow a global option that
   115     #  receives an argument)
   115     #  receives an argument)
   116     for (( i=1; $i<=$COMP_CWORD; i++ )); do
   116     for ((i=1; $i<=$COMP_CWORD; i++)); do
   117 	if [[ ${COMP_WORDS[i]} != -* ]]; then
   117 	if [[ ${COMP_WORDS[i]} != -* ]]; then
   118 	    if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
   118 	    if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
   119 		cmd="${COMP_WORDS[i]}"
   119 		cmd="${COMP_WORDS[i]}"
   120 		break
   120 		break
   121 	    fi
   121 	    fi
   123     done
   123     done
   124 
   124 
   125     if [[ "$cur" == -* ]]; then
   125     if [[ "$cur" == -* ]]; then
   126 	opts=$(_hg_option_list $cmd)
   126 	opts=$(_hg_option_list $cmd)
   127 
   127 
   128 	COMPREPLY=( ${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur") )
   128 	COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
   129 	return
   129 	return
   130     fi
   130     fi
   131 
   131 
   132     # global options
   132     # global options
   133     case "$prev" in
   133     case "$prev" in
   145 	_hg_commands
   145 	_hg_commands
   146 	return
   146 	return
   147     fi
   147     fi
   148 
   148 
   149     # canonicalize command name
   149     # canonicalize command name
   150     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')
   151 
   151 
   152     if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
   152     if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" = --rev ]; then
   153 	_hg_tags
   153 	_hg_tags
   154 	return
   154 	return
   155     fi
   155     fi
   189 	clone)
   189 	clone)
   190 	    local count=$(_hg_count_non_option)
   190 	    local count=$(_hg_count_non_option)
   191 	    if [ $count = 1 ]; then
   191 	    if [ $count = 1 ]; then
   192 		_hg_paths
   192 		_hg_paths
   193 	    fi
   193 	    fi
   194             _hg_repos
   194 	    _hg_repos
   195 	;;
   195 	;;
   196 	debugindex|debugindexdot)
   196 	debugindex|debugindexdot)
   197 	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.i" -- "$cur" ))
   197 	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
   198 	;;
   198 	;;
   199 	debugdata)
   199 	debugdata)
   200 	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -f -X "!*.d" -- "$cur" ))
   200 	    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
   201 	;;
   201 	;;
   202     esac
   202     esac
   203 
   203 
   204 }
   204 }
   205 
   205 
   206 complete -o bashdefault -o default -F _hg hg 2> /dev/null \
   206 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
   207     || complete -o default -F _hg hg
   207     || complete -o default -F _hg hg