Subject: [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: Mon, 08 Apr 2019 11:41:20 +0100

I've updated the patch following input from Drew and Juri.
This now adds 'grep-default-file-pattern-function' which major modes can 
implement.
Dired implements this to take the extension of the file at point, with a 
default of 'all',
thus solving the issue I was having with the django-1.11 directory 
mentioned earlier in the thread.
Behaviour for major modes that don't implement 
'grep-default-file-pattern-function' will remain as before.

diff --git a/lisp/dired.el b/lisp/dired.el
index 3cb645eea7..219acbf148 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -4138,6 +4138,19 @@ dired-restore-desktop-buffer
  (add-to-list 'desktop-buffer-mode-handlers
              '(dired-mode . dired-restore-desktop-buffer))

+(defun dired-grep-default-file-pattern ()
+  "Use extension of file at point as the default file pattern for grep.
+If a directory or nothing is found at point, fallback to 'all'."
+  (let* ((dired-file-name (ignore-errors (dired-get-filename)))
+        (dired-file-extension (if (and dired-file-name
+                                       (not (file-directory-p 
dired-file-name)))
+                                  (file-name-extension 
dired-file-name))))
+    (if dired-file-extension
+       (concat "*." dired-file-extension)
+       "all")))
+(put 'dired-mode 'grep-default-file-pattern-function 
'dired-grep-default-file-pattern)
+
+
  (provide 'dired)

  (run-hooks 'dired-load-hook)           ; for your customizations
diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index 3fd2a7e701..54d1412a66 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -961,6 +961,8 @@ grep-read-files
          (fn (and bn
                   (stringp bn)
                   (file-name-nondirectory bn)))
+        (default-file-pattern-function
+          (get major-mode 'grep-default-file-pattern-function))
          (default-alias
            (and fn
                 (let ((aliases (remove (assoc "all" grep-files-aliases)
@@ -982,10 +984,12 @@ grep-read-files
                 (let ((ext (file-name-extension fn)))
                   (and ext (concat "*." ext)))))
          (default
-          (or default-alias
-              default-extension
-              (car grep-files-history)
-              (car (car grep-files-aliases))))
+          (if default-file-pattern-function
+            (funcall default-file-pattern-function)
+            (or default-alias
+                default-extension
+                (car grep-files-history)
+                (car (car grep-files-aliases)))))
          (files (completing-read
                  (concat "Search for \"" regexp
                          "\" in files matching wildcard"

Changelog entry:
* lisp/progmodes/grep.el (grep-read-files): Allow major modes to
define default search file pattern
* lisp/dired.el (dired-grep-default-file-pattern): Define default search
file pattern for grep

Thanks for the input so far. Any further suggestions are welcome.