Skip to content

Commit 98a383a

Browse files
vitorqbvitor-gpm
authored andcommitted
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.
1 parent bd81d68 commit 98a383a

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

ag.el

+39-3
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,22 @@ If called with a prefix, prompts for flags to pass to ag."
515515
(defalias 'ag-regexp-project-at-point 'ag-project-regexp)
516516
(make-obsolete 'ag-regexp-project-at-point 'ag-project-regexp "0.46")
517517

518+
;;;###autoload
519+
(defun ag-on-marked-files (string)
520+
"Same as `ag`, but look only for the files marked on the current dired buffer."
521+
(interactive (list (ag/read-from-minibuffer "Search string")))
522+
(let ((files (if (version< emacs-version "27.0")
523+
(dired-get-marked-files t current-prefix-arg nil nil)
524+
(dired-get-marked-files t current-prefix-arg nil nil t))))
525+
(ag/search string default-directory :files files)))
526+
527+
;;;###autoload
528+
(defun ag-regexp-on-marked-files (string)
529+
"Same as `ag-regexp`, but look only for the files marked on the current dired buffer."
530+
(interactive (list (ag/read-from-minibuffer "Search regexp")))
531+
(let ((files (dired-get-marked-files t current-prefix-arg nil nil t)))
532+
(ag/search string default-directory :regexp t :files files)))
533+
518534
;;;###autoload
519535
(defun ag-dired (dir string)
520536
"Recursively find files in DIR matching literal search STRING.
@@ -530,13 +546,22 @@ See also `ag-dired-regexp'."
530546
(ag-dired-regexp dir (ag/escape-pcre string)))
531547

532548
;;;###autoload
533-
(defun ag-dired-regexp (dir regexp)
549+
(defun ag-contents-dired (dir string)
550+
"Same as `ag-dired`, but look at file contents rather than file paths."
551+
(interactive "DDirectory: \nsFile contents pattern: ")
552+
(ag-dired-regexp dir (ag/escape-pcre string) t))
553+
554+
;;;###autoload
555+
(defun ag-dired-regexp (dir regexp &optional match-contents-p)
534556
"Recursively find files in DIR matching REGEXP.
535557
REGEXP should be in PCRE syntax, not Emacs regexp syntax.
536558
537559
The REGEXP is matched against the full path to the file, not
538560
only against the file name.
539561
562+
If `match-contents-p` is non-nil, look at the file contents
563+
rather than the file full paths.
564+
540565
Results are presented as a `dired-mode' buffer with
541566
`default-directory' being DIR.
542567
@@ -549,12 +574,16 @@ See also `find-dired'."
549574
"*ag dired*"
550575
(format "*ag dired pattern:%s dir:%s*" regexp dir)))
551576
(cmd (if (string= system-type "windows-nt")
552-
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") " -g \"" regexp "\" "
577+
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ")
578+
(if match-contents-p " -l " " -g ")
579+
"\"" regexp "\" "
553580
(shell-quote-argument dir)
554581
" | grep -v \"^$\" | sed \"s/'/\\\\\\\\'/g\" | xargs -I '{}' "
555582
insert-directory-program " "
556583
dired-listing-switches " '{}' &")
557-
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") " -g '" regexp "' "
584+
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ")
585+
(if match-contents-p " -l " " -g ")
586+
"'" regexp "' "
558587
(shell-quote-argument dir)
559588
" | grep -v '^$' | sed s/\\'/\\\\\\\\\\'/g | xargs -I '{}' "
560589
insert-directory-program " "
@@ -592,6 +621,13 @@ See also `find-dired'."
592621
(move-marker (process-mark proc) 1 (current-buffer)))
593622
(setq mode-line-process '(":%s")))))
594623

624+
;;;###autoload
625+
(defun ag-contents-dired-regexp (dir regexp)
626+
"Same as `ag-dired-regexp`, but look for regexp in file contents rather
627+
than in file names."
628+
(interactive "DDirectory: \nsFile contents regexp: ")
629+
(ag-dired-regexp dir regexp t))
630+
595631
;;;###autoload
596632
(defun ag-project-dired (pattern)
597633
"Recursively find files in current project matching PATTERN.

0 commit comments

Comments
 (0)