Skip to content

Commit

Permalink
NEW Improves integration with dired-mode
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
vitorqb committed Jul 31, 2019
1 parent bd81d68 commit 2eedcf8
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions ag.el
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,20 @@ 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 (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.
Expand All @@ -530,13 +544,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.
Expand All @@ -549,12 +572,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 " "
Expand Down Expand Up @@ -592,6 +619,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.
Expand Down

0 comments on commit 2eedcf8

Please sign in to comment.