Subject: Re: 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: Tue, 09 Apr 2019 13:23:11 +0100

> Just a minor suggestion meta suggestion: post your patches by attaching
> the output from 'git format-patch', rather than inlining the output of
> git diff.  That will avoid patch corruption due to line-wrapping, like
> in the above quote (although that line is also a bit long, and you
> should break it manually).

Thanks, Noam. Trying this now. Patch should be attached.

From af551683f9ff6c9bcdd967135a72a268eda6177a 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
 (Bug#34621)

* lisp/progmodes/grep.el (grep-read-files): Allow major modes to
define file name to use for default search pattern
* lisp/dired.el (dired-grep-file-name-for-default-pattern): Use file
at point for grep file name pattern
---
 lisp/dired.el          | 11 +++++++++++
 lisp/progmodes/grep.el |  8 ++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/lisp/dired.el b/lisp/dired.el
index fc0b71238b..33abca550a 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -4162,6 +4162,17 @@ dired-restore-desktop-buffer
 (add-to-list 'desktop-buffer-mode-handlers
 	     '(dired-mode . dired-restore-desktop-buffer))
 
+(defun dired-grep-file-name-for-default-pattern ()
+  "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 ((dired-file-name
+	  (run-hook-with-args-until-success 'file-name-at-point-functions)))
+    (if (and dired-file-name
+	     (not (file-directory-p dired-file-name)))
+	dired-file-name)))
+(put 'dired-mode 'grep-file-name-for-default-pattern-function 'dired-grep-file-name-for-default-pattern)
+
+
 (provide 'dired)
 
 (run-hooks 'dired-load-hook)		; for your customizations
diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index c0f47159c9..b0bb8e6924 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -959,8 +959,12 @@ 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* ((file-name-for-default-pattern-function
+	   (get major-mode 'grep-file-name-for-default-pattern-function))
+	 (bn (if file-name-for-default-pattern-function
+		 (funcall file-name-for-default-pattern-function)
+	       (or (buffer-file-name)
+		 (replace-regexp-in-string "<[0-9]+>\\'" "" (buffer-name)))))
 	 (fn (and bn
 		  (stringp bn)
 		  (file-name-nondirectory bn)))
-- 
2.11.0