grep.zsh 1.33 KB
Newer Older
Marc Cornellà's avatar
Marc Cornellà committed
1
2
3
4
5
__GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias

# See if there's a cache file modified in the last day
__GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
if [[ -z "$__GREP_ALIAS_CACHES" ]]; then
6
    grep-flags-available() {
Marc Cornellà's avatar
Marc Cornellà committed
7
        command grep "$@" "" &>/dev/null <<< ""
mattmc3's avatar
mattmc3 committed
8
    }
nXqd's avatar
nXqd committed
9

Marc Cornellà's avatar
Marc Cornellà committed
10
    # Ignore these folders (if the necessary grep flags are available)
mattmc3's avatar
mattmc3 committed
11
    EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
12

Marc Cornellà's avatar
Marc Cornellà committed
13
14
15
    # Check for --exclude-dir, otherwise check for --exclude. If --exclude
    # isn't available, --color won't be either (they were released at the same
    # time (v2.5): http://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
16
    if grep-flags-available --color=auto --exclude-dir=.cvs; then
Marc Cornellà's avatar
Marc Cornellà committed
17
        GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
18
    elif grep-flags-available --color=auto --exclude=.cvs; then
Marc Cornellà's avatar
Marc Cornellà committed
19
        GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
mattmc3's avatar
mattmc3 committed
20
    fi
21

mattmc3's avatar
mattmc3 committed
22
    {
23
24
        if [[ -n "$GREP_OPTIONS" ]]; then
            # export grep, egrep and fgrep settings
Marc Cornellà's avatar
Marc Cornellà committed
25
26
27
            echo "alias grep='grep $GREP_OPTIONS'"
            echo "alias egrep='egrep $GREP_OPTIONS'"
            echo "alias fgrep='fgrep $GREP_OPTIONS'"
28
        fi
Marc Cornellà's avatar
Marc Cornellà committed
29
    } > "$__GREP_CACHE_FILE"
Marc Cornellà's avatar
Marc Cornellà committed
30

Marc Cornellà's avatar
Marc Cornellà committed
31
    # Clean up
mattmc3's avatar
mattmc3 committed
32
    unset GREP_OPTIONS EXC_FOLDERS
33
    unfunction grep-flags-available
mattmc3's avatar
mattmc3 committed
34
fi
Marc Cornellà's avatar
Marc Cornellà committed
35
36
37

source "$__GREP_CACHE_FILE"
unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES