# 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" ) )
    __ltrim_colon_completions "$1"
}

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

    COMPREPLY=()
    _get_comp_words_by_ref -n : cur prev
    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 "$cur"
            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 -o nospace -o filenames perl

_perldoc()
{
    local cur prev prefix temp

    COMPREPLY=()
    _get_comp_words_by_ref -n : cur prev
    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 say 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 break caller import local my our state 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 lock' -- "$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 "$cur"
            COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W \
                '$( PAGER=/bin/cat man perl |  \
                sed -ne "/perl.*Perl overview/,/perlwin32/p" | \
                awk "\$NF=2 { print \$1}" | command grep perl )' -- "$cur" ) )
        fi
        _filedir '@(pl|PL|pm|PM|pod|POD)'
    fi
}
complete -F _perldoc -o bashdefault 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
