branch: master
commit 225eb2f75a544b331d3cd06c2bd70ad97467ef39
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>

    counsel.el (counsel-grep-like-occur): Add
    
    * counsel.el (counsel-ag-occur): Update.
    (counsel-rg-occur): Update.
    (counsel-grep-occur): Update.
    
    This fixes the (inconvenient and inconsistent) approach of using the cached
    search result for `ivy-occur'. With the old approach, it was impossible to 
run a
    search, save to *ivy-occur*, change and save the buffer(s), and re-run the
    search from *ivy-occur* with "g". Now this ability is back.
    
    Sample use:
        1. `counsel-rg' on a project for a variable to remove/refactor.
        2. save the search to *ivy-occur*.
        3. rewrite file1 and save it.
        4. press "g" in *ivy-occur* to refresh.
        5. rewrite file2 and save it.
        ...
    
    Another issue that's fixed here is the removal of `rg' column limiting by 
width.
    I use this setting:
    
        (setq counsel-rg-base-command
              "rg -i -M 120 --no-heading --line-number --color never %s .")
    
    This means that lines longer than 120 will be clobbered by `rg' in order to
    limit search output and speed it up.  But when saving to *ivy-occur*, I 
don't
    want those lines to be clobbered any more.
---
 counsel.el | 34 +++++++++++++++++++++++++++++++---
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/counsel.el b/counsel.el
index 32973c4..2a3b10e 100644
--- a/counsel.el
+++ b/counsel.el
@@ -2082,9 +2082,30 @@ AG-PROMPT, if non-nil, is passed as `ivy-read' prompt 
argument."
                       (swiper--cleanup))
             :caller 'counsel-ag))
 
+(defun counsel-grep-like-occur (cmd-template)
+  (unless (eq major-mode 'ivy-occur-grep-mode)
+    (ivy-occur-grep-mode)
+    (setq default-directory counsel--git-dir))
+  (setq ivy-text
+        (and (string-match "\"\\(.*\\)\"" (buffer-name))
+             (match-string 1 (buffer-name))))
+  (let* ((cmd (format cmd-template
+                      (counsel-unquote-regex-parens
+                       (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
+     (mapcar
+      (lambda (cand) (concat "./" cand))
+      cands))))
+
 (defun counsel-ag-occur ()
   "Generate a custom occur buffer for `counsel-ag'."
-  (counsel--grep-mode-occur nil))
+  (counsel-grep-like-occur
+   "ag --nocolor --nogroup %s"))
 
 ;;** `counsel-pt'
 (defcustom counsel-pt-base-command "pt --nocolor --nogroup -e %s"
@@ -2166,7 +2187,8 @@ RG-PROMPT, if non-nil, is passed as `ivy-read' prompt 
argument."
 
 (defun counsel-rg-occur ()
   "Generate a custom occur buffer for `counsel-rg'."
-  (counsel--grep-mode-occur nil))
+  (counsel-grep-like-occur
+   "rg -i --no-heading --line-number --color never \"%s\" ."))
 
 ;;** `counsel-grep'
 (defcustom counsel-grep-base-command "grep -nE '%s' %s"
@@ -2224,7 +2246,13 @@ RG-PROMPT, if non-nil, is passed as `ivy-read' prompt 
argument."
 
 (defun counsel-grep-occur ()
   "Generate a custom occur buffer for `counsel-grep'."
-  (counsel--grep-mode-occur t))
+  (counsel-grep-like-occur
+   (format
+    "grep -niE '%%s' %s /dev/null"
+    (shell-quote-argument
+     (file-name-nondirectory
+      (buffer-file-name
+       (ivy-state-buffer ivy-last)))))))
 
 (ivy-set-occur 'counsel-grep 'counsel-grep-occur)
 (counsel-set-async-exit-code 'counsel-grep 1 "")

Reply via email to