]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/lib/grep.zsh
54e0f694e7d7a86ead2020907d88ddaf32861522
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / lib / grep.zsh
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))
5 if [[ -n "$__GREP_ALIAS_CACHES" ]]; then
6     source "$__GREP_CACHE_FILE"
7 else
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}"
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 $GREP_OPTIONS"
28         alias fgrep="grep -F $GREP_OPTIONS"
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
39 fi
40
41 unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES