[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))
- [elpa] master 2237916 223/348: ivy.el (ivy-call): setq default-directory instead of let, (continued)
- [elpa] master 2237916 223/348: ivy.el (ivy-call): setq default-directory instead of let, Oleh Krehel, 2017/04/08
- [elpa] master 1d97984 227/348: Ensure that ivy--regex-ignore-order always returns legal regexps, Oleh Krehel, 2017/04/08
- [elpa] master 7e02815 235/348: Fix counsel-rg to follow matching candidate, Oleh Krehel, 2017/04/08
- [elpa] master 530b7de 246/348: ivy.el: Use predicate when reading file names, Oleh Krehel, 2017/04/08
- [elpa] master 1c801a0 243/348: Ignore TAGS buffers in swiper-all, Oleh Krehel, 2017/04/08
- [elpa] master 6519ecc 248/348: ivy.el (ivy-call): Don't mess with default-directory, Oleh Krehel, 2017/04/08
- [elpa] master 721db4c 244/348: counsel.el (counsel-grep-post-action-hook): New hook for counsel-ag/grep/pt, Oleh Krehel, 2017/04/08
- [elpa] master 49acd4c 242/348: Remap scroll-up/down-command instead of assigning C-v/M-v so page up/down works, Oleh Krehel, 2017/04/08
- [elpa] master c322613 240/348: ivy.el (ivy--magic-file-slash): Fix pasting "/etc/", Oleh Krehel, 2017/04/08
- [elpa] master 1304925 239/348: ivy.el (ivy--directory-done): Fix broken "/ssh:", Oleh Krehel, 2017/04/08
- [elpa] master 359a6c8 241/348: Address multiple problems with counsel-rg,
Oleh Krehel <=
- [elpa] master 2ed3da4 252/348: counsel.el: Fix byte-compile warning, Oleh Krehel, 2017/04/08
- [elpa] master 09da543 253/348: swiper.el: Ease the creation of swiper-in-region functions, Oleh Krehel, 2017/04/08
- [elpa] master 3940f17 266/348: Fix typos, Oleh Krehel, 2017/04/08
- [elpa] master 25ac29f 079/348: ivy.el (ivy-completion-in-region): Avoid error for 0 candidates, Oleh Krehel, 2017/04/08
- [elpa] master 0572f16 123/348: swiper now respects ivy-re-builders-alist, Oleh Krehel, 2017/04/08
- [elpa] master d528f04 260/348: ivy.el (ivy--virtual-buffers): Don't modify recentfs or bookmarks, Oleh Krehel, 2017/04/08
- [elpa] master ed579dd 274/348: ivy.el: Fix ivy-highlight-functions-alist, Oleh Krehel, 2017/04/08
- [elpa] master 9151862 277/348: ivy.el (ivy--format-minibuffer-line): Fix for ivy-display-style nil, Oleh Krehel, 2017/04/08
- [elpa] master 6cf7feb 282/348: ivy.el: Allow toggling of sorting in ivy-read, Oleh Krehel, 2017/04/08
- [elpa] master dbcee94 288/348: counsel.el: support universal argument for counsel-unicode-char, Oleh Krehel, 2017/04/08