From fec736f7f62a5747dd46f07c4374cf8700862ac4 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 7 Jan 2015 03:34:22 +0100 Subject: [PATCH 1/5] Setup `grep` alias lazily through a function This saves an extra / initial call to `grep` during shell startup. --- lib/grep.zsh | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index 348ebe623..9a2c6f5a3 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -1,24 +1,25 @@ -# is x grep argument available? -grep-flag-available() { - echo | grep $1 "" >/dev/null 2>&1 -} - -# color grep results -GREP_OPTIONS="--color=auto" - -# ignore VCS folders (if the necessary grep flags are available) +# Ignore VCS folders (if the necessary grep flags are available). VCS_FOLDERS="{.bzr,.cvs,.git,.hg,.svn}" -if grep-flag-available --exclude-dir=.cvs; then - GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS" -elif grep-flag-available --exclude=.cvs; then - GREP_OPTIONS+=" --exclude=$VCS_FOLDERS" -fi +_setup_grep_alias() { + # Is grep argument $1 available? + grep-flag-available() { + echo | grep $1 "" >/dev/null 2>&1 + } -# export grep settings -alias grep="grep $GREP_OPTIONS" + # Color grep results. + local GREP_OPTIONS="--color=auto" -# clean up -unset GREP_OPTIONS -unset VCS_FOLDERS -unfunction grep-flag-available + if grep-flag-available --exclude-dir=.cvs; then + GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS" + elif grep-flag-available --exclude=.cvs; then + GREP_OPTIONS+=" --exclude=$VCS_FOLDERS" + fi + + # Re-define alias. + alias grep="grep $GREP_OPTIONS" + + # Clean up. + unfunction grep-flag-available +} +alias grep=_setup_grep_alias From 4cfcf09cefbcd50e6aab809fa158bfd25bdde547 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 7 Jan 2015 14:40:28 +0100 Subject: [PATCH 2/5] grep: use array for options --- lib/grep.zsh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index 9a2c6f5a3..f36970f32 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -7,13 +7,14 @@ _setup_grep_alias() { echo | grep $1 "" >/dev/null 2>&1 } + local GREP_OPTIONS # Color grep results. - local GREP_OPTIONS="--color=auto" + GREP_OPTIONS=(--color=auto) if grep-flag-available --exclude-dir=.cvs; then - GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS" + GREP_OPTIONS+=(--exclude-dir=$VCS_FOLDERS) elif grep-flag-available --exclude=.cvs; then - GREP_OPTIONS+=" --exclude=$VCS_FOLDERS" + GREP_OPTIONS+=(--exclude=$VCS_FOLDERS) fi # Re-define alias. From b88039e0f49e69665fe7c33db7aa9a2ba0946ac3 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 7 Jan 2015 14:40:43 +0100 Subject: [PATCH 3/5] grep: run it on first invocation --- lib/grep.zsh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/grep.zsh b/lib/grep.zsh index f36970f32..c7e8bbffc 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -22,5 +22,8 @@ _setup_grep_alias() { # Clean up. unfunction grep-flag-available + + # Run it on first invocation. + command grep $GREP_OPTIONS "$@" } alias grep=_setup_grep_alias From 330f02a245cfe879632341071084a5f12483b37f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 7 Jan 2015 15:11:30 +0100 Subject: [PATCH 4/5] grep: keep grep-flag-available together --- lib/grep.zsh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index c7e8bbffc..af78c3d7d 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -2,15 +2,14 @@ VCS_FOLDERS="{.bzr,.cvs,.git,.hg,.svn}" _setup_grep_alias() { - # Is grep argument $1 available? - grep-flag-available() { - echo | grep $1 "" >/dev/null 2>&1 - } - local GREP_OPTIONS # Color grep results. GREP_OPTIONS=(--color=auto) + # Is grep argument $1 available? + grep-flag-available() { + echo | grep $1 "" >/dev/null 2>&1 + } if grep-flag-available --exclude-dir=.cvs; then GREP_OPTIONS+=(--exclude-dir=$VCS_FOLDERS) elif grep-flag-available --exclude=.cvs; then From bd505c9db33aa68b3215abec20b15b453710987c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 7 Jan 2015 15:14:50 +0100 Subject: [PATCH 5/5] grep: use a function instead of aliases, and auto-add '-r' for dirs --- lib/grep.zsh | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index af78c3d7d..8d72d3bb3 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -15,14 +15,23 @@ _setup_grep_alias() { elif grep-flag-available --exclude=.cvs; then GREP_OPTIONS+=(--exclude=$VCS_FOLDERS) fi - - # Re-define alias. - alias grep="grep $GREP_OPTIONS" - # Clean up. unfunction grep-flag-available + # Remove alias and setup function. + unalias grep + setopt localoptions norcexpandparam + eval "grep() { + local options + options=(${(@)GREP_OPTIONS}) + # Add '-r' if grepping a dir. + if [[ -d \$@[\$#] ]]; then + options+=(-r) + fi + command grep \$options \"\$@\" + }" + # Run it on first invocation. - command grep $GREP_OPTIONS "$@" + grep $GREP_OPTIONS "$@" } alias grep=_setup_grep_alias