From 86de5615fff5dcbe09db39745fbe9c625a77c46c Mon Sep 17 00:00:00 2001 From: Nathan Moreau Date: Wed, 5 Dec 2018 00:23:17 +0100 Subject: [PATCH] counsel.el (counsel--normalize-grep-match): remove column indicators for wgrep. --- counsel.el | 11 ++++++++++- ivy-test.el | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/counsel.el b/counsel.el index bd21a159..afc33fe1 100644 --- a/counsel.el +++ b/counsel.el @@ -1503,7 +1503,16 @@ If NO-ASYNC is non-nil, do it synchronously instead." nil)))) (defun counsel--normalize-grep-match (str) - (if (string-match-p "\\`\\.[/\\]" str) str (concat "./" str))) + ;; Prepend ./ if necessary: + (unless (string-match-p "\\`\\.[/\\]" str) + (setq str (concat "./" str))) + ;; Remove column info if any: + (save-match-data + (when (string-match + "[^\n:]+?[^\n/:]:[\t ]*[1-9][0-9]*[\t ]*:\\([1-9][0-9]*:\\)" + str) + (setq str (replace-match "" t t str 1)))) + str) (defun counsel-git-grep-occur () "Generate a custom occur buffer for `counsel-git-grep'. diff --git a/ivy-test.el b/ivy-test.el index ba01143e..d82603ff 100644 --- a/ivy-test.el +++ b/ivy-test.el @@ -947,6 +947,30 @@ a buffer visiting a file." (should (not (ivy--starts-with-dotslash "t\\est5"))) (should (not (ivy--starts-with-dotslash "tes./t6")))) +(ert-deftest counsel--normalize-grep-match () + (with-temp-buffer + (let ((match-data-orig + (progn + (insert "abcd\nefgh") + (goto-char (point-min)) + (re-search-forward "\\(ab\\)\\(cd\\)") + (match-data))) + input expected out) + (dolist (test '(("./FILENAME:1234:32: TEXT MORETEXT" . + "./FILENAME:1234: TEXT MORETEXT") + ("FILENAME:1234: TEXT MORETEXT" . + "./FILENAME:1234: TEXT MORETEXT") + )) + (setq input (car test)) + (setq expected (cdr test)) + (setq out (counsel--normalize-grep-match input)) + (should (equal out expected)) + (should (equal match-data-orig (match-data))) + (print (list :out out :expected expected)) + (setq out (counsel--normalize-grep-match out)) + (should (equal out expected)) + (should (equal match-data-orig (match-data))))))) + (provide 'ivy-test) ;;; ivy-test.el ends here