From 36b0729ce765c132e04586be0e2deca405b4c313 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Mon, 18 Jan 2016 22:11:46 +0300 Subject: Add xref-based replacements for Dired search commands * lisp/dired-aux.el (dired-do-find-regexp) (dired-do-find-regexp-and-replace): New commands. http://lists.gnu.org/archive/html/emacs-devel/2016-01/msg00864.html * lisp/dired.el (dired-mode-map): Change bindings for `A' and `Q' to the new commands. * lisp/progmodes/xref.el (xref-query-replace) (xref-collect-matches): Add progress reporters. (xref--find-ignores-arguments): Return nil for zero ignores. (xref--show-xrefs): Add an optional argument. (xref-collect-matches): Drop the assert. 'find' accepts a regular file in place of directory argument, too. --- lisp/dired-aux.el | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lisp/dired-aux.el') diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index a8c40b2835c..831278cb762 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -2713,6 +2713,41 @@ with the command \\[tags-loop-continue]." (tags-query-replace from to delimited '(dired-get-marked-files nil nil 'dired-nondirectory-p))) +(declare-function xref--show-xrefs "xref") +(declare-function xref-query-replace "xref") + +;;;###autoload +(defun dired-do-find-regexp (regexp) + "Find all matches for REGEXP in all marked files, recursively." + (interactive "sSearch marked files (regexp): ") + (require 'grep) + (defvar grep-find-ignored-files) + (let* ((files (dired-get-marked-files)) + (ignores (nconc (mapcar + (lambda (s) (concat s "/")) + vc-directory-exclusion-list) + grep-find-ignored-files)) + (xrefs (cl-mapcan + (lambda (file) + (xref-collect-matches regexp "*" file + (and (file-directory-p file) + ignores))) + files))) + (unless xrefs + (user-error "No matches for: %s" regexp)) + (xref--show-xrefs xrefs nil t))) + +;;;###autoload +(defun dired-do-find-regexp-and-replace (from to) + "Replace matches of FROM with TO, in all marked files, recursively." + (interactive + (let ((common + (query-replace-read-args + "Query replace regexp in marked files" t t))) + (list (nth 0 common) (nth 1 common)))) + (with-current-buffer (dired-do-find-regexp from) + (xref-query-replace from to))) + (defun dired-nondirectory-p (file) (not (file-directory-p file))) -- cgit v1.2.1