grep.zsh 1.4 KB

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