Subject: bug#7378: 23.2; grep buffer misinterprets result if filename contains colon character

From: Stefan Monnier <[email-masked]>
Date: Mon, 15 Nov 2010 11:29:21 -0500

>> AFAIK we do try to figure out what was meant, even though in general
>> grep's output is simply ambiguous when the file name has colons.
>> So, could you give the exact file name you used
> ./installed/data/x11-libs---nx/3.4.0:0:C.30253.1289557929.792611.C/nx-3.4.0.exheres-0

I guess strictly speaking it's not ambiguous since grep normally starts
counting from 1, so the ":0:" cannot actually be the line number
(e.g. the patch below could help).
But I also expect that among your file names, the ":0:" is just an
accident and it could just as well be ":5:" instead, in which case
greps' output will be *really* ambiguous.

I.e. to resolve this kind of ambiguity, grep-mode would have to check
all the possible interpretations and see which ones lead to an existing
file name.  Such a change would require a good bit more work on grep.
Furthermore such ambiguity would need to be resolved not only when
the ":N:" comes from the file name but also when it comes from the
file's content.

        Stefan

=== modified file 'lisp/progmodes/grep.el'
--- lisp/progmodes/grep.el	2010-05-21 20:43:04 +0000
+++ lisp/progmodes/grep.el	2010-11-15 16:28:07 +0000
@@ -348,7 +348,11 @@
     ;; produces them
     ;; ("^\\(.+?\\)\\(:[ \t]*\\)\\([0-9]+\\)\\2\\(?:\\([0-9]+\\)\\(?:-\\([0-9]+\\)\\)?\\2\\)?"
     ;;  1 3 (4 . 5))
-    ("^\\(\\(.+?\\):\\([0-9]+\\):\\).*?\
+    ;; Note that we want to use as tight a regexp as we can to try and
+    ;; handle weird file names (with colons in them) as well as possible.
+    ;; E.g. we use [1-9][0-9]* rather than [0-9]+ so as to accept ":034:" in
+    ;; file names.
+    ("^\\(\\(.+?\\):\\([1-9][0-9]*\\):\\).*?\
 \\(\033\\[01;31m\\(?:\033\\[K\\)?\\)\\(.*?\\)\\(\033\\[[0-9]*m\\)"
      2 3
      ;; Calculate column positions (beg . end) of first grep match on a line