emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 359a6c8 241/348: Address multiple problems with counsel-rg


From: Oleh Krehel
Subject: [elpa] master 359a6c8 241/348: Address multiple problems with counsel-rg
Date: Sat, 8 Apr 2017 11:04:06 -0400 (EDT)

branch: master
commit 359a6c869c107f66a1681baa36a3c4380a635c1b
Author: Michael Olson <address@hidden>
Commit: Oleh Krehel <address@hidden>

    Address multiple problems with counsel-rg
    
    * counsel-ag-function: Take base-cmd as a parameter instead of using 
counsel-ag-base-command
    * counsel-rg-base-command: Include line numbers and current directory so it 
works on Windows
    * Make counsel-rg a modified copy of counsel-ag instead of calling it, so 
we get a working occur buffer
    * Update docs to mention counsel-rg
    * ivy: Add 'counsel-rg to multiple memq lists that check for 'counsel-ag
    
    Fixes #795
    Fixes #796
---
 counsel.el   | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++----------
 doc/ivy.org  |  2 +-
 doc/ivy.texi |  7 +++---
 ivy.el       |  6 +++---
 4 files changed, 66 insertions(+), 18 deletions(-)

diff --git a/counsel.el b/counsel.el
index 1103cb7..0febe1e 100644
--- a/counsel.el
+++ b/counsel.el
@@ -1632,9 +1632,9 @@ regex string. The default is \"ag --nocolor --nogroup 
%s\"."
 (ivy-set-occur 'counsel-ag 'counsel-ag-occur)
 (ivy-set-display-transformer 'counsel-ag 'counsel-git-grep-transformer)
 
-(defun counsel-ag-function (string extra-ag-args)
+(defun counsel-ag-function (string base-cmd extra-ag-args)
   "Grep in the current directory for STRING.
-If non-nil, EXTRA-AG-ARGS string is appended to `counsel-ag-base-command'."
+If non-nil, EXTRA-AG-ARGS string is appended to BASE-CMD."
   (when (null extra-ag-args)
     (setq extra-ag-args ""))
   (if (< (length string) 3)
@@ -1643,7 +1643,7 @@ If non-nil, EXTRA-AG-ARGS string is appended to 
`counsel-ag-base-command'."
           (regex (counsel-unquote-regex-parens
                   (setq ivy--old-re
                         (ivy--regex string)))))
-      (let ((ag-cmd (format counsel-ag-base-command
+      (let ((ag-cmd (format base-cmd
                             (concat extra-ag-args
                                     " -- "
                                     (shell-quote-argument regex)))))
@@ -1669,7 +1669,7 @@ AG-PROMPT, if non-nil, is passed as `ivy-read' prompt 
argument. "
   (setq counsel--git-grep-dir (or initial-directory default-directory))
   (ivy-read (or ag-prompt (car (split-string counsel-ag-base-command)))
             (lambda (string)
-              (counsel-ag-function string extra-ag-args))
+              (counsel-ag-function string counsel-ag-base-command 
extra-ag-args))
             :initial-input initial-input
             :dynamic-collection t
             :keymap counsel-ag-map
@@ -1721,20 +1721,67 @@ This uses `counsel-ag' with `counsel-pt-base-command' 
replacing
     (counsel-ag initial-input)))
 
 ;;** `counsel-rg'
-(defcustom counsel-rg-base-command "rg -i --no-heading %s"
+(defcustom counsel-rg-base-command "rg -i --no-heading --line-number %s ."
   "Used to in place of `counsel-rg-base-command' to search with
 ripgrep using `counsel-rg'."
   :type 'string
   :group 'ivy)
 
+(counsel-set-async-exit-code 'counsel-rg 1 "No matches found")
+(ivy-set-occur 'counsel-rg 'counsel-rg-occur)
+(ivy-set-display-transformer 'counsel-rg 'counsel-git-grep-transformer)
+
 ;;;###autoload
-(defun counsel-rg (&optional initial-input)
+(defun counsel-rg (&optional initial-input initial-directory extra-rg-args 
rg-prompt)
   "Grep for a string in the current directory using rg.
-This uses `counsel-ag' with `counsel-rg-base-command' replacing
-`counsel-ag-base-command'."
-  (interactive)
-  (let ((counsel-ag-base-command counsel-rg-base-command))
-    (counsel-ag initial-input)))
+INITIAL-INPUT can be given as the initial minibuffer input.
+INITIAL-DIRECTORY, if non-nil, is used as the root directory for search.
+EXTRA-RG-ARGS string, if non-nil, is appended to `counsel-rg-base-command'.
+RG-PROMPT, if non-nil, is passed as `ivy-read' prompt argument. "
+  (interactive
+   (list nil
+         (when current-prefix-arg
+           (read-directory-name (concat
+                                 (car (split-string counsel-rg-base-command))
+                                 " in directory: ")))))
+  (ivy-set-prompt 'counsel-rg counsel-prompt-function)
+  (setq counsel--git-grep-dir (or initial-directory default-directory))
+  (ivy-read (or rg-prompt (car (split-string counsel-rg-base-command)))
+            (lambda (string)
+              (counsel-ag-function string counsel-rg-base-command 
extra-rg-args))
+            :initial-input initial-input
+            :dynamic-collection t
+            :keymap counsel-ag-map
+            :history 'counsel-git-grep-history
+            :action #'counsel-git-grep-action
+            :unwind (lambda ()
+                      (counsel-delete-process)
+                      (swiper--cleanup))
+            :caller 'counsel-rg))
+
+(defun counsel-rg-occur ()
+  "Generate a custom occur buffer for `counsel-rg'."
+  (unless (eq major-mode 'ivy-occur-grep-mode)
+    (ivy-occur-grep-mode))
+  (setq default-directory counsel--git-grep-dir)
+  (let* ((regex (counsel-unquote-regex-parens
+                 (setq ivy--old-re
+                       (ivy--regex
+                        (progn (string-match "\"\\(.*\\)\"" (buffer-name))
+                               (match-string 1 (buffer-name)))))))
+         (cands (split-string
+                 (shell-command-to-string
+                  (format counsel-rg-base-command (shell-quote-argument 
regex)))
+                 "\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))))
 
 ;;** `counsel-grep'
 (defcustom counsel-grep-base-command "grep -nE \"%s\" %s"
diff --git a/doc/ivy.org b/doc/ivy.org
index 8b49ed2..86099da 100644
--- a/doc/ivy.org
+++ b/doc/ivy.org
@@ -407,7 +407,7 @@ actions.
      line.
 
      Comes in handy when opening multiple files from
-     =counsel-find-file=, =counsel-git-grep=, =counsel-ag=, or
+     =counsel-find-file=, =counsel-git-grep=, =counsel-ag=, =counsel-rg=, or
      =counsel-locate= lists. Just hold ~C-M-n~ for rapid-fire default
      action on each successive element of the list.
 
diff --git a/doc/ivy.texi b/doc/ivy.texi
index 616ddb2..445627b 100644
--- a/doc/ivy.texi
+++ b/doc/ivy.texi
@@ -544,9 +544,10 @@ Combines @kbd{C-n} and @kbd{C-M-m}. Applies an action and 
moves to next
 line.
 
 Comes in handy when opening multiple files from
address@hidden, @code{counsel-git-grep}, @code{counsel-ag}, or
address@hidden lists. Just hold @kbd{C-M-n} for rapid-fire default
-action on each successive element of the list.
address@hidden, @code{counsel-git-grep}, @code{counsel-ag},
address@hidden, or @code{counsel-locate} lists. Just hold
address@hidden for rapid-fire default action on each successive element
+of the list.
 @end indentedblock
 @subsubheading @kbd{C-M-p} (@code{ivy-previous-line-and-call})
 @vindex ivy-previous-line-and-call
diff --git a/ivy.el b/ivy.el
index e940227..385a891 100644
--- a/ivy.el
+++ b/ivy.el
@@ -2773,7 +2773,7 @@ SEPARATOR is used to join the candidates."
 (defun ivy--format-minibuffer-line (str)
   (let ((start
          (if (and (memq (ivy-state-caller ivy-last)
-                        '(counsel-git-grep counsel-ag counsel-pt))
+                        '(counsel-git-grep counsel-ag counsel-rg counsel-pt))
                   (string-match "^[^:]+:[^:]+:" str))
              (match-end 0)
            0))
@@ -3431,7 +3431,7 @@ updated original buffer."
              (let ((inhibit-read-only t))
                (erase-buffer)
                (funcall (plist-get ivy--occurs-list caller) t))))
-          ((memq caller '(counsel-git-grep counsel-grep counsel-ag))
+          ((memq caller '(counsel-git-grep counsel-grep counsel-ag counsel-rg))
            (let ((inhibit-read-only t))
              (erase-buffer)
              (funcall (plist-get ivy--occurs-list caller)))))))
@@ -3486,7 +3486,7 @@ EVENT gives the mouse position."
           (beginning-of-line)
           (looking-at "\\(?:./\\|    \\)\\(.*\\)$"))
     (when (memq (ivy-state-caller ivy-occur-last)
-                '(swiper counsel-git-grep counsel-grep counsel-ag
+                '(swiper counsel-git-grep counsel-grep counsel-ag counsel-rg
                   counsel-describe-function counsel-describe-variable))
       (let ((window (ivy-state-window ivy-occur-last)))
         (when (or (null (window-live-p window))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]