branch: master
commit 5fcdfb4e13899b2ba6d0848a747d14c441a8ac6b
Author: Oleh Krehel <[email protected]>
Commit: Oleh Krehel <[email protected]>
Add better positioning to counsel-git-grep finalizer
counsel.el (counsel-git-grep-action): Use a regex instead of just
splitting the string on ":". Additionally, goto match, not just the line
of the match.
Fixes #153
---
counsel.el | 19 +++++++++++--------
1 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/counsel.el b/counsel.el
index 3c9fa1f..3f82275 100644
--- a/counsel.el
+++ b/counsel.el
@@ -241,14 +241,17 @@
(recenter-top-bottom)))
(defun counsel-git-grep-action (x)
- (let ((lst (split-string x ":")))
- (find-file (expand-file-name (car lst) counsel--git-grep-dir))
- (goto-char (point-min))
- (forward-line (1- (string-to-number (cadr lst))))
- (unless (eq ivy-exit 'done)
- (setq swiper--window (selected-window))
- (swiper--cleanup)
- (swiper--add-overlays (ivy--regex ivy-text)))))
+ (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 counsel--git-grep-dir))
+ (goto-char (point-min))
+ (forward-line (1- (string-to-number line-number)))
+ (re-search-forward (ivy--regex ivy-text t) (line-end-position) t)
+ (unless (eq ivy-exit 'done)
+ (setq swiper--window (selected-window))
+ (swiper--cleanup)
+ (swiper--add-overlays (ivy--regex ivy-text))))))
;;;###autoload
(defun counsel-git-grep (&optional initial-input)