Subject: Re: bug#8340: 23.2; recompile does not retain compilation-environment needed by vc-git-grep

From: Juri Linkov <[email-masked]>
Date: Tue, 06 Sep 2011 12:27:19 +0300

> Generally it would be better to reuse `compilation-environment' and set
> it buffer-locally in the *compilation* buffer, but there is another problem.
> Some users might want to set `compilation-environment' in .emacs like:
>
>   (add-hook 'compilation-mode-hook
>             (lambda ()
>               (setq compilation-environment '("LANG=C"))))
>
> See more in http://thread.gmane.org/gmane.emacs.devel/108353
>
> But then let-binding (let ((compilation-environment '("PAGER="))))
> in `vc-git-grep' is ineffective because `compilation-mode-hook'
> overrides it.  So `compilation-environment' should be defcustom
> to avoid this problem.

The following patch fixed all these problems:

=== modified file 'lisp/progmodes/compile.el'
--- lisp/progmodes/compile.el	2011-09-02 16:38:40 +0000
+++ lisp/progmodes/compile.el	2011-09-06 09:23:31 +0000
@@ -637,11 +637,15 @@ (defvar compilation-exit-message-functio
 and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
 write into the compilation buffer, and to put in its mode line.")
 
-(defvar compilation-environment nil
-  "*List of environment variables for compilation to inherit.
+(defcustom compilation-environment nil
+  "List of environment variables for compilation to inherit.
 Each element should be a string of the form ENVVARNAME=VALUE.
 This list is temporarily prepended to `process-environment' prior to
-starting the compilation process.")
+starting the compilation process."
+  :type '(repeat (string :tag "ENVVARNAME=VALUE"))
+  :options '(("LANG=C"))
+  :group 'compilation
+  :version "24.1")
 
 ;; History of compile commands.
 (defvar compile-history nil)
@@ -1482,6 +1491,7 @@ (defun compilation-start (command &optio
 	      "compilation"
 	    (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
 	 (thisdir default-directory)
+	 (thisenv compilation-environment)
 	 outwin outbuf)
     (with-current-buffer
 	(setq outbuf
@@ -1530,6 +1541,7 @@ (defun compilation-start (command &optio
         ;; affected by the special handling of "cd ...;".
         ;; NB: must be fone after (funcall mode) as that resets local variables
         (set (make-local-variable 'compilation-directory) thisdir)
+	(set (make-local-variable 'compilation-environment) thisenv)
 	(if highlight-regexp
 	    (set (make-local-variable 'compilation-highlight-regexp)
 		 highlight-regexp))

=== modified file 'lisp/vc/vc-git.el'
--- lisp/vc/vc-git.el	2011-09-01 07:29:56 +0000
+++ lisp/vc/vc-git.el	2011-09-06 09:19:48 +0000
@@ -998,7 +998,7 @@ (defun vc-git-grep (regexp &optional fil
 	    (add-to-history 'grep-history command))))
       (when command
 	(let ((default-directory dir)
-	      (compilation-environment '("PAGER=")))
+	      (compilation-environment (cons "PAGER=" compilation-environment)))
 	  ;; Setting process-setup-function makes exit-message-function work
 	  ;; even when async processes aren't supported.
 	  (compilation-start command 'grep-mode))