                                                (symbol-name (symbol-at-point))))))
    (ag/search string (ag/project-root default-directory)))
 
+;; Taken from grep-filter, just changed the color regex.
+(defun ag-filter ()
+  "Handle match highlighting escape sequences inserted by the grep process.
+This function is called from `compilation-filter-hook'."
+  (save-excursion
+    (forward-line 0)
+    (let ((end (point)) beg)
+      (goto-char compilation-filter-start)
+      (forward-line 0)
+      (setq beg (point))
+      ;; Only operate on whole lines so we don't get caught with part of an
+      ;; escape sequence in one chunk and the rest in another.
+      (when (< (point) end)
+        (setq end (copy-marker end))
+        ;; Highlight grep matches and delete marking sequences.
+        (while (re-search-forward "\033\\[30;43m\\(.*?\\)\033\\[[0-9]*m" end 1)
+          (replace-match (propertize (match-string 1)
+                                     'face nil 'font-lock-face ag-match-face)
+                         t t))
+        ;; Delete all remaining escape sequences
+        (goto-char beg)
+        (while (re-search-forward "\033\\[[0-9;]*[mK]" end 1)
+          (replace-match "" t t))))))
+
 (provide 'ag)

;; Boston, MA 02110-1301, USA.

(defcustom ag-arguments
  (list "--nocolor" "--literal" "--smart-case" "--nogroup" "--column")
  "Default arguments passed to ag."
  :type '(repeat (string)))

(require 'compile)

(define-compilation-mode ag-mode "Ag"
  "Ag results compilation mode")

(defun ag/s-join (separator strings)
  "Join all the strings in STRINGS with SEPARATOR in between."

  "Wrap in single quotes, and quote existing single quotes to make shell safe."
  (concat "'" (ag/s-replace "'" "'\\''" string) "'"))

(defun ag/search (string directory &optional regexp)
  "Run ag searching for the STRING given in DIRECTORY. If REGEXP
is non-nil, treat STRING as a regular expression."
  (let ((default-directory (file-name-as-directory directory))
        (arguments (if regexp
                       ag-arguments
                     (cons "--literal" ag-arguments))))
    (unless (file-exists-p default-directory)
      (error "No such directory %s" default-directory))
    (compilation-start
     (ag/s-join " "
                (append '("ag") arguments (list (ag/shell-quote string))))
     'ag-mode)))

(autoload 'vc-git-root "vc-git")
