Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for opening counsel-git-grep in other window #3044

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions counsel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1429,23 +1429,36 @@ This function should set `ivy--old-re'."
"Grep in the current Git repository for STRING."
(or
(ivy-more-chars)
(progn
(counsel--async-command
(concat
(funcall counsel-git-grep-cmd-function string)
(if (ivy--case-fold-p string) " -i" "")))
nil)))
(ignore
(counsel--async-command
(concat
(funcall counsel-git-grep-cmd-function string)
(and (ivy--case-fold-p string) " -i"))))))

(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)))
(find-file (expand-file-name
file-name
(ivy-state-directory ivy-last)))
(counsel--git-grep-visit x))

(defun counsel-git-grep-action-other-window (x)
"Go to occurrence X in current Git repository in another window."
(counsel--git-grep-visit x t))

(defun counsel--git-grep-file-and-line (x)
"Extract file name and line number from `counsel-git-grep' line X.
Return a pair (FILE . LINE) on success; nil otherwise."
(and (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-visit (cand &optional other-window)
"Visit `counsel-git-grep' CAND, optionally in OTHER-WINDOW."
(let ((file-and-line (counsel--git-grep-file-and-line cand)))
(when file-and-line
(funcall (if other-window #'find-file-other-window #'find-file)
(expand-file-name (car file-and-line)
(ivy-state-directory ivy-last)))
(goto-char (point-min))
(forward-line (1- (string-to-number line-number)))
(forward-line (1- (cdr file-and-line)))
(when (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
(when swiper-goto-start-of-match
(goto-char (match-beginning 0))))
Expand All @@ -1455,6 +1468,10 @@ This function should set `ivy--old-re'."
(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."
(when (string-match "\\`\\([^:]+\\):\\([^:]+\\):" str)
Expand Down