]> code.delx.au - gnu-emacs-elpa/commitdiff
counsel.el (counsel-ag): New command
authorOleh Krehel <ohwoeowho@gmail.com>
Mon, 27 Jul 2015 11:09:10 +0000 (13:09 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Mon, 27 Jul 2015 11:11:30 +0000 (13:11 +0200)
* counsel.el (counsel-ag-function): New defun.
(counsel-git-grep): Update prompt.

Going from sync to async now is as simple as:

- add :dynamic-collection t
- replace `shell-command-to-string' with `counsel--async-command'

counsel.el

index 161d789f8f5f00d03bffa113a435b2a5399b68cb..cb87e8a80f9a877a28f594a69395ef9ef6301f65 100644 (file)
@@ -332,7 +332,7 @@ INITIAL-INPUT can be given as the initial minibuffer input."
   (if (null counsel--git-grep-dir)
       (error "Not in a git repository")
     (setq counsel--git-grep-count (counsel--gg-count "" t))
-    (ivy-read "pattern: " 'counsel-git-grep-function
+    (ivy-read "git grep: " 'counsel-git-grep-function
               :initial-input initial-input
               :matcher #'counsel-git-grep-matcher
               :dynamic-collection (> counsel--git-grep-count 20000)
@@ -826,6 +826,31 @@ Usable with `ivy-resume', `ivy-next-line-and-call' and
               :history 'org-tags-history
               :action 'counsel-org-tag-action)))
 
+(defun counsel-ag-function (string &optional _pred &rest _unused)
+  "Grep in the current directory for STRING."
+  (if (< (length string) 3)
+      (counsel-more-chars 3)
+    (let ((regex (replace-regexp-in-string
+                  "\\\\)" ")"
+                  (replace-regexp-in-string
+                   "\\\\(" "("
+                   (ivy--regex string)))))
+      (counsel--async-command
+       (format "ag --noheading --nocolor %S" regex))
+      nil)))
+
+(defun counsel-ag (&optional initial-input)
+  "Grep for a string in the current directory using ag.
+INITIAL-INPUT can be given as the initial minibuffer input."
+  (interactive)
+  (setq counsel--git-grep-dir default-directory)
+  (ivy-read "ag: " 'counsel-ag-function
+            :initial-input initial-input
+            :dynamic-collection t
+            :history 'counsel-git-grep-history
+            :action #'counsel-git-grep-action
+            :unwind #'swiper--cleanup))
+
 (provide 'counsel)
 
 ;;; counsel.el ends here