461b2134ded0b698c41b21be847aa4b16362ae7f

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Merge branch 'grep.zsh-improvements'

Closes #5085
Closes #7451
Closes #7265
Fixes #8444
Closes #8445

Diff

 1diff --git a/lib/grep.zsh b/lib/grep.zsh
 2index abc1650a1cca6f117bb1d212a8501d53a06f83a7..1d94eb97895bbf94f567c2f164d16caac921fe34 100644
 3--- a/lib/grep.zsh
 4+++ b/lib/grep.zsh
 5@@ -1,28 +1,37 @@
 6-# is x grep argument available?
 7-grep-flag-available() {
 8-    echo | grep $1 "" >/dev/null 2>&1
 9-}
10+__GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias
11 
12-GREP_OPTIONS=""
13+# See if there's a cache file modified in the last day
14+__GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
15+if [[ -z "$__GREP_ALIAS_CACHES" ]]; then
16+    grep-flags-available() {
17+        command grep "$@" "" &>/dev/null <<< ""
18+    }
19 
20-# color grep results
21-if grep-flag-available --color=auto; then
22-    GREP_OPTIONS+=" --color=auto"
23-fi
24+    # Ignore these folders (if the necessary grep flags are available)
25+    EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
26 
27-# ignore VCS folders (if the necessary grep flags are available)
28-VCS_FOLDERS="{.bzr,CVS,.git,.hg,.svn}"
29+    # Check for --exclude-dir, otherwise check for --exclude. If --exclude
30+    # isn't available, --color won't be either (they were released at the same
31+    # time (v2.5): http://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
32+    if grep-flags-available --color=auto --exclude-dir=.cvs; then
33+        GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
34+    elif grep-flags-available --color=auto --exclude=.cvs; then
35+        GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
36+    fi
37 
38-if grep-flag-available --exclude-dir=.cvs; then
39-    GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS"
40-elif grep-flag-available --exclude=.cvs; then
41-    GREP_OPTIONS+=" --exclude=$VCS_FOLDERS"
42-fi
43+    {
44+        if [[ -n "$GREP_OPTIONS" ]]; then
45+            # export grep, egrep and fgrep settings
46+            echo "alias grep='grep $GREP_OPTIONS'"
47+            echo "alias egrep='egrep $GREP_OPTIONS'"
48+            echo "alias fgrep='fgrep $GREP_OPTIONS'"
49+        fi
50+    } > "$__GREP_CACHE_FILE"
51 
52-# export grep settings
53-alias grep="grep $GREP_OPTIONS"
54+    # Clean up
55+    unset GREP_OPTIONS EXC_FOLDERS
56+    unfunction grep-flags-available
57+fi
58 
59-# clean up
60-unset GREP_OPTIONS
61-unset VCS_FOLDERS
62-unfunction grep-flag-available
63+source "$__GREP_CACHE_FILE"
64+unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES