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: Mon, 08 Apr 2019 11:41:20 +0100 Organization: Reckon Digital Message-ID: <82347809b60dfc4e18a822d2d604475c@reckondigital.com> References: <08e7108149ce2374fd0c9b7fc3e6ae38@reckondigital.com> <40839b67-bce7-4a01-89f1-c14643b18b32@default> <80f4335be923e21fe782780c6812a03f@reckondigital.com> <878sxd248k.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="107405"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Roundcube Webmail/1.3.8 To: 34621@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Apr 08 12:42:19 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 1hDRjD-000Rmc-R9 for geb-bug-gnu-emacs@m.gmane.org; Mon, 08 Apr 2019 12:42:15 +0200 Original-Received: from localhost ([127.0.0.1]:50881 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDRjC-0000me-Q1 for geb-bug-gnu-emacs@m.gmane.org; Mon, 08 Apr 2019 06:42:14 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:60094) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDRj5-0000mU-E2 for bug-gnu-emacs@gnu.org; Mon, 08 Apr 2019 06:42:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hDRj3-0002ht-EW for bug-gnu-emacs@gnu.org; Mon, 08 Apr 2019 06:42:07 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:35483) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hDRj0-0002gw-F6 for bug-gnu-emacs@gnu.org; Mon, 08 Apr 2019 06:42:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hDRj0-0002wi-A6 for bug-gnu-emacs@gnu.org; Mon, 08 Apr 2019 06:42: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: Mon, 08 Apr 2019 10:42: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.155472009011266 (code B ref 34621); Mon, 08 Apr 2019 10:42:02 +0000 Original-Received: (at 34621) by debbugs.gnu.org; 8 Apr 2019 10:41:30 +0000 Original-Received: from localhost ([127.0.0.1]:49027 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hDRiQ-0002vZ-Vn for submit@debbugs.gnu.org; Mon, 08 Apr 2019 06:41:29 -0400 Original-Received: from relay9-d.mail.gandi.net ([217.70.183.199]:38621) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hDRiO-0002vP-ME for 34621@debbugs.gnu.org; Mon, 08 Apr 2019 06:41:25 -0400 Original-Received: from webmail.gandi.net (webmail18.sd4.0x35.net [10.200.201.18]) (Authenticated sender: c.thorne@reckondigital.com) by relay9-d.mail.gandi.net (Postfix) with ESMTPA id EF1BEFF810 for <34621@debbugs.gnu.org>; Mon, 8 Apr 2019 10:41:20 +0000 (UTC) In-Reply-To: <878sxd248k.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:157344 Archived-At: 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.