Looking at changing the option style for sage got me looking for tab completion 
in bash (e.g. ./sage --n[TAB] => ./sage --notebook).  I thought there was 
already something written but, I couldn't find anything and so I threw 
something together.  

It's very inefficient (it parses the output of `sage --help` and `sage 
--advanced` and the first run is quite slow), but it's small (only 35 lines) 
and shouldn't require much maintenance as long as the help messages are 
updated.  It's not POSIX compliant (e.g. it uses grep -o) but I would be 
willing to make it so.

I'm wondering if others are interested, and if so whether I should put it on 
the wiki, make an spkg or add it directly to sage somewhere (data/extcode??).

-Ivan

# add this to .bashrc or paste in terminal

# bash tab completion for sage
SAGE_OPTIONS=
_sage_tab() {
    COMPREPLY=()
    _sage_tab_binary=${COMP_WORDS[0]}
    _sage_tab_current_word=${COMP_WORDS[COMP_CWORD]}

    # Cache SAGE_OPTIONS for performance
    if [ "$SAGE_OPTIONS" = '' ]; then
        SAGE_OPTIONS=$(
            ("$_sage_tab_binary" --help 2>/dev/null;
                "$_sage_tab_binary" --advanced 2>/dev/null; ) \
                    | grep -Eoe '--?[a-zA-Z_][a-zA-Z_-]*' | sort -u)
    fi

    case ${_sage_tab_current_word} in
        -*) # Options
            COMPREPLY=($(echo ${sage_optio...@]} | tr " " "\n" \
                | grep -oe "${_sage_tab_current_word}[a-z-]*" \
                | sort | uniq | tr "\n" " "));;
        *)  # Search for files that sage understands
            # If it doesn't find any it will default to directories
            SAGE_EXTENSIONS='sage|py|spkg'
            COMPREPLY=($(compgen -G "${_sage_tab_current_word}*" \
                | grep -Ee '\.('$SAGE_EXTENSIONS')(\.gz)?$'));;
    esac
    return 0
}
# use _sage_tab to complete, it produces filenames, and if no matches
# are returned, complete with directory names
complete -F _sage_tab -o filenames -o dirnames sage

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

To unsubscribe from this group, send email to 
sage-devel+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to