GNU bug report logs - #34621
[PATCH] lisp/progmodes/grep.el (grep-read-files): Add file-directory-p check

Previous Next

Package: emacs;

Reported by: Christopher Thorne <c.thorne <at> reckondigital.com>

Date: Fri, 22 Feb 2019 21:16:01 UTC

Severity: normal

Tags: patch

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Christopher Thorne <c.thorne <at> reckondigital.com>
To: 34621 <at> debbugs.gnu.org
Cc: juri <at> linkov.net
Subject: bug#34621: [PATCH] Fix rgrep in dired taking default search file pattern from directory name (e.g. *.11 for django-1.11)
Date: Tue, 09 Apr 2019 12:09:27 +0100
> Even more, you don't need any changes in dired.el.  Just use in grep.el
> 
>   (run-hook-with-args-until-success 'file-name-at-point-functions)
> 
> because 'file-name-at-point-functions' already has the right value in 
> Dired.
> 
> You can call it at the very beginning of grep-read-files in the same 
> 'or'
> before buffer-file-name, so the existing code will take care about 
> getting
> its file extension.

Thanks, Juri. I tried this:

diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index 3fd2a7e701..4fb4490104 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -956,11 +956,17 @@ 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)
+  (let* ((file-name-at-point (run-hook-with-args-until-success 
'file-name-at-point-functions))
+        (bn (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

However, it doesn't cover the case where emacs is in a dired buffer 
called django-1.11 with nothing at point. In this case, *.11 will be 
suggested because buffer-name is used. I'm not sure how to fix this in 
grep.el without an "if is dired" check.
That said, I do like the fact that this approach preserves the existing 
aliasing behaviour (e.g. *.c -> *.[ch]), so I've changed the approach of 
the previous patch to let major modes define the file-name grep should 
use rather than extension:

diff --git a/lisp/dired.el b/lisp/dired.el
index 3cb645eea7..d66f12b3a6 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -4138,6 +4138,16 @@ 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 3fd2a7e701..5fb56a33be 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -956,8 +956,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)))

Changelog entry:
* 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

Let me know if there are any further improvements I can make.

Regards,
Chris




This bug report was last modified 6 years and 36 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.