Skip to content

Commit 2eedcf8

Browse files
committed
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 2eedcf8

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

ag.el

+37-3
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,20 @@ 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 (dired-get-marked-files t current-prefix-arg nil nil t)))
523+
(ag/search string default-directory :files files)))
524+
525+
;;;###autoload
526+
(defun ag-regexp-on-marked-files (string)
527+
"Same as `ag-regexp`, but look only for the files marked on the current dired buffer."
528+
(interactive (list (ag/read-from-minibuffer "Search regexp")))
529+
(let ((files (dired-get-marked-files t current-prefix-arg nil nil t)))
530+
(ag/search string default-directory :regexp t :files files)))
531+
518532
;;;###autoload
519533
(defun ag-dired (dir string)
520534
"Recursively find files in DIR matching literal search STRING.
@@ -530,13 +544,22 @@ See also `ag-dired-regexp'."
530544
(ag-dired-regexp dir (ag/escape-pcre string)))
531545

532546
;;;###autoload
533-
(defun ag-dired-regexp (dir regexp)
547+
(defun ag-contents-dired (dir string)
548+
"Same as `ag-dired`, but look at file contents rather than file paths."
549+
(interactive "DDirectory: \nsFile contents pattern: ")
550+
(ag-dired-regexp dir (ag/escape-pcre string) t))
551+
552+
;;;###autoload
553+
(defun ag-dired-regexp (dir regexp &optional match-contents-p)
534554
"Recursively find files in DIR matching REGEXP.
535555
REGEXP should be in PCRE syntax, not Emacs regexp syntax.
536556
537557
The REGEXP is matched against the full path to the file, not
538558
only against the file name.
539559
560+
If `match-contents-p` is non-nil, look at the file contents
561+
rather than the file full paths.
562+
540563
Results are presented as a `dired-mode' buffer with
541564
`default-directory' being DIR.
542565
@@ -549,12 +572,16 @@ See also `find-dired'."
549572
"*ag dired*"
550573
(format "*ag dired pattern:%s dir:%s*" regexp dir)))
551574
(cmd (if (string= system-type "windows-nt")
552-
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") " -g \"" regexp "\" "
575+
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ")
576+
(if match-contents-p " -l " " -g ")
577+
"\"" regexp "\" "
553578
(shell-quote-argument dir)
554579
" | grep -v \"^$\" | sed \"s/'/\\\\\\\\'/g\" | xargs -I '{}' "
555580
insert-directory-program " "
556581
dired-listing-switches " '{}' &")
557-
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ") " -g '" regexp "' "
582+
(concat ag-executable " " (combine-and-quote-strings ag-dired-arguments " ")
583+
(if match-contents-p " -l " " -g ")
584+
"'" regexp "' "
558585
(shell-quote-argument dir)
559586
" | grep -v '^$' | sed s/\\'/\\\\\\\\\\'/g | xargs -I '{}' "
560587
insert-directory-program " "
@@ -592,6 +619,13 @@ See also `find-dired'."
592619
(move-marker (process-mark proc) 1 (current-buffer)))
593620
(setq mode-line-process '(":%s")))))
594621

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

0 commit comments

Comments
 (0)