;;; -*- lexical-binding: t -*-

;; I don't like that `grep' asks me to save unsaved files.  It makes
;; me think it's about to kill my buffers.
(setq grep-save-buffers nil)

;; rgrep-compatibility stolen from Faye }:P.
(when (executable-find "rg")
  ;; Use rgrep-compatible flags for exclusion
  (with-eval-after-load 'grep
    (add-to-list
     'grep-expand-keywords
     '("<X>" .
       (when-let* ((ignores (grep-find-ignored-files dir)))
         (concat " --glob="
                 (mapconcat
                  (lambda (ignore)
                    (shell-quote-argument (concat "!" ignore)
                                          grep-quoting-style))
                  ignores
                  " --glob=")))))

    ;; Hide ripgrep exclusion flags
    (add-to-list
     'grep-mode-font-lock-keywords
     '("^rg \\( *--glob.*--glob[^ ]+\\)"
       (1 (if grep-find-abbreviate grep-find-abbreviate-properties
  	    '(face nil abbreviated-command t)))))

    ;; Populate rg cmdline for grep-template and grep-find-template
    (grep-apply-setting
     'grep-template
     "rg <X> --hidden -d 1 --no-heading -H -uu -g <F> <R> .")
    (grep-apply-setting
     'grep-find-template
     "rg <X> --hidden --no-heading -H -uu <R> <D>")))

(provide 'syd/grep)
