# bash completion for perl

have perl &&
{
_perlmodules()
{
    COMPREPLY=( $( compgen -P "$prefix" -W "$( perl -e 'sub mods { my ($base,$dir)=@_; return if  $base !~ /^\Q$ENV{cur}/; chdir($dir) or return; for (glob(q[*.pm])) {s/\.pm$//; print qq[$base$_\n]}; mods(/^(?:[.\d]+|$Config{archname}-$Config{osname}|auto)$/ ? undef : qq[${base}${_}\\\\:\\\\:],qq[$dir/$_]) for grep {-d} glob(q[*]); } mods(undef,$_) for @INC;' )" -- "$cur" ) )
}

_perl()
{
    local cur prev prefix temp
    local optPrefix optSuffix

    COMPREPLY=()
    cur=`_get_cword`
    prev=${COMP_WORDS[COMP_CWORD-1]}
    prefix=""

    # If option not followed by whitespace, reassign prev and cur
    if [[ "$cur" == -?* ]]; then
        temp=$cur
        prev=${temp:0:2}
        cur=${temp:2}
        optPrefix=-P$prev
        optSuffix=-S/
        prefix=$prev
    fi

    # only handle module completion for now
    case "$prev" in
        -I|-x)
            local IFS=$'\t\n'
            COMPREPLY=( $( compgen -d $optPrefix $optSuffix -- "$cur" ) )
            return 0
            ;;
        -m|-M)
            _perlmodules
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
	    COMPREPLY=( $( compgen -W '-C -s -T -u -U -W -X -h -v -V -c -w -d \
		    -D -p -n -a -F -l -0 -I -m -M -P -S -x -i -e ' -- "$cur" ) )
    else
        _filedir
    fi
}
complete -F _perl $nospace $filenames perl

_perldoc()
{
    local cur prev prefix temp

    COMPREPLY=()
    cur=`_get_cword`
    prev=${COMP_WORDS[COMP_CWORD-1]}
    prefix=""

    # completing an option (may or may not be separated by a space)
    if [[ "$cur" == -?* ]]; then
        temp=$cur
        prev=${temp:0:2}
        cur=${temp:2}
        prefix=$prev
    fi

    # complete builtin perl functions
    case $prev in
        -f)
            COMPREPLY=( $( compgen -W 'chomp chop chr crypt hex index lc \
                lcfirst length oct ord pack q qq reverse rindex sprintf \
                substr tr uc ucfirst y m pos quotemeta s split study qr abs \
                atan2 cos exp hex int log oct rand sin sqrt srand pop push \
                shift splice unshift grep join map qw reverse sort unpack \
                delete each exists keys values binmode close closedir \
                dbmclose dbmopen die eof fileno flock format getc print \
                printf read readdir rewinddir seek seekdir select syscall \
                sysread sysseek syswrite tell telldir truncate warn write \
                pack read syscall sysread syswrite unpack vec -X chdir chmod \
                chown chroot fcntl glob ioctl link lstat mkdir open opendir \
                readlink rename rmdir stat symlink umask unlink utime caller \
                continue do dump eval exit goto last next redo return \
                sub wantarray caller import local my our package use defined \
                formline reset scalar undef \
                alarm exec fork getpgrp getppid getpriority kill pipe qx \
                setpgrp setpriority sleep system times wait waitpid \
                import no package require use bless dbmclose dbmopen package \
                ref tie tied untie use accept bind connect getpeername \
                getsockname getsockopt listen recv send setsockopt shutdown \
                socket socketpair msgctl msgget msgrcv msgsnd semctl semget \
                semop shmctl shmget shmread shmwrite endgrent endhostent \
                endnetent endpwent getgrent getgrgid getgrnam getlogin \
                getpwent getpwnam getpwuid setgrent setpwent endprotoent \
                endservent gethostbyaddr gethostbyname gethostent \
                getnetbyaddr getnetbyname getnetent getprotobyname \
                getprotobynumber getprotoent getservbyname getservbyport \
                getservent sethostent setnetent setprotoent setservent \
                gmtime localtime time times' -- "$cur" ) )
            return 0
            ;;
    esac

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '-h -v -t -u -m -l -F -X -f -q' -- "$cur" ))
    else
        # return available modules (unless it is clearly a file)
        if [[ "$cur" != */* ]]; then
            _perlmodules
            COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W \
                '$( PAGER=/bin/cat man perl |  \
                sed -ne "/perl.*Perl overview/,/perlwin32/p" | \
                awk "\$NF=2 { print \$1}" | grep perl )' -- "$cur" ) )
        fi
    fi
}
complete -F _perldoc $default perldoc
}

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
