Subject: bug#12640: 23.3; compilation-start automatic `cd` doesn't work if path has spaces

From: Stefan Monnier <[email-masked]>
Date: Tue, 23 Oct 2012 15:08:36 -0400

> Function `compilation-start' in compile.el detects if the COMMAND starts
> with a `cd directory && ...` and sets the `default-directory'
> accordingly.  But it doesn't work if the directory being `cd`ed to
> contains spaces, even if correctly quoted using
> `shell-quote-argument'.

> The regexp used to detect the path is too simple but I'm not sure how
> you would get one to detect matching quote pairs.

I've installed the patch below which should handle the simple cases
of quoting.

        Stefan

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2012-10-23 18:40:23 +0000
+++ lisp/ChangeLog	2012-10-23 19:06:26 +0000
@@ -1,5 +1,8 @@
 2012-10-23  Stefan Monnier  <[email-masked]>
 
+	* progmodes/compile.el (compilation-start): Try to handle common
+	quoting of `cd' argument (bug#12640).
+
 	* vc/diff-mode.el (diff-hunk): `save-excursion' while refining
 	(bug#12671).

=== modified file 'lisp/progmodes/compile.el'
--- lisp/progmodes/compile.el	2012-10-14 07:40:05 +0000
+++ lisp/progmodes/compile.el	2012-10-23 19:04:14 +0000
@@ -1568,12 +1568,20 @@
 	;; Then evaluate a cd command if any, but don't perform it yet, else
 	;; start-command would do it again through the shell: (cd "..") AND
 	;; sh -c "cd ..; make"
-	(cd (if (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\)\\)?\\s *[;&\n]"
-			      command)
-		(if (match-end 1)
-		    (substitute-env-vars (match-string 1 command))
-		  "~")
-	      default-directory))
+	(cd (cond
+             ((not (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\|'[^']*'\\|\"\\(?:[^\"`$\\]\\|\\\\.\\)*\"\\)\\)?\\s *[;&\n]"
+                                 command))
+              default-directory)
+             ((not (match-end 1)) "~")
+             ((eq (aref command (match-beginning 1)) ?\')
+              (substring command (1+ (match-beginning 1))
+                         (1- (match-end 1))))
+             ((eq (aref command (match-beginning 1)) ?\")
+              (replace-regexp-in-string
+               "\\\\\\(.\\)" "\\1"
+               (substring command (1+ (match-beginning 1))
+                          (1- (match-end 1)))))
+             (t (substitute-env-vars (match-string 1 command)))))
 	(erase-buffer)
 	;; Select the desired mode.
 	(if (not (eq mode t))