all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Christopher Thorne <c.thorne@reckondigital.com>
To: 34621@debbugs.gnu.org
Cc: juri@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	[thread overview]
Message-ID: <336a681ce53ad7623fc5f98936577d82@reckondigital.com> (raw)
In-Reply-To: <87mul0wacr.fsf@mail.linkov.net>

> 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





  reply	other threads:[~2019-04-09 11:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-22 17:29 bug#34621: [PATCH] lisp/progmodes/grep.el (grep-read-files): Add file-directory-p check Christopher Thorne
2019-03-04 11:13 ` bug#34621: Patch Update Christopher Thorne
2019-03-04 15:26   ` Drew Adams
2019-03-05 10:49     ` Christopher Thorne
2019-03-05 17:48       ` Drew Adams
2019-03-05 18:22         ` Christopher Thorne
2019-03-05 18:44           ` Drew Adams
2019-03-06 11:10             ` Christopher Thorne
2019-03-17 21:28               ` bug#34621: [PATCH] lisp/progmodes/grep.el (grep-read-files): Add file-directory-p check Juri Linkov
2019-04-08 10:41                 ` bug#34621: [PATCH] Fix rgrep in dired taking default search file pattern from directory name (e.g. *.11 for django-1.11) Christopher Thorne
2019-04-08 19:44                   ` Juri Linkov
2019-04-09 11:09                     ` Christopher Thorne [this message]
2019-04-09 11:52                       ` Noam Postavsky
2019-04-09 12:23                         ` Christopher Thorne
2019-04-09 14:18                           ` Eli Zaretskii
2019-04-09 14:32                             ` Christopher Thorne
2019-04-09 20:32                       ` Juri Linkov
2019-04-10 10:42                         ` Christopher Thorne
2019-04-10 20:37                           ` Juri Linkov
2019-04-11 20:51                             ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=336a681ce53ad7623fc5f98936577d82@reckondigital.com \
    --to=c.thorne@reckondigital.com \
    --cc=34621@debbugs.gnu.org \
    --cc=juri@linkov.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.