Subject: bug#16045: 24.3.50; rgrep can't work

From: Eli Zaretskii <[email-masked]>
Date: Wed, 04 Dec 2013 19:24:07 +0200

> Date: Wed, 4 Dec 2013 13:57:45 +0800
> From: zijianyue <[email-masked]>
> 
>   Hello,rgrep worked well before I use this version 24.3.50.
>   Now,rgrep always found nothing in emacs,lgrep works well.I don't know why.

Michael, this is due to this commit of yours (almost a year ago!):

  revno: 111276
  committer: Michael Albinus <[email-masked]>
  branch nick: trunk
  timestamp: Thu 2012-12-20 11:15:38 +0000
  message:
    * progmodes/grep.el (rgrep): Escape command line.  Sometimes, it
    is too long for Tramp.  See discussion in
    <http://thread.gmane.org/gmane.emacs.tramp/8233/focus=8244>.

    * progmodes/compile.el (compilation-start): Remove line escape template.

Command lines with newlines are non-portable: Windows shells don't
support them, and don't treat backslashes specially anyway (that's why
we have shell-quote-argument).  So what that change did on Windows is
add literal \r\n strings to the command, making it wrong at best and
un-parsable at worst.

Since the original problem was with Tramp, i.e. with remote files, I
suggest the patch below; it worked for me on Windows.  Please see if
there's anything wrong with it; if not, I will commit.

Btw, I cannot say I like the fact that the command displayed in the
*grep* buffer is different from what is actually passed to the shell.
It makes debugging much harder when some error occurs.  In my case,
'find' displayed several warnings like this:

  find: warning: Filenames usually don't contain slashes (though pathnames do).  That means that '-name .#*\
  ' will probably evaluate to false all the time on this system.  You might find the '-wholename' test more useful, or perhaps '-samefile'.  Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ .#*\
  '.

and I stared at this for several minutes trying to figure out what the
heck was it talking about, since the command displayed in the buffer
had no backslashes at all.  I needed to look at the command line in
GDB to see what is actually being sent.  So I think this is not a good
idea at all.

Here's the patch I suggest:

=== modified file 'lisp/progmodes/grep.el'
--- lisp/progmodes/grep.el	2013-05-24 20:54:38 +0000
+++ lisp/progmodes/grep.el	2013-12-04 17:10:42 +0000
@@ -1005,7 +1005,9 @@ to specify a command to run."
 			      (mapconcat
 			       #'shell-quote-argument
 			       (split-string files)
-			       (concat "\\\n" " -o " find-name-arg " "))
+			       (concat
+				(if (file-remote-p dir) "\\\n")
+				" -o " find-name-arg " "))
 			      " "
 			      (shell-quote-argument ")"))
 		      dir
@@ -1026,7 +1028,9 @@ to specify a command to run."
 						      (concat "*/"
 							      (cdr ignore)))))))
 				     grep-find-ignored-directories
-				     "\\\n -o -path ")
+				     (if (file-remote-p dir)
+					 "\\\n -o -path "
+				       " -o -path "))
 				    " "
 				    (shell-quote-argument ")")
 				    " -prune -o "))
@@ -1044,7 +1048,9 @@ to specify a command to run."
 						     (shell-quote-argument
 						      (cdr ignore))))))
 				     grep-find-ignored-files
-				     "\\\n -o -name ")
+				     (if (file-remote-p dir)
+					 "\\\n -o -name "
+				       " -o -name "))
 				    " "
 				    (shell-quote-argument ")")
 				    " -prune -o "))))))