_UseFragments ()   # By convention, the function name
{                  # starts with an underscore.
  local curr prev cmd
  # Pointer to current completion word.
  # By convention, it's named "cur" but this isn't strictly necessary.

  COMPREPLY=()   # Array variable storing the possible completions.
  curr=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}
  if [ $COMP_CWORD -gt "1" ] ; then
    cmd=${COMP_WORDS[1]}
  fi

  if [ "$cmd" == "follow" ] ; then
      case "$curr" in
        *)
            COMPREPLY=( $( compgen -W '`find . -not -regex ".*/\..*" -not -regex ".*/_fragments.*" -type f | xargs fragments stat -l ? | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );;
      esac;
  elif [ "$cmd" == "forget" -o "$cmd" == "status" ] ; then
      case "$curr" in
        *)
            COMPREPLY=( $( compgen -W '`$1 status -l AMD\  | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );;
      esac;
  elif [ "$cmd" == "revert" ] ; then
      case "$curr" in
        *)
            COMPREPLY=( $( compgen -W '`$1 status -l MD | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );;
      esac;
  elif [ "$cmd" == "commit" ] ; then
      case "$curr" in
        *)
            COMPREPLY=( $( compgen -W '`$1 status -l AM | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );;
      esac;
  elif [ "$cmd" == "apply" ] ; then
      case "$curr" in
        -*)
            COMPREPLY=( $( compgen -W '-i -a -U --unified' -- $curr ) );;
        *)
            if [ $COMP_CWORD -gt "2" ] ; then
                COMPREPLY=( $( compgen -W '`$1 status -l AMD\  | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );
            else
                COMPREPLY=( $( compgen -W '`$1 status -l M | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );
            fi
      esac;
  elif [ "$cmd" == "rename" ] ; then
      case "$curr" in
        *)
            if [ $COMP_CWORD -gt "2" ] ; then
                COMPREPLY=( $( compgen -f -- $curr | xargs fragments stat -l ? | grep -v "fragments version" | cut -f 2 - | grep -v '_fragments' ) );
            else
                COMPREPLY=( $( compgen -W '`$1 status -l AMD\  | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );
            fi
      esac;
  elif [ "$cmd" == "diff" ] ; then
      case "$curr" in
        -*)
            COMPREPLY=( $( compgen -W '-U --unified' -- $curr ) );;
        *)
            COMPREPLY=( $( compgen -W '`$1 status -l AMD | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );;
      esac;
  elif [ "$cmd" == "fork" ] ; then
      case "$curr" in
        -*)
            COMPREPLY=( $( compgen -W '-U --unified' -- $curr ) );;
        *)
            COMPREPLY=( $( compgen -W '`$1 status -l AM\  | grep -v "fragments version" | cut -f 2 -`' -- $curr ) );;
      esac;
  elif [ "$prev" == "fragments" -o "$cmd" == "help" ] ; then
      case "$curr" in
        a*)
            COMPREPLY=( $( compgen -W 'apply' -- $curr ) );;
        c*)
            COMPREPLY=( $( compgen -W 'commit' -- $curr ) );;
        d*)
            COMPREPLY=( $( compgen -W 'diff' -- $curr ) );;
        h*)
            COMPREPLY=( $( compgen -W 'help' -- $curr ) );;
        i*)
            COMPREPLY=( $( compgen -W 'init' -- $curr ) );;
        f*)
            COMPREPLY=( $( compgen -W 'follow forget fork' -- $curr ) );;
        r*)
            COMPREPLY=( $( compgen -W 'rename revert' -- $curr ) );;
        s*)
            COMPREPLY=( $( compgen -W 'status' -- $curr ) );;
        *)
            COMPREPLY=( $( compgen -W 'help init status follow forget rename diff commit revert fork apply' -- $curr ) );;
      esac
  fi
  return 0
}

complete -F _UseFragments -o filenames fragments
