.vimrc: Enhanced strip-trailing-whitespace
[dotfiles.git] / bin / ack-wrapper
index 49e53867b9f3c88e156208af48618a996ffe3927..a542bc6cf62cb38891283d8b13672817fbc7c44e 100755 (executable)
@@ -1,26 +1,44 @@
 #!/bin/sh
-# Wrapper around 'ack' to crawl all directories from $PWD to / (or $HOME)
+# Wrapper around 'ack' to crawl all directories from `pwd` to / (or $HOME)
 # looking for a "local" .ackrc file. Useful for setting project-level
 # --ignore-dir settings.
 
-PWD=$(pwd)
+# Search for "local" .ackrc file in CWD or any parents
 HOME=~
-ACKARGS=""
-
+ackrc=""
 match=""
-dir="$PWD"
-while [ "$dir" != "/" -a "$match" = "" ]; do
-    if [ -e "$dir/.ackrc" ]; then
-        if [ "$dir" != "$HOME" ]; then
+dir=$(pwd)
+while [ "${dir}" != "/" -a "${match}" = "" ]; do
+    if [ -e "${dir}/.ackrc" ]; then
+        if [ "${dir}" != "${HOME}" ]; then
             match=1
             echo "(Include: ${dir}/.ackrc)"
-            ACKARGS="$(egrep "^[^#]" "${dir}/.ackrc" | xargs) $ACKARGS"
+            ackrc=$(egrep "^[^#]" "${dir}/.ackrc" | tr '\n' ' ')
         else
             match=0
         fi
     else
-        dir=$(dirname "$dir")
+        dir=$(dirname "${dir}")
+    fi
+done
+
+# Add quote-wrapping for any additional args to ensure proper passing to
+# real 'ack'.
+for arg in "$@"; do
+    if [ -z "${ackrc}" ]; then
+        ackrc="'${arg}'"
+    else
+        ackrc="${ackrc} '${arg}'"
     fi
 done
 
-command ack $ACKARGS $*
+# Build command to eval
+cmd="command ack ${ackrc}"
+if [ ! -t 0 ]; then
+    # If stdin is a pipe, use cat to redirect stdin to stdout and pipe
+    # that data into ack.
+    cmd="cat | ${cmd}"
+fi
+
+# Eval the final command
+eval "${cmd}"