# -*- mode: shell-script; sh-basic-offset: 2; indent-tabs-mode: nil -*- # global options that receive an argument global_args='-w|--workspace|-r|--remote-repo-base' # as a workspace contains a large number of dirs, we set # this var to avoid the question 'display all n possibilities?' if [ `bind -v | grep completion-query-items | cut -f 3 -d ' '` -lt 200 ] ; then bind 'set completion-query-items 200' fi _dwimdev_commands() { local commands commands=$(grep -e "^function.*# *public" `dirname $(which dwimdev)`/environment.sh | sed -e 's/function[ \t]*// ; s/^\([^ \t]*\).*/\1/') COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur")) } _dwimdev_completion_type() { local type type=$(grep -e "^function.*$cmd.*# *public" `dirname $(which dwimdev)`/environment.sh | cut -f 2 -d :) if [ "$type" != "" ] ; then completion_type=$type fi } _dwimdev_local() { if [ ! $(type -t locate) ] ; then echo "Please install locate (mlocate) for repository completion" return fi local paths="$(locate -r 'hu.dwim.environment$' | grep -v -e '\(\.hg\|_darcs\|\.git\)' | sed -e 's/hu.dwim.environment$//' )" COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur")) } _dwimdev_workspace_repo() { local i stripped for i in $(compgen -d -- "$workspace/$cur"); do if [[ -d "$i"/_darcs || -d "$i"/.git || -d "$i"/.svn || -d "$i"/CVS ]] ; then stripped=$(echo $i | sed -e "s|$workspace/||") COMPREPLY=(${COMPREPLY[@]:-} "$stripped") fi done } _dwimdev_workspace_dir() { local i stripped for i in $(compgen -d -- "$workspace/$cur" | sed -e "s|$workspace/||g"); do # stripped=$(echo $i | sed -e "s|$workspace/||g") # COMPREPLY=(${COMPREPLY[@]:-} "$stripped") COMPREPLY=(${COMPREPLY[@]:-} "$i") done } _dwimdev() { local cur COMPREPLY=() local cur prev cmd cmd_index opts i local dwimdev="$1" local canonical=0 local workspace=${DWIM_WORKSPACE} local completion_type="none" # check whether the shell knows dwimdev at all if [ ! $(type -t dwimdev) ] ; then printf "\nPlease add hu.dwim.environment/bin to your PATH (e.g. by sourcing environment.sh)\n" return fi COMPREPLY=() cur="$2" prev="$3" # searching for the command # (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]} != -* ]]; then if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then cmd="${COMP_WORDS[i]}" cmd_index=$i break elif [[ ${COMP_WORDS[i-1]} == @(-w|--workspace) ]]; then workspace=${COMP_WORDS[i]} fi fi done if [[ "$cur" == -* ]]; then opts=$(echo $global_args | sed -e 's/|/ /') COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur")) return fi # global options case "$prev" in -w|--workspace) _dwimdev_local return ;; -r|--remote-repo-base) _dwimdev_remote return ;; --cwd) # Stick with default bash completion return ;; esac if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then _dwimdev_commands return fi # debug output # echo "cmd $cmd completion_type $completion_type cur $cur prev $prev workspace $workspace i $i COMP_CWORD $COMP_CWORD" _dwimdev_completion_type if [ "$completion_type" != "none" ] ; then _dwimdev_workspace_$completion_type fi } complete -F _dwimdev dwimdev