emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/urgrep 794f1bb069 1/3: Add support for generating Urgre


From: ELPA Syncer
Subject: [elpa] externals/urgrep 794f1bb069 1/3: Add support for generating Urgrep commands with no search root
Date: Tue, 21 Nov 2023 00:58:45 -0500 (EST)

branch: externals/urgrep
commit 794f1bb06914021bcf26cda4abdb0e7ab1cd6c9b
Author: Jim Porter <jporterbugs@gmail.com>
Commit: Jim Porter <jporterbugs@gmail.com>

    Add support for generating Urgrep commands with no search root
---
 NEWS.md         |   2 +
 urgrep-tests.el |  81 ++++++++++++++++++++------------
 urgrep.el       | 143 ++++++++++++++++++++++++++++++++------------------------
 3 files changed, 134 insertions(+), 92 deletions(-)

diff --git a/NEWS.md b/NEWS.md
index 10459c8593..188175f020 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -8,6 +8,8 @@
 ### Breaking changes
 - `:files` keyword argument in `urgrep-command` and friends is now
   `:file-wildcard`
+- `:directory` keyword argument in `urgrep-commnd` and friends is now `:root`,
+  and can accept file and/or directory names
 
 ---
 
diff --git a/urgrep-tests.el b/urgrep-tests.el
index 11290f1cb6..019a6e782a 100644
--- a/urgrep-tests.el
+++ b/urgrep-tests.el
@@ -199,18 +199,21 @@ joined to compare against COMMAND."
        (urgrep-command "foo" :tool tool :file-wildcard '("*.c" "*.h"))
        `(,@common-args "--include=*.c" "--include=*.h" "--heading" "--break"
                        "-i" "-F" "-e" "foo")))
-    (ert-info ("Directory")
+    (ert-info ("Root")
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "dir")
+       (urgrep-command "foo" :tool tool :root "dir")
        `(,@common-args "--heading" "--break" "-i" "-F" "-e" "foo" "dir"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
+       (urgrep-command "foo" :tool tool :root '("dir1" "dir2"))
        `(,@common-args "--heading" "--break" "-i" "-F" "-e" "foo" "dir1"
                        "dir2"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "~/dir")
+       (urgrep-command "foo" :tool tool :root "~/dir")
        `(,@common-args "--heading" "--break" "-i" "-F" "-e" "foo"
-                       ,(expand-file-name "~/dir"))))
+                       ,(expand-file-name "~/dir")))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :root nil)
+       `(,@common-args "--heading" "--break" "-i" "-F" "-e" "foo")))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -284,17 +287,20 @@ joined to compare against COMMAND."
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :file-wildcard '("*.c" "*.h"))
        `(,@common-args "-g" "*.c" "-g" "*.h" "--heading" "-i" "-F" "--" 
"foo")))
-    (ert-info ("Directory")
+    (ert-info ("Root")
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "dir")
+       (urgrep-command "foo" :tool tool :root "dir")
        `(,@common-args "--heading" "-i" "-F" "--" "foo" "dir"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
+       (urgrep-command "foo" :tool tool :root '("dir1" "dir2"))
        `(,@common-args "--heading" "-i" "-F" "--" "foo" "dir1" "dir2"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "~/dir")
+       (urgrep-command "foo" :tool tool :root "~/dir")
        `(,@common-args "--heading" "-i" "-F" "--" "foo"
-                       ,(expand-file-name "~/dir"))))
+                       ,(expand-file-name "~/dir")))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :root nil)
+       `(,@common-args "--heading" "-i" "-F" "--" "foo")))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -362,17 +368,20 @@ joined to compare against COMMAND."
        (urgrep-command "foo" :tool tool :file-wildcard '("*.c" "*.h"))
        `(,@common-args "-G" "^[^\\000]*\\.(c|h)$" "--group" "-i" "-Q" "--"
                        "foo")))
-    (ert-info ("Directory")
+    (ert-info ("Root")
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "dir")
+       (urgrep-command "foo" :tool tool :root "dir")
        `(,@common-args "--group" "-i" "-Q" "--" "foo" "dir"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
+       (urgrep-command "foo" :tool tool :root '("dir1" "dir2"))
        `(,@common-args "--group" "-i" "-Q" "--" "foo" "dir1" "dir2"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "~/dir")
+       (urgrep-command "foo" :tool tool :root "~/dir")
        `(,@common-args "--group" "-i" "-Q" "--" "foo"
-                       ,(expand-file-name "~/dir"))))
+                       ,(expand-file-name "~/dir")))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :root nil)
+       `(,@common-args "--group" "-i" "-Q" "--" "foo")))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -449,18 +458,21 @@ joined to compare against COMMAND."
        (urgrep-command "foo" :tool tool :file-wildcard '("*.c" "*.h"))
        `(,@common-args ,@no-hidden-args "-G" "^[^\\000]*\\.(c|h)$" "--group"
                        "-i" "-Q" "--" "foo")))
-    (ert-info ("Directory")
+    (ert-info ("Root")
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "dir")
+       (urgrep-command "foo" :tool tool :root "dir")
        `(,@common-args ,@no-hidden-args "--group" "-i" "-Q" "--" "foo" "dir"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
+       (urgrep-command "foo" :tool tool :root '("dir1" "dir2"))
        `(,@common-args ,@no-hidden-args "--group" "-i" "-Q" "--" "foo" "dir1"
                        "dir2"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "~/dir")
+       (urgrep-command "foo" :tool tool :root "~/dir")
        `(,@common-args ,@no-hidden-args "--group" "-i" "-Q" "--" "foo"
-                       ,(expand-file-name "~/dir"))))
+                       ,(expand-file-name "~/dir")))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :root nil)
+       `(,@common-args ,@no-hidden-args "--group" "-i" "-Q" "--" "foo")))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -546,27 +558,31 @@ joined to compare against COMMAND."
        (urgrep-command "foo" :tool tool :file-wildcard '("*.c" "*.h"))
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
                        "*.c" "*.h")))
-    (ert-info ("Directory")
+    (ert-info ("Root")
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "dir")
+       (urgrep-command "foo" :tool tool :root "dir")
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
                        "dir"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
+       (urgrep-command "foo" :tool tool :root '("dir1" "dir2"))
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
                        "dir1" "dir2"))
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :directory "~/dir")
+       (urgrep-command "foo" :tool tool :root "~/dir")
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
-                       ,(expand-file-name "~/dir"))))
+                       ,(expand-file-name "~/dir")))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :root nil)
+       `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--"
+                       ,@no-hidden-args)))
     (ert-info ("File wildcard + Directory")
       (urgrep-tests/check-command
-       (urgrep-command "foo" :tool tool :file-wildcard "*.el" :directory "dir")
+       (urgrep-command "foo" :tool tool :file-wildcard "*.el" :root "dir")
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
                        ":(glob)dir/**/*.el"))
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :file-wildcard '("*.c" "*.h")
-                       :directory '("dir1" "dir2"))
+                       :root '("dir1" "dir2"))
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
                        ":(glob)dir1/**/*.c" ":(glob)dir2/**/*.c"
                        ":(glob)dir1/**/*.h" ":(glob)dir2/**/*.h")))
@@ -643,17 +659,20 @@ joined to compare against COMMAND."
                          "--color=always -i -F" "foo")
                  (urgrep-command "foo" :tool tool
                                  :file-wildcard '("*.c" "*.h"))))))
-    (ert-info ("Directory")
+    (ert-info ("Root")
       (should (string-match
                (format template "dir" "" "--color=always -i -F" "foo")
-               (urgrep-command "foo" :tool tool :directory "dir")))
+               (urgrep-command "foo" :tool tool :root "dir")))
       (should (string-match
                (format template "dir1 dir2" "" "--color=always -i -F" "foo")
-               (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))))
+               (urgrep-command "foo" :tool tool :root '("dir1" "dir2"))))
       (should (string-match
                (format template (expand-file-name "~/dir") ""
                        "--color=always -i -F" "foo")
-               (urgrep-command "foo" :tool tool :directory "~/dir"))))
+               (urgrep-command "foo" :tool tool :root "~/dir")))
+      (should (string-match
+               "grep  --color=always -i -F\\(\\|.+ \\)foo"
+               (urgrep-command "foo" :tool tool :root nil))))
     (ert-info ("Color")
       (should (string-match (format template "." "" "+-i -F" "foo")
                             (urgrep-command "foo" :tool tool :color nil))))))
diff --git a/urgrep.el b/urgrep.el
index df86c219fb..71ed4e17d7 100644
--- a/urgrep.el
+++ b/urgrep.el
@@ -257,60 +257,75 @@ properties defined in the `urgrep-tools' entry for TOOL."
     (`(,b . ,a) (list (format "-B%d" b) (format "-A%d" a)))))
 
 (cl-defun urgrep--rgrep-command (query &key tool regexp case-fold hidden
-                                       file-wildcard directory context color
+                                       file-wildcard root context color
                                        &allow-other-keys)
   "Get the command to run for QUERY when using rgrep.
 Optional keys TOOL, REGEXP, CASE-FOLD, HIDDEN, FILE-WILDCARD,
-DIRECTORY, CONTEXT, and COLOR are as in `urgrep-command'."
+ROOT, CONTEXT, and COLOR are as in `urgrep-command'."
   (grep-compute-defaults)
-  ;; Locally add options to `grep-find-template' that grep.el isn't aware of.
-  (let ((grep-find-template grep-find-template)
+  (let ((case-fold-search nil)
         (grep-highlight-matches (if color 'always nil))
-        (file-wildcard (if file-wildcard (string-join file-wildcard " ") "*"))
-        (directory (when directory (urgrep--shell-join directory))))
-    (pcase-dolist (`(,k . ,v) `((regexp    . ,regexp)
-                                (case-fold . ,case-fold)
-                                (context   . ,context)))
-      (when-let ((args (urgrep--get-prop-pcase k tool v "-arguments"))
-                 (args (urgrep--shell-join args))
-                 ((string-match "<C>" grep-find-template)))
-        (setq grep-find-template
-              (replace-match (concat "<C> " args) t t grep-find-template))))
-    (let ((case-fold-search nil)
-          (grep-find-ignored-directories grep-find-ignored-directories)
-          (grep-find-ignored-files grep-find-ignored-files))
-      (unless hidden
-        (setq grep-find-ignored-directories
-              (cons ".*" (seq-filter (lambda (s) (not (string-prefix-p "." s)))
-                                     grep-find-ignored-directories))
-              grep-find-ignored-files
-              (cons ".*" (seq-filter (lambda (s) (not (string-prefix-p "." s)))
-                                     grep-find-ignored-files))))
-      (let ((command (rgrep-default-command query file-wildcard directory)))
-        (save-match-data
-          ;; Hide excessive part of rgrep command.
-          (when (string-match
-                 (rx bol "find " (group (*? nonl)) " " (or "-exec" "-print"))
-                 command)
-            (put-text-property (match-beginning 1) (match-end 1)
-                               'abbreviated-command t command)))
-        command))))
+        (extra-opts
+         (apply #'append
+                (mapcar (pcase-lambda (`(,k . ,v))
+                          (urgrep--get-prop-pcase k tool v "-arguments"))
+                        `((context   . ,context)
+                          (case-fold . ,case-fold)
+                          (regexp    . ,regexp))))))
+    (if root
+        (let ((file-wildcard
+               (if file-wildcard (string-join file-wildcard " ") "*"))
+              (dirs (urgrep--shell-join root))
+              (grep-find-template grep-find-template)
+              (grep-find-ignored-directories grep-find-ignored-directories)
+              (grep-find-ignored-files grep-find-ignored-files))
+          (save-match-data
+            (unless (string-match "<C>" grep-find-template)
+              (error "`grep-find-template' should have a <C> placeholder"))
+            ;; Locally add options to `grep-find-template' that grep.el isn't
+            ;; aware of.
+            (setq grep-find-template
+                  (replace-match (concat "<C> " (urgrep--shell-join 
extra-opts))
+                                 t t grep-find-template))
+            (unless hidden
+              ;; Ignore all dotfiles.
+              (let ((pred (lambda (s) (not (string-prefix-p "." s)))))
+                (setq grep-find-ignored-directories
+                      (cons ".*" (seq-filter
+                                  pred grep-find-ignored-directories))
+                      grep-find-ignored-files
+                      (cons ".*" (seq-filter pred grep-find-ignored-files)))))
+            (let ((command (rgrep-default-command query file-wildcard dirs)))
+              ;; Hide excessive part of rgrep command.
+              (when (string-match (rx bol "find " (group (*? nonl)) " "
+                                      (or "-exec" "-print"))
+                     command)
+                (put-text-property (match-beginning 1) (match-end 1)
+                                   'abbreviated-command t command))
+              command)))
+      ;; No ROOT, so do a regular `grep'.
+      (grep-expand-template grep-template query nil nil nil extra-opts))))
 
 (cl-defun urgrep--git-grep-command (query &key tool regexp case-fold hidden
-                                          file-wildcard directory group context
+                                          file-wildcard root group context
                                           color)
   "Get the command to run for QUERY when using git grep.
 Optional keys TOOL, REGEXP, CASE-FOLD, HIDDEN, FILE-WILDCARD,
-DIRECTORY, CONTEXT, and COLOR are as in `urgrep-command'."
+ROOT, CONTEXT, and COLOR are as in `urgrep-command'."
   (let ((pathspecs
-         (if (and file-wildcard directory)
-             (mapcan
-              (lambda (file)
-                (mapcar (lambda (dir)
-                          (concat ":(glob)" (file-name-concat dir "**" file)))
-                        directory))
-              file-wildcard)
-           (or file-wildcard directory))))
+         (cond
+          ((equal root '("."))
+           file-wildcard)
+          ((and file-wildcard root)
+           (mapcan
+            (lambda (file)
+              (mapcar (lambda (dir)
+                        (concat ":(glob)" (file-name-concat dir "**" file)))
+                      root))
+            file-wildcard))
+          (file-wildcard
+           (error ":file-wildcard expects a non-nil :root"))
+          (t root))))
     (urgrep--interpolate-arguments query tool
                                    `((regexp      . ,regexp)
                                      (case-fold   . ,case-fold)
@@ -337,7 +352,7 @@ See also `grep-process-setup'."
      (regexp-syntax bre ere pcre)
      (arguments executable (:abbreviate color "-rn" "--ignore-files")
                 hidden-file file-wildcard group context case-fold regexp "-e"
-                query directory)
+                query root)
      (regexp-arguments ('bre  '("-G"))
                        ('ere  '("-E"))
                        ('pcre '("-P"))
@@ -347,7 +362,8 @@ See also `grep-process-setup'."
      (file-wildcard-arguments
       ((and x (pred identity))
        (mapcar (lambda (i) (concat "--include=" i)) x)))
-     (directory-arguments (x x))
+     (root-arguments ('(".") nil)
+                     (x x))
      (group-arguments ((pred identity) '("--heading" "--break")))
      (context-arguments . ,urgrep--context-arguments)
      (color-arguments
@@ -358,14 +374,15 @@ See also `grep-process-setup'."
      (executable-name . "rg")
      (regexp-syntax pcre)
      (arguments executable (:abbreviate color) hidden-file file-wildcard group
-                context case-fold regexp "--" query directory)
+                context case-fold regexp "--" query root)
      (regexp-arguments ('nil '("-F")))
      (case-fold-arguments ((pred identity) '("-i")))
      (hidden-file-arguments ((pred identity) '("--hidden")))
      (file-wildcard-arguments
       ((and x (pred identity))
        (flatten-list (mapcar (lambda (i) (cons "-g" i)) x))))
-     (directory-arguments (x x))
+     (root-arguments ('(".") nil)
+                     (x x))
      (group-arguments ('nil '("--no-heading"))
                       (_    '("--heading")))
      (context-arguments . ,urgrep--context-arguments)
@@ -379,7 +396,7 @@ See also `grep-process-setup'."
      (executable-name . "ag")
      (regexp-syntax pcre)
      (arguments executable (:abbreviate color) hidden-file file-wildcard group
-                context case-fold regexp "--" query directory)
+                context case-fold regexp "--" query root)
      (regexp-arguments ('nil '("-Q")))
      (case-fold-arguments ('nil '("-s"))
                           (_    '("-i")))
@@ -387,7 +404,8 @@ See also `grep-process-setup'."
      (file-wildcard-arguments
       ((and x (pred identity))
        (list "-G" (urgrep--wildcards-to-regexp x 'pcre))))
-     (directory-arguments (x x))
+     (root-arguments ('(".") nil)
+                     (x x))
      (group-arguments ('nil '("--nogroup"))
                       (_    '("--group")))
      (context-arguments . ,urgrep--context-arguments)
@@ -398,7 +416,7 @@ See also `grep-process-setup'."
      (executable-name . "ack")
      (regexp-syntax pcre)
      (arguments executable (:abbreviate color) hidden-file file-wildcard group
-                context case-fold regexp "--" query directory)
+                context case-fold regexp "--" query root)
      (regexp-arguments ('nil '("-Q")))
      (case-fold-arguments ((pred identity) '("-i")))
      (hidden-file-arguments ('nil '("--ignore-dir=match:/^\\./"
@@ -406,7 +424,8 @@ See also `grep-process-setup'."
      (file-wildcard-arguments
       ((and x (pred identity))
        (list "-G" (urgrep--wildcards-to-regexp x 'pcre))))
-     (directory-arguments (x x))
+     (root-arguments ('(".") nil)
+                     (x x))
      (group-arguments ('nil '("--nogroup"))
                       (_    '("--group")))
      (context-arguments . ,urgrep--context-arguments)
@@ -611,7 +630,7 @@ it up in `urgrep-tools'.  Otherwise, return TOOL as-is."
 
 ;;;###autoload
 (cl-defun urgrep-command (query &key tool regexp (case-fold 'inherit) hidden
-                                file-wildcard directory (group t) (context 0)
+                                file-wildcard (root ".") (group t) (context 0)
                                 (color t))
   "Return a command to use to search for QUERY.
 Several keyword arguments can be supplied to adjust the resulting
@@ -634,8 +653,10 @@ HIDDEN: non-nil to search in hidden files; defaults to nil.
 FILE-WILDCARD: a wildcard (or list of wildcards) to limit the
 files searched.
 
-DIRECTORY: the directory (or list of directories) to search in;
-if nil (the default), search in `default-directory'.
+ROOT: a file/directory (or list thereof) to search in; by
+default, search within `default-directory'.  If nil, don't search
+anywhere; this is useful when making a template command, e.g. to
+use with \"xargs\".
 
 GROUP: show results grouped by filename (t, the default), or if
 nil, prefix the filename on each result line.
@@ -649,7 +670,7 @@ COLOR: non-nil (the default) if the output should use 
color."
   (with-connection-local-variables
    (let* ((regexp-syntax (if (eq regexp t) urgrep-regexp-syntax regexp))
           (file-wildcard (ensure-list file-wildcard))
-          (directory (mapcar #'urgrep--safe-file-name (ensure-list directory)))
+          (root (mapcar #'urgrep--safe-file-name (ensure-list root)))
           (tool (or (urgrep-get-tool tool)
                     (error "unknown tool %s" tool)))
           (tool-re-syntax (urgrep--get-best-syntax regexp-syntax tool))
@@ -664,14 +685,14 @@ COLOR: non-nil (the default) if the output should use 
color."
      (if cmd-fun
          (funcall cmd-fun query :tool tool :regexp tool-re-syntax
                   :case-fold case-fold :hidden hidden
-                  :file-wildcard file-wildcard :directory directory
+                  :file-wildcard file-wildcard :root root
                   :group group :context context :color color)
        (urgrep--interpolate-arguments query tool
                                       `((regexp        . ,tool-re-syntax)
                                         (case-fold     . ,case-fold)
                                         (hidden-file   . ,hidden)
                                         (file-wildcard . ,file-wildcard)
-                                        (directory     . ,directory)
+                                        (root          . ,root)
                                         (group         . ,group)
                                         (context       . ,context)
                                         (color         . ,color)))))))
@@ -1359,11 +1380,11 @@ This is meant to be used as a command in Eshell."
          "number of lines of trailing context to print")
      ;; General options
      (?h "help" nil nil "show this help message")
-     :usage "[OPTION]... PATTERN [PATH]...
-Recursively search for PATTERN within PATH.")
+     :usage "[OPTION]... PATTERN [ROOT]...
+Recursively search for PATTERN within ROOT.")
    (unless args (error "Expected a search pattern"))
    (let* ((query (car args))
-          (directory (cdr args))
+          (root (cdr args))
           (context
            (cond
             (context-around (string-to-number context-around))
@@ -1372,7 +1393,7 @@ Recursively search for PATTERN within PATH.")
                    (if context-after (string-to-number context-after) 0)))))
           options)
      ;; Fill the options to pass to `urgrep'.
-     (when directory (setq options `(:directory ,directory       . ,options)))
+     (when root      (setq options `(:root      ,root            . ,options)))
      (when context   (setq options `(:context   ,context         . ,options)))
      (when hidden    (setq options `(:hidden    ,(car hidden)    . ,options)))
      (when group     (setq options `(:group     ,(car group)     . ,options)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]