From 98a383a5345d29956e66f10068896d81b8c08e3d Mon Sep 17 00:00:00 2001 From: vitor quintanilha barbosa Date: Wed, 31 Jul 2019 20:03:27 +0200 Subject: [PATCH] NEW Improves integration with `dired-mode` - Improves `ag-dired-regexp` to accept `match-contents-p` flag, under which the matching is performed at the file contents level instead of the file full path (using -l flag instead of f-g). - Adds `ag-contents-dired` and `ag-contents-dired-regexp` as shortcuts for openning dired on files with contents matching some pattern. - Adds `ag-on-marked-files` and `ag-regexp-on-marked-files` as shortcuts to call `ag` on the files currently selected for the dired buffer. --- ag.el | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/ag.el b/ag.el index 43a10d6..8da1911 100644 --- a/ag.el +++ b/ag.el @@ -515,6 +515,22 @@ If called with a prefix, prompts for flags to pass to ag." (defalias 'ag-regexp-project-at-point 'ag-project-regexp) (make-obsolete 'ag-regexp-project-at-point 'ag-project-regexp "0.46") +;;;###autoload +(defun ag-on-marked-files (string) + "Same as `ag`, but look only for the files marked on the current dired buffer." + (interactive (list (ag/read-from-minibuffer "Search string"))) + (let ((files (if (version< emacs-version "27.0") + (dired-get-marked-files t current-prefix-arg nil nil) + (dired-get-marked-files t current-prefix-arg nil nil t)))) + (ag/search string default-directory :files files))) + +;;;###autoload +(defun ag-regexp-on-marked-files (string) + "Same as `ag-regexp`, but look only for the files marked on the current dired buffer." + (interactive (list (ag/read-from-minibuffer "Search regexp"))) + (let ((files (dired-get-marked-files t current-prefix-arg nil nil t))) + (ag/search string default-directory :regexp t :files files))) + ;;;###autoload (defun ag-dired (dir string) "Recursively find files in DIR matching literal search STRING. @@ -530,13 +546,22 @@ See also `ag-dired-regexp'." (ag-dired-regexp dir (ag/escape-pcre string))) ;;;###autoload -(defun ag-dired-regexp (dir regexp) +(defun ag-contents-dired (dir string) + "Same as `ag-dired`, but look at file contents rather than file paths." + (interactive "DDirectory: \nsFile contents pattern: ") + (ag-dired-regexp dir (ag/escape-pcre string) t)) + +;;;###autoload +(defun ag-dired-regexp (dir regexp &optional match-contents-p) "Recursively find files in DIR matching REGEXP. REGEXP should be in PCRE syntax, not Emacs regexp syntax. The REGEXP is matched against the full path to the file, not only against the file name. +If `match-contents-p` is non-nil, look at the file contents +rather than the file full paths. + Results are presented as a `dired-mode' buffer with `default-directory' being DIR. @@ -549,12 +574,16 @@ See also `find-dired'." "*ag dired*" (format "*ag dired pattern:%s dir:%s*" regexp dir))) (cmd (if (string= system-type "windows-nt") - (concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") " -g \"" regexp "\" " + (concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") + (if match-contents-p " -l " " -g ") + "\"" regexp "\" " (shell-quote-argument dir) " | grep -v \"^$\" | sed \"s/'/\\\\\\\\'/g\" | xargs -I '{}' " insert-directory-program " " dired-listing-switches " '{}' &") - (concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") " -g '" regexp "' " + (concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") + (if match-contents-p " -l " " -g ") + "'" regexp "' " (shell-quote-argument dir) " | grep -v '^$' | sed s/\\'/\\\\\\\\\\'/g | xargs -I '{}' " insert-directory-program " " @@ -592,6 +621,13 @@ See also `find-dired'." (move-marker (process-mark proc) 1 (current-buffer))) (setq mode-line-process '(":%s"))))) +;;;###autoload +(defun ag-contents-dired-regexp (dir regexp) + "Same as `ag-dired-regexp`, but look for regexp in file contents rather +than in file names." + (interactive "DDirectory: \nsFile contents regexp: ") + (ag-dired-regexp dir regexp t)) + ;;;###autoload (defun ag-project-dired (pattern) "Recursively find files in current project matching PATTERN.