Skip to content

Commit

Permalink
consult-grep: add consult-grep-directory-hook
Browse files Browse the repository at this point in the history
This allows the grep commands to use the current project root
if that is desired.
  • Loading branch information
minad committed Dec 29, 2020
1 parent ee561cb commit f8df1b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Most provided commands follow the naming scheme `consult-<thing>`.
input is used in order to use the *fast* Emacs filtering to further narrow
down the list of matches. `consult-grep` supports preview.
* `consult-ripgrep`: **EXPERIMENTAL** Search in current directory, like
`consult-grep`. similarily to `consult-grep`.
`consult-grep`.
* `consult-git-grep`: **EXPERIMENTAL** Search in current git repository, like
`consult-grep`.

Expand Down Expand Up @@ -244,6 +244,7 @@ that the main package `consult.el` only depends on Emacs core components.
|------------------------------|------------------|---------------------------------------------------------|
| consult-after-jump-hook | '(recenter) | Functions to call after jumping to a location |
| consult-goto-line-numbers | t | Show line numbers for `consult-goto-line` |
| consult-grep-directory-hook || Return directory to use for grep |
| consult-grep-min-input | 3 | Minimum numbers of letters needed for grep |
| consult-imenu-narrow || Narrowing keys for imenu |
| consult-line-numbers-widen | t | Show absolute line numbers when narrowing is active. |
Expand All @@ -257,7 +258,7 @@ that the main package `consult.el` only depends on Emacs core components.
| consult-preview-flycheck | t | Enable flycheck error preview during selection |
| consult-preview-flymake | t | Enable flymake diagnostic preview during selection |
| consult-preview-global-mark | t | Enable global mark preview during selection |
| consult-preview-grep | t | Enable grep preview during selection |
| consult-preview-grep | t | Enable grep preview during selection |
| consult-preview-line | t | Enable line preview during selection |
| consult-preview-mark | t | Enable mark preview during selection |
| consult-preview-outline | t | Enable outline preview during selection |
Expand Down
41 changes: 26 additions & 15 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ If this key is unset, defaults to 'consult-narrow-key SPC'."
"Minimum number of letters which must be entered, before grep is called."
:type 'integer)

(defcustom consult-grep-directory-hook (list (lambda () default-directory))
"Return directory to use for `consult-grep'."
:type 'hook)

(defcustom consult-mode-histories
'((eshell-mode . eshell-history-ring)
(comint-mode . comint-input-ring)
Expand Down Expand Up @@ -1927,17 +1931,17 @@ Prepend PREFIX in front of all items."
(save-match-data
(dolist (str lines)
(when (string-match consult--grep-regexp str)
(let* ((file (consult--strip-ansi-escape (match-string 1 str)))
(let* ((file (expand-file-name (consult--strip-ansi-escape (match-string 1 str))))
(line (string-to-number (consult--strip-ansi-escape (match-string 2 str))))
(str (substring str (match-end 0)))
(loc (consult--format-location file line)))
(loc (consult--format-location (file-relative-name file) line)))
(while (string-match consult--grep-match-regexp str)
(setq str (concat (substring str 0 (match-beginning 0))
(propertize (substring (match-string 1 str)) 'face 'consult-preview-match)
(substring str (match-end 0)))))
(setq str (consult--strip-ansi-escape str))
(push (list (concat loc str)
(expand-file-name file) line
file line
(next-single-char-property-change 0 'face str))
candidates)))))
(nreverse candidates)))
Expand Down Expand Up @@ -1993,23 +1997,30 @@ PROMPT is the prompt string."
:history '(:input consult--grep-history)
:sort nil))))

(defsubst consult--grep-directory ()
"Return grep directory."
(run-hook-with-args-until-success 'consult-grep-directory-hook))

;;;###autoload
(defun consult-grep ()
"Search for REGEXP with grep."
(interactive)
(consult--grep "Grep: " consult--grep-command))
(defun consult-grep (dir)
"Search for REGEXP with grep in DIR."
(interactive (list (consult--grep-directory)))
(let ((default-directory dir))
(consult--grep "Grep: " consult--grep-command)))

;;;###autoload
(defun consult-git-grep ()
"Search for REGEXP with grep."
(interactive)
(consult--grep "Git Grep: " consult--git-grep-command))
(defun consult-git-grep (dir)
"Search for REGEXP with grep in DIR."
(interactive (list (consult--grep-directory)))
(let ((default-directory dir))
(consult--grep "Git Grep: " consult--git-grep-command)))

;;;###autoload
(defun consult-ripgrep ()
"Search for REGEXP with rg."
(interactive)
(consult--grep "Ripgrep: " consult--ripgrep-command))
(defun consult-ripgrep (dir)
"Search for REGEXP with rg in DIR."
(interactive (list (consult--grep-directory)))
(let ((default-directory dir))
(consult--grep "Ripgrep: " consult--ripgrep-command)))

;;;; default completion-system support

Expand Down

0 comments on commit f8df1b1

Please sign in to comment.