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 5 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
54 changes: 40 additions & 14 deletions counsel.el
Original file line number Diff line number Diff line change
Expand Up @@ -1438,22 +1438,48 @@ 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)))))

(rx-define grouped-regex-matches
basil-conto marked this conversation as resolved.
Show resolved Hide resolved
(seq string-start (group (zero-or-more any))
":" (group (+ digit)) ":"
(group (* any)) string-end))

(defun counsel--git-grep-file-and-line-number (x)
"Returns a cons cell of file-name and line number"
(when (string-match (rx grouped-regex-matches) x)
(cons (match-string-no-properties 1 x) (match-string-no-properties 2 x))))
basil-conto marked this conversation as resolved.
Show resolved Hide resolved

(defun counsel--git-grep-go-to-location (line-number)
"Go to line-number within current buffer"
basil-conto marked this conversation as resolved.
Show resolved Hide resolved
(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))))

(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."
Expand Down
Loading