]> code.delx.au - gnu-emacs-elpa/commitdiff
Add better positioning to counsel-git-grep finalizer
authorOleh Krehel <ohwoeowho@gmail.com>
Fri, 19 Jun 2015 12:34:19 +0000 (14:34 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Fri, 19 Jun 2015 12:34:19 +0000 (14:34 +0200)
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

index 3c9fa1fa575d8328017f5081fefd7803cafcac45..3f82275adb5c7d1dada239713aa93f189a244b1e 100644 (file)
     (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)