Subject: bug#34621: [PATCH] Fix rgrep in dired taking default search file pattern from directory name (e.g. *.11 for django-1.11)

From: Christopher Thorne <[email-masked]>
Date: Wed, 10 Apr 2019 11:42:50 +0100

> This is an overwhelmingly long name, please use the same name as
> the function that uses this feature, i.e. just 'grep-read-files'.
> Then when someone reading dired.el will find a line
> 
>   (put 'dired-mode 'grep-read-files 'dired-grep-read-files)
> 
> it will be clear for them the purpose of this feature.

Good point, I've changed it to grep-read-files.

> Please place this change in dired.el after the definition of
> 'dired-file-name-at-point' that you can use in your dired function
> instead of (run-hook-with-args-until-success 
> 'file-name-at-point-functions)

Thanks, also done this.

> If you want you could also add as a 4th option additionally
> (run-hook-with-args-until-success 'file-name-at-point-functions)
> to automatically support modes other than dired, i.e. other modes
> that set file-name-at-point-functions.

Sounds like a sensible default, added this as well.

Patch attached.

From bbf584e0c5f836c55579b9804e091c736bcf7a23 Mon Sep 17 00:00:00 2001
From: Christopher Thorne <[email-masked]>
Date: Tue, 9 Apr 2019 13:12:39 +0100
Subject: [PATCH] Fix rgrep in dired using directory for search file pattern

* lisp/progmodes/grep.el (grep-read-files): Allow major modes to
define file name to use for default search pattern.
* lisp/progmodes/grep.el (grep-read-files): Add non-directory file at
point as default search pattern candidate.
* lisp/dired.el (dired-grep-read-files): Use non-directory file at
point for grep file name pattern.  (Bug#34621)
---
 lisp/dired.el          |  9 +++++++++
 lisp/progmodes/grep.el | 12 ++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/lisp/dired.el b/lisp/dired.el
index fc0b71238b..63d28835aa 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -774,6 +774,15 @@ dired-file-name-at-point
 	  (file-name-as-directory (abbreviate-file-name filename))
 	(abbreviate-file-name filename)))))
 
+(defun dired-grep-read-files ()
+  "Use file at point as the file for grep's default file-name pattern suggestion.
+If a directory or nothing is found at point, return nil."
+  (let ((file-name (dired-file-name-at-point)))
+    (if (and file-name
+	     (not (file-directory-p file-name)))
+	file-name)))
+(put 'dired-mode 'grep-read-files 'dired-grep-read-files)
+
 ;;;###autoload (define-key ctl-x-map "d" 'dired)
 ;;;###autoload
 (defun dired (dirname &optional switches)
diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index c0f47159c9..8c7a58fd8b 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -959,8 +959,16 @@ grep-read-files
 The pattern can include shell wildcards.  As whitespace triggers
 completion when entering a pattern, including it requires
 quoting, e.g. `\\[quoted-insert]<space>'."
-  (let* ((bn (or (buffer-file-name)
-		 (replace-regexp-in-string "<[0-9]+>\\'" "" (buffer-name))))
+  (let* ((grep-read-files-function (get major-mode 'grep-read-files))
+	 (file-name-at-point
+	   (run-hook-with-args-until-success 'file-name-at-point-functions))
+	 (bn (if grep-read-files-function
+		 (funcall grep-read-files-function)
+	       (or (if (and (stringp file-name-at-point)
+			    (not (file-directory-p file-name-at-point)))
+		       file-name-at-point)
+		   (buffer-file-name)
+		   (replace-regexp-in-string "<[0-9]+>\\'" "" (buffer-name)))))
 	 (fn (and bn
 		  (stringp bn)
 		  (file-name-nondirectory bn)))
-- 
2.11.0