summaryrefslogtreecommitdiffstats
path: root/lisp/init-grep.el
blob: 4b0d0ca3d8b8d6ca7e2e8d3e1e2bcc42fb01371b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;;; init-grep.el --- configure grep  -*- lexical-binding:t -*-
;;; Commentary:
;;; Code:

(defvar mpolden/rg-template "rg -nH --no-heading <C> -e <R> -- "
  "The grep template to use when rg (ripgrep) is installed.")

(defun mpolden/grep ()
  "Run grep interactively in `default-directory' or current VC tree."
  (interactive)
  (let ((vc-root-dir (vc-root-dir)))
    (if vc-root-dir
        (vc-git-grep (grep-read-regexp) "" vc-root-dir)
      (lgrep (grep-read-regexp) "" default-directory))))

(defun mpolden/grep-visit-buffer-other-window (&optional result noselect)
  "Visit grep RESULT in another window.
If NOSELECT is non-nil, do not select the window."
  (interactive)
  (let ((current-window (selected-window)))
    (compile-goto-error result)
    (when noselect
      (select-window current-window))))

(defun mpolden/grep-visit-buffer-other-window-noselect (&optional result)
  "Visit grep RESULT another window, but don't select it."
  (interactive)
  (mpolden/grep-visit-buffer-other-window result t))

(use-package grep
  :commands grep-read-regexp
  :config
  (when (executable-find "rg")
    ;; auto-detection of -H may fail when using rg so explicitly never use null
    ;; device
    (setq grep-use-null-device nil)
    (grep-apply-setting 'grep-template mpolden/rg-template))
  :bind (;; C-c g runs git grep in current vc tree
         ("C-c g" . mpolden/grep)
         :map grep-mode-map
         ;; make C-o and o behave as in dired
         ("o" . mpolden/grep-visit-buffer-other-window)
         ("C-o" . mpolden/grep-visit-buffer-other-window-noselect)
         ;; n and p changes line as in ag-mode
         ("n" . compilation-next-error)
         ("p" . compilation-previous-error)))

(use-package vc-git
  :commands vc-git-grep
  :init
  (when (executable-find "rg")
    (setq vc-git-grep-template mpolden/rg-template)))

(provide 'init-grep)

;;; init-grep.el ends here