branch: externals/urgrep commit 350d6d18892ac31e85acdcb966805fba2c572479 Author: Jim Porter <jporterb...@gmail.com> Commit: Jim Porter <jporterb...@gmail.com>
Add support for regexp-syntax and context with the grep backend --- urgrep-tests.el | 22 +++++++++++++--------- urgrep.el | 26 ++++++++++++++++++++------ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/urgrep-tests.el b/urgrep-tests.el index d9c8ed4fc4..e7fefa918f 100644 --- a/urgrep-tests.el +++ b/urgrep-tests.el @@ -147,29 +147,33 @@ (ert-deftest urgrep-tests-command-grep () (let ((tool (assoc "grep" urgrep-tools))) ;; String/case - (should (string-match "^find \\. .*grep .*-i .*foo" + (should (string-match "^find \\. .*grep -F .*-i .*foo" (urgrep-command "foo" :tool tool))) - (should (string-match "^find \\. .*grep .*Foo" + (should (string-match "^find \\. .*grep -F .*Foo" (urgrep-command "Foo" :tool tool))) (let ((case-fold-search nil)) - (should (string-match "^find \\. .*grep .*foo" + (should (string-match "^find \\. .*grep -F .*foo" (urgrep-command "foo" :tool tool)))) ;; Group - (should (string-match "^find \\. .*grep .*-i .*foo" + (should (string-match "^find \\. .*grep -F .*-i .*foo" (urgrep-command "foo" :tool tool :group nil))) ;; Regexp - (should (string-match "^find \\. .*grep .*-i .*\\\\(foo\\\\)" + (should (string-match "^find \\. .*grep -G .*-i .*\\\\(foo\\\\)" (urgrep-command "(foo)" :tool tool :regexp-syntax 'bre))) - (should (string-match "^find \\. .*grep .*-i .*\\\\(foo\\\\)" + (should (string-match "^find \\. .*grep -E .*-i .*\\\\(foo\\\\)" (urgrep-command "(foo)" :tool tool :regexp-syntax 'ere))) - (should (string-match "^find \\. .*grep .*-i .*\\\\(foo\\\\)" + (should (string-match "^find \\. .*grep -P .*-i .*\\\\(foo\\\\)" (urgrep-command "(foo)" :tool tool :regexp-syntax 'pcre))) ;; Context - (should (string-match "^find \\. .*grep .*-i .*foo" - (urgrep-command "foo" :tool tool :context 3))))) + (should (string-match "^find \\. .*grep -F -C3 .*-i .*foo" + (urgrep-command "foo" :tool tool :context 3))) + (should (string-match "^find \\. .*grep -F -C3 .*-i .*foo" + (urgrep-command "foo" :tool tool :context '(3 . 3)))) + (should (string-match "^find \\. .*grep -F -B2 -A4 .*-i .*foo" + (urgrep-command "foo" :tool tool :context '(2 . 4)))))) (ert-deftest urgrep-tests-get-tool-default () (cl-letf (((symbol-function #'executable-find) #'always)) diff --git a/urgrep.el b/urgrep.el index 39ef195278..b2a73571cb 100644 --- a/urgrep.el +++ b/urgrep.el @@ -82,11 +82,20 @@ If a cons, show CAR and CDR lines before and after, respectively." ;; Urgrep tools -(cl-defun urgrep-rgrep--command (query &key &allow-other-keys) - ;; XXX: Support literal/regexp and context settings. Perhaps let-bind - ;; `grep-find-template' to include these options? +(cl-defun urgrep--rgrep-command (query &key tool regexp-syntax context + &allow-other-keys) (grep-compute-defaults) - (rgrep-default-command query "*" nil)) + ;; Locally add options to `grep-find-template' that grep.el isn't aware of. + (let ((grep-find-template grep-find-template)) + (dolist (i `((regexp-arguments . ,regexp-syntax) + (context-arguments . ,context))) + (when-let ((args (urgrep-get-property-pcase tool (car i) (cdr i))) + (args (mapconcat #'urgrep--maybe-shell-quote-argument args + " ")) + ((string-match "<C>" grep-find-template))) + (setq grep-find-template + (replace-match (concat args " <C>") t t grep-find-template)))) + (rgrep-default-command query "*" nil))) (defconst urgrep--context-arguments '(((or '(0 . 0) 0) nil) @@ -145,7 +154,12 @@ If a cons, show CAR and CDR lines before and after, respectively." (case-fold-arguments (((pred identity) '("-i"))))) ("grep" (executable-name "grep") - (command-function ,#'urgrep-rgrep--command))) + (command-function ,#'urgrep--rgrep-command) + (context-arguments ,urgrep--context-arguments) + (regexp-arguments (('bre '("-G")) + ('ere '("-E")) + ('pcre '("-P")) + (_ '("-F")))))) "An alist of known tools to try when running urgrep.") (defcustom urgrep-preferred-tools nil @@ -248,7 +262,7 @@ for MS shells." (context 0)) (if-let ((tool (urgrep-get-tool tool)) (cmd-fun (urgrep-get-property tool 'command-function))) - (apply cmd-fun query rest) + (apply cmd-fun query :tool tool rest) (let* ((tool-re-syntax (urgrep--get-best-syntax regexp-syntax tool)) (query (urgrep--convert-regexp query regexp-syntax tool-re-syntax)) (fold-case (and case-fold-search