From: Christopher Thorne Date: Thu, 11 Apr 2019 20:51:13 +0000 (+0300) Subject: Fix rgrep in dired using directory for search file pattern X-Git-Tag: emacs-27.0.90~3261^2 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=de238b39e335c6814283faa171b35145f124edf2;p=emacs.git Fix rgrep in dired using directory for search file pattern * lisp/progmodes/grep.el (grep-read-files): Allow major modes to define file name to use for default search pattern. Add non-directory file at point as default search pattern candidate. * lisp/dired.el (dired-grep-read-files): Use non-directory file at point for grep file name pattern. (Bug#34621) Copyright-paperwork-exempt: yes --- diff --git a/lisp/dired.el b/lisp/dired.el index 4c2c3f44e72..63082fe3927 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -774,6 +774,15 @@ as an argument to `dired-goto-file'." (file-name-as-directory (abbreviate-file-name filename)) (abbreviate-file-name filename))))) +(defun dired-grep-read-files () + "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 ((file-name (dired-file-name-at-point))) + (if (and file-name + (not (file-directory-p file-name))) + file-name))) +(put 'dired-mode 'grep-read-files 'dired-grep-read-files) + ;;;###autoload (define-key ctl-x-map "d" 'dired) ;;;###autoload (defun dired (dirname &optional switches) diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el index c0f47159c95..8c7a58fd8bd 100644 --- a/lisp/progmodes/grep.el +++ b/lisp/progmodes/grep.el @@ -959,8 +959,16 @@ substitution string. Note dynamic scoping of variables.") 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* ((grep-read-files-function (get major-mode 'grep-read-files)) + (file-name-at-point + (run-hook-with-args-until-success 'file-name-at-point-functions)) + (bn (if grep-read-files-function + (funcall grep-read-files-function) + (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 (stringp bn) (file-name-nondirectory bn)))