cp-grep: Simplify

This commit is contained in:
Eric Danan 2018-12-26 15:12:57 +01:00
parent 5f554cdca7
commit b3ea4f242d

View file

@ -647,9 +647,7 @@ construct the command.")
"Grep for STRING in the current project."
(or (counsel-more-chars)
(let ((default-directory (ivy-state-directory ivy-last))
(regex (counsel--elisp-to-pcre
(setq ivy--old-re
(ivy--regex string)))))
(regex (counsel--grep-regex string)))
(counsel--async-command (format counsel-projectile-grep-command
(shell-quote-argument regex)))
nil)))
@ -671,24 +669,8 @@ construct the command.")
(defun counsel-projectile-grep-occur ()
"Generate a custom occur buffer for `counsel-projectile-grep'."
;; Copied from `counsel-grep-like-occur', except that we don't
;; prepend "./" to the candidates since grep already does so.
(unless (eq major-mode 'ivy-occur-grep-mode)
(ivy-occur-grep-mode)
(setq default-directory (ivy-state-directory ivy-last)))
(setq ivy-text
(and (string-match "\"\\(.*\\)\"" (buffer-name))
(match-string 1 (buffer-name))))
(let* ((cmd (format counsel-projectile-grep-command
(shell-quote-argument
(counsel--elisp-to-pcre
(ivy--regex ivy-text)))))
(cands (split-string (shell-command-to-string cmd) "\n" t)))
;; Need precise number of header lines for `wgrep' to work.
(insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
default-directory))
(insert (format "%d candidates:\n" (length cands)))
(ivy--occur-insert-lines cands)))
(counsel-grep-like-occur
counsel-projectile-grep-command))
;;;###autoload
(defun counsel-projectile-grep (&optional options-or-cmd)
@ -743,6 +725,7 @@ called with a prefix argument."
(swiper--cleanup))
:caller 'counsel-projectile-grep)))))
(cl-pushnew 'counsel-projectile-grep ivy-highlight-grep-commands)
(counsel-set-async-exit-code 'counsel-projectile-grep 1 "No matches found")
(ivy-set-occur 'counsel-projectile-grep 'counsel-projectile-grep-occur)
(ivy-set-display-transformer 'counsel-projectile-grep 'counsel-projectile-grep-transformer)