Parent directory

grep.zsh

1454 bytes
 1__GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias
 2
 3# See if there's a cache file modified in the last day
 4__GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
 5if [[ -n "$__GREP_ALIAS_CACHES" ]]; then
 6    source "$__GREP_CACHE_FILE"
 7else
 8    grep-flags-available() {
 9        command grep "$@" "" &>/dev/null <<< ""
10    }
11
12    # Ignore these folders (if the necessary grep flags are available)
13    EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox,.venv,venv}"
14
15    # Check for --exclude-dir, otherwise check for --exclude. If --exclude
16    # isn't available, --color won't be either (they were released at the same
17    # time (v2.5): https://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
18    if grep-flags-available --color=auto --exclude-dir=.cvs; then
19        GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
20    elif grep-flags-available --color=auto --exclude=.cvs; then
21        GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
22    fi
23
24    if [[ -n "$GREP_OPTIONS" ]]; then
25        # export grep, egrep and fgrep settings
26        alias grep="grep $GREP_OPTIONS"
27        alias egrep="grep -E"
28        alias fgrep="grep -F"
29
30        # write to cache file if cache directory is writable
31        if [[ -w "$ZSH_CACHE_DIR" ]]; then
32            alias -L grep egrep fgrep >| "$__GREP_CACHE_FILE"
33        fi
34    fi
35
36    # Clean up
37    unset GREP_OPTIONS EXC_FOLDERS
38    unfunction grep-flags-available
39fi
40
41unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES