branch: externals/ivy
commit b26d3816ffd649c35356f5a114b779b4ad66ef65
Author: Christopher Floess <chris.flo...@mailbox.org>
Commit: Basil L. Contovounesios <ba...@contovou.net>

    Add support for opening git-grep in other window
    
    * counsel.el (counsel-git-grep-action): Refactor in terms of
    counsel--git-grep-file-and-line-number and
    counsel--git-grep-go-to-location.
    (counsel-git-grep-action-other-window)
    (counsel--git-grep-file-and-line-number)
    (counsel--git-grep-go-to-location): New functions.
    (counsel-git-grep): Set "j" action to
    counsel-git-grep-action-other-window (#3044).
    
    Closes #3043.
---
 counsel.el | 50 ++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 14 deletions(-)

diff --git a/counsel.el b/counsel.el
index a9f8574680..7d33ff7438 100644
--- a/counsel.el
+++ b/counsel.el
@@ -1438,22 +1438,44 @@ This function should set `ivy--old-re'."
 
 (defun counsel-git-grep-action (x)
   "Go to occurrence X in current Git repository."
-  (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
-    (let ((file-name (match-string-no-properties 1 x))
-          (line-number (match-string-no-properties 2 x)))
+  (let ((file-and-line-number (counsel--git-grep-file-and-line-number x)))
+    (when file-and-line-number
       (find-file (expand-file-name
-                  file-name
+                  (car file-and-line-number)
                   (ivy-state-directory ivy-last)))
-      (goto-char (point-min))
-      (forward-line (1- (string-to-number line-number)))
-      (when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
-        (when swiper-goto-start-of-match
-          (goto-char (match-beginning 0))))
-      (swiper--ensure-visible)
-      (run-hooks 'counsel-grep-post-action-hook)
-      (unless (eq ivy-exit 'done)
-        (swiper--cleanup)
-        (swiper--add-overlays (ivy--regex ivy-text))))))
+      (counsel--git-grep-go-to-location (cdr file-and-line-number)))))
+
+(defun counsel-git-grep-action-other-window (x)
+  "Go to occurrence X in current Git repository in another window."
+  (let ((file-and-line-number (counsel--git-grep-file-and-line-number x)))
+    (when file-and-line-number
+      (find-file-other-window (expand-file-name
+                               (car file-and-line-number)
+                               (ivy-state-directory ivy-last)))
+      (counsel--git-grep-go-to-location (cdr file-and-line-number)))))
+
+(defun counsel--git-grep-file-and-line-number (x)
+  "Extract file name and line number from `counsel-git-grep' line X.
+Return a pair (FILE . LINE) on success; nil otherwise."
+  (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x)
+    (cons (match-string-no-properties 1 x) (string-to-number 
(match-string-no-properties 2 x)))))
+
+(defun counsel--git-grep-go-to-location (line-number)
+  "Go to LINE-NUMBER within current buffer."
+  (goto-char (point-min))
+  (forward-line (1- line-number))
+  (when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
+    (when swiper-goto-start-of-match
+      (goto-char (match-beginning 0))))
+  (swiper--ensure-visible)
+  (run-hooks 'counsel-grep-post-action-hook)
+  (unless (eq ivy-exit 'done)
+    (swiper--cleanup)
+    (swiper--add-overlays (ivy--regex ivy-text))))
+
+(ivy-set-actions
+ 'counsel-git-grep
+ '(("j" counsel-git-grep-action-other-window "other window")))
 
 (defun counsel-git-grep-transformer (str)
   "Highlight file and line number in STR."

Reply via email to