Subject: bug#23219: 25.0.92; Find command cannot be customized for grep-find

From: Eli Zaretskii <[email-masked]>
Date: Tue, 05 Apr 2016 18:01:34 +0300

> From: Dmitry Gutov <[email-masked]>
> Date: Tue, 5 Apr 2016 00:16:32 +0300
> 
> On 04/04/2016 11:37 PM, Eli Zaretskii wrote:
> > On one of my machines, the GNU Find utility's executable file is named
> > 'gfind'.  To have xref-collect-matches work, I customized
> > grep-find-command to have the value "gfind", but the find-grep pipe
> > used after that still tried to invoke 'find', not 'gfind'.
> 
> You need grep-find-template, not grep-find-command.
> 
> > Looking
> > around, I see that grep-compute-defaults effectively ignores
> > grep-find-command and uses the value of find-program instead.  Now,
> > find-program is a defvar, so it wasn't supposed to be customized.  Its
> > value is never changed, no matter what are your customizations.
> 
> Yes, it's a mess. Auto-computed custom variable values don't make much 
> sense to me either, but maybe the idea is if you want to change the 
> program to use as 'find', it'll likely have a slightly different syntax 
> (not in your case, though), and grep-compute-defaults might fail to 
> work, so you're better off customizing the end values.

I see.  But if only the name of the program's executable changes, then
customizing everything would be an overkill, IMO.

> > How am I supposed to make this stuff work in this situation?  If the
> > solution is to poke find-program,
> 
> Probably.
> 
> > it should be defcustom,
> 
> *shrug*

OK, suggested patch below.  Thanks for the feedback.

--- lisp/progmodes/grep.el~0	2016-02-15 06:39:16.000000000 +0200
+++ lisp/progmodes/grep.el	2016-04-05 17:45:42.595409800 +0300
@@ -411,21 +411,33 @@
 This gets tacked on the end of the generated expressions.")
 
 ;;;###autoload
-(defvar grep-program (purecopy "grep")
+(defcustom grep-grep-program (purecopy "grep")
   "The default grep program for `grep-command' and `grep-find-command'.
-This variable's value takes effect when `grep-compute-defaults' is called.")
+This variable's value takes effect when `grep-compute-defaults' is called."
+  :type 'string
+  :set 'grep-apply-setting
+  :version "25.1"
+  :group 'grep)
 
 ;;;###autoload
-(defvar find-program (purecopy "find")
+(defcustom grep-find-program (purecopy "find")
   "The default find program.
 This is used by commands like `grep-find-command', `find-dired'
-and others.")
+and others."
+  :type 'string
+  :set 'grep-apply-setting
+  :version "25.1"
+  :group 'grep)
 
 ;;;###autoload
-(defvar xargs-program (purecopy "xargs")
+(defcustom grep-xargs-program (purecopy "xargs")
   "The default xargs program for `grep-find-command'.
 See `grep-find-use-xargs'.
-This variable's value takes effect when `grep-compute-defaults' is called.")
+This variable's value takes effect when `grep-compute-defaults' is called."
+  :type 'string
+  :set 'grep-apply-setting
+  :version "25.1"
+  :group 'grep)
 
 ;;;###autoload
 (defvar grep-find-use-xargs nil
@@ -549,8 +561,8 @@
 			  (grep-probe grep-command
 				      `(nil t nil "^English" ,hello-file)
 				      #'call-process-shell-command)
-			;; otherwise use `grep-program'
-			(grep-probe grep-program
+			;; otherwise use `grep-grep-program'
+			(grep-probe grep-grep-program
 				    `(nil t nil "-nH" "^English" ,hello-file)))
 		      (progn
 			(goto-char (point-min))
@@ -561,7 +573,7 @@
     (when (eq grep-highlight-matches 'auto-detect)
       (setq grep-highlight-matches
 	    (with-temp-buffer
-	      (and (grep-probe grep-program '(nil t nil "--help"))
+	      (and (grep-probe grep-grep-program '(nil t nil "--help"))
 		   (progn
 		     (goto-char (point-min))
 		     (search-forward "--color" nil t))
@@ -573,16 +585,16 @@
 		 grep-template grep-find-template)
       (let ((grep-options
 	     (concat (if grep-use-null-device "-n" "-nH")
-		     (if (grep-probe grep-program
+		     (if (grep-probe grep-grep-program
 				     `(nil nil nil "-e" "foo" ,null-device)
 				     nil 1)
 			 " -e"))))
 	(unless grep-command
 	  (setq grep-command
-		(format "%s %s %s " grep-program
+		(format "%s %s %s " grep-grep-program
                         (or
                          (and grep-highlight-matches
-                              (grep-probe grep-program
+                              (grep-probe grep-grep-program
                                           `(nil nil nil "--color" "x" ,null-device)
                                           nil 1)
                               (if (eq grep-highlight-matches 'always)
@@ -591,17 +603,18 @@
                          grep-options)))
 	(unless grep-template
 	  (setq grep-template
-		(format "%s <X> <C> %s <R> <F>" grep-program grep-options)))
+		(format "%s <X> <C> %s <R> <F>" grep-grep-program grep-options)))
 	(unless grep-find-use-xargs
 	  (setq grep-find-use-xargs
 		(cond
-		 ((grep-probe find-program
+		 ((grep-probe grep-find-program
 			      `(nil nil nil ,null-device "-exec" "echo"
 				    "{}" "+"))
 		  'exec-plus)
 		 ((and
-		   (grep-probe find-program `(nil nil nil ,null-device "-print0"))
-		   (grep-probe xargs-program `(nil nil nil "-0" "echo")))
+		   (grep-probe grep-find-program
+                               `(nil nil nil ,null-device "-print0"))
+		   (grep-probe grep-xargs-program `(nil nil nil "-0" "echo")))
 		  'gnu)
 		 (t
 		  'exec))))
@@ -612,10 +625,11 @@
 		       ;; after the pipe symbol be quoted if they use
 		       ;; forward slashes as directory separators.
 		       (format "%s . -type f -print0 | \"%s\" -0 %s"
-			       find-program xargs-program grep-command))
+			       grep-find-program grep-xargs-program
+                               grep-command))
 		      ((memq grep-find-use-xargs '(exec exec-plus))
 		       (let ((cmd0 (format "%s . -type f -exec %s"
-					   find-program grep-command))
+					   grep-find-program grep-command))
 			     (null (if grep-use-null-device
 				       (format "%s " null-device)
 				     "")))
@@ -627,27 +641,28 @@
 			  (1+ (length cmd0)))))
 		      (t
 		       (format "%s . -type f -print | \"%s\" %s"
-			       find-program xargs-program grep-command)))))
+			       grep-find-program grep-xargs-program
+                               grep-command)))))
 	(unless grep-find-template
 	  (setq grep-find-template
 		(let ((gcmd (format "%s <C> %s <R>"
-				    grep-program grep-options))
+				    grep-grep-program grep-options))
 		      (null (if grep-use-null-device
 				(format "%s " null-device)
 			      "")))
 		  (cond ((eq grep-find-use-xargs 'gnu)
 			 (format "%s <D> <X> -type f <F> -print0 | \"%s\" -0 %s"
-				 find-program xargs-program gcmd))
+				 grep-find-program grep-xargs-program gcmd))
 			((eq grep-find-use-xargs 'exec)
 			 (format "%s <D> <X> -type f <F> -exec %s {} %s%s"
-				 find-program gcmd null
+				 grep-find-program gcmd null
 				 (shell-quote-argument ";")))
 			((eq grep-find-use-xargs 'exec-plus)
 			 (format "%s <D> <X> -type f <F> -exec %s %s{} +"
-				 find-program gcmd null))
+				 grep-find-program gcmd null))
 			(t
 			 (format "%s <D> <X> -type f <F> -print | \"%s\" %s"
-				 find-program xargs-program gcmd))))))))
+				 grep-find-program grep-xargs-program gcmd))))))))
 
     ;; Save defaults for this host.
     (setq grep-host-defaults-alist
@@ -1080,7 +1095,7 @@
 ;;;###autoload
 (defun zrgrep (regexp &optional files dir confirm template)
   "Recursively grep for REGEXP in gzipped FILES in tree rooted at DIR.
-Like `rgrep' but uses `zgrep' for `grep-program', sets the default
+Like `rgrep' but uses `zgrep' for `grep-grep-program', sets the default
 file name to `*.gz', and sets `grep-highlight-matches' to `always'."
   (interactive
    (progn
@@ -1088,7 +1103,7 @@
      (grep-compute-defaults)
      ;; Compute the default zrgrep command by running `grep-compute-defaults'
      ;; for grep program "zgrep", but not changing global values.
-     (let ((grep-program "zgrep")
+     (let ((grep-grep-program "zgrep")
 	   ;; Don't change global values for variables computed
 	   ;; by `grep-compute-defaults'.
 	   (grep-find-template nil)