Comment 2 for bug 84822

Revision history for this message
clemens fischer (ino-news) wrote :

the bash-completer for bazaar was one of the easiest i ever got working!
this is due to the builtin support for it, although it might better suit
the zsh. --clemens

# scans args, returns 1st non-option or 1 if none
__get_1st_nonopt()
{
  _1st_nonopt=""
  shift
  for i in "$@"
  do
    case "$i" in
      --)
        return 1
        ;;
      -*)
        ;;
      *)
        _1st_nonopt="$i"
        return 0
        ;;
    esac
  done
  return 1
}
#
_bzr_commands()
{
  bzr s-c | sed -nE 's/^([^:]+):.*/\1/p'
}
_bzr_opts()
{
  bzr s-c "$1" | sed -nE 's/^.+(--[[:alnum:]_-]+).*/\1/p'
  bzr s-c "$1" | sed -nE 's/^.+(-[[:alnum:]]+).*/\1/p'|grep -v -- '-None'
}
_bzr()
{
  cur=${COMP_WORDS[COMP_CWORD]}
  prev=${COMP_WORDS[COMP_CWORD-1]}
  case ".${cur}" in
    .-*)
      __get_1st_nonopt "${COMP_WORDS[@]}" || : 'COMPREPLY=( $(compgen -f $cur) )'
      COMPREPLY=( $(compgen -W "$(_bzr_opts ${_1st_nonopt})" -- $cur) )
      ;;
    .*)
      case "$prev" in
        --)
          COMPREPLY=( $(compgen -f -- $cur) )
          ;;
        bzr)
          COMPREPLY=( $(compgen -W "$(_bzr_commands)" -- $cur))
          ;;
        help|\?|--help|-\?|-h)
          COMPREPLY=( $(compgen -W "$(_bzr_commands)" -- $cur)
            $(compgen -W "$(bzr help topics | sed -nE 's/^([^[:space:]]+).*/\1/p')" -- $cur) )
          ;;
      esac
      ;;
  esac
}
complete -F _bzr -o default bzr