From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Christopher Thorne Newsgroups: gmane.emacs.bugs 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 Organization: Reckon Digital Message-ID: <336a681ce53ad7623fc5f98936577d82@reckondigital.com> References: <08e7108149ce2374fd0c9b7fc3e6ae38@reckondigital.com> <40839b67-bce7-4a01-89f1-c14643b18b32@default> <80f4335be923e21fe782780c6812a03f@reckondigital.com> <878sxd248k.fsf@mail.linkov.net> <82347809b60dfc4e18a822d2d604475c@reckondigital.com> <87mul0wacr.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="118732"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Roundcube Webmail/1.3.8 Cc: juri@linkov.net To: 34621@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Apr 09 13:10:15 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hDodq-000Uk1-5s for geb-bug-gnu-emacs@m.gmane.org; Tue, 09 Apr 2019 13:10:14 +0200 Original-Received: from localhost ([127.0.0.1]:39223 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDodp-0005a7-4v for geb-bug-gnu-emacs@m.gmane.org; Tue, 09 Apr 2019 07:10:13 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:56667) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDodh-0005Zv-M5 for bug-gnu-emacs@gnu.org; Tue, 09 Apr 2019 07:10:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hDodf-00009j-QU for bug-gnu-emacs@gnu.org; Tue, 09 Apr 2019 07:10:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:37076) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hDode-000092-IE for bug-gnu-emacs@gnu.org; Tue, 09 Apr 2019 07:10:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hDode-00025r-8Y for bug-gnu-emacs@gnu.org; Tue, 09 Apr 2019 07:10:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Christopher Thorne Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 09 Apr 2019 11:10:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 34621 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 34621-submit@debbugs.gnu.org id=B34621.15548081747914 (code B ref 34621); Tue, 09 Apr 2019 11:10:02 +0000 Original-Received: (at 34621) by debbugs.gnu.org; 9 Apr 2019 11:09:34 +0000 Original-Received: from localhost ([127.0.0.1]:50620 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hDodB-00023V-Rw for submit@debbugs.gnu.org; Tue, 09 Apr 2019 07:09:34 -0400 Original-Received: from relay12.mail.gandi.net ([217.70.178.232]:48869) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hDod9-000239-82 for 34621@debbugs.gnu.org; Tue, 09 Apr 2019 07:09:32 -0400 Original-Received: from webmail.gandi.net (webmail18.sd4.0x35.net [10.200.201.18]) (Authenticated sender: c.thorne@reckondigital.com) by relay12.mail.gandi.net (Postfix) with ESMTPA id 4410B20000D; Tue, 9 Apr 2019 11:09:27 +0000 (UTC) In-Reply-To: <87mul0wacr.fsf@mail.linkov.net> X-Sender: c.thorne@reckondigital.com X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:157405 Archived-At: > 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]'." - (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]'." - (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