teach bash_completion about --cwd
authorAlexis S. L. Carvalho <alexis@cecm.usp.br>
Mon, 29 Aug 2005 20:37:07 +0200
changeset 1151 10b4f2a5ce17
parent 1150 4ee09418c8e5
child 1152 ff560ce0c635
teach bash_completion about --cwd
contrib/bash_completion
--- a/contrib/bash_completion	Mon Aug 29 20:37:07 2005 +0200
+++ b/contrib/bash_completion	Mon Aug 29 20:37:07 2005 +0200
@@ -56,19 +56,22 @@
 _hg()
 {
     local cur prev cmd opts i
+    # global options that receive an argument
+    local global_args='--cwd|-R|--repository'
 
     COMPREPLY=()
     cur="$2"
     prev="$3"
 
     # searching for the command 
-    # (first non-option argument that doesn't follow -R/--repository)
+    # (first non-option argument that doesn't follow a global option that
+    #  receives an argument)
     for (( i=1; $i<=$COMP_CWORD; i++ )); do
-	if [[ ${COMP_WORDS[i]} != -* ]] \
-	   && [ "${COMP_WORDS[i-1]}" != -R ] \
-	   && [ "${COMP_WORDS[i-1]}" != --repository ]; then
-	    cmd="${COMP_WORDS[i]}"
-	    break
+	if [[ ${COMP_WORDS[i]} != -* ]]; then
+	    if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
+		cmd="${COMP_WORDS[i]}"
+		break
+	    fi
 	fi
     done
 
@@ -80,10 +83,17 @@
 	return
     fi
 
-    if [ "$prev" = -R ] || [ "$prev" = --repository ]; then
-        COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
-	return
-    fi
+    # global options
+    case "$prev" in
+	-R|--repository)
+	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
+	    return
+	;;
+	--cwd)
+	    COMPREPLY=(${COMPREPLY[@]:-} $( compgen -d -- "$cur" ))
+	    return
+	;;
+    esac
 
     if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
 	_hg_commands