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

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

[elpa] externals/urgrep a4b543871a: Rework `urgrep-run-command`


From: ELPA Syncer
Subject: [elpa] externals/urgrep a4b543871a: Rework `urgrep-run-command`
Date: Sun, 6 Aug 2023 21:58:45 -0400 (EDT)

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

    Rework `urgrep-run-command`
    
    Now, the directory and tool are optional (the latter being guessed from the
    command string).
---
 NEWS.md         |  3 +++
 urgrep-tests.el | 16 +++++++++++++++-
 urgrep.el       | 30 +++++++++++++++++++++++++-----
 3 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/NEWS.md b/NEWS.md
index cfa43fb2a7..175c7b5f55 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -6,6 +6,9 @@
 - Add support for toggling whether to search in hidden files (`M-s h` in the
   search prompt, or `urgrep-search-hidden-files` globally)
 
+### Breaking changes
+- `urgrep-run-command` now takes `directory` and `tool` as optional keys
+
 ---
 
 ## v0.1.1 (2023-06-07)
diff --git a/urgrep-tests.el b/urgrep-tests.el
index 52de32c789..e4aca926ce 100644
--- a/urgrep-tests.el
+++ b/urgrep-tests.el
@@ -626,6 +626,20 @@ joined to compare against COMMAND."
       (should (equal (urgrep--get-prop 'executable-name tool) "gf"))
       (should (equal urgrep--cached-tool nil)))))
 
+(ert-deftest urgrep-tests/guess-tool/simple ()
+  (should (equal (car (urgrep--guess-tool "ag query")) 'ag))
+  (should (equal (car (urgrep--guess-tool "rg query")) 'ripgrep)))
+
+(ert-deftest urgrep-tests/guess-tool/list ()
+  (should (equal (car (urgrep--guess-tool "find query")) 'grep)))
+
+(ert-deftest urgrep-tests/guess-tool/fully-qualified ()
+  (should (equal (car (urgrep--guess-tool "/usr/bin/ag query")) 'ag))
+  (should (equal (car (urgrep--guess-tool "/home/me/bin/rg query")) 'ripgrep)))
+
+(ert-deftest urgrep-tests/guess-tool/error ()
+  (should-error (urgrep--guess-tool "goofy query")))
+
 (ert-deftest urgrep-tests/get-tool/remote-host ()
   (skip-unless (urgrep-tests/remote-accessible-p))
   (defvar ert-remote-temporary-file-directory)
@@ -677,7 +691,7 @@ joined to compare against COMMAND."
   (urgrep-tests/check-match-at-point))
 
 (ert-deftest urgrep-tests/urgrep-run-command ()
-  (switch-to-buffer (urgrep-run-command (urgrep-command "urgrep") nil nil))
+  (switch-to-buffer (urgrep-run-command (urgrep-command "urgrep")))
   (while (get-buffer-process (current-buffer))
     (accept-process-output))
   (should (and (equal urgrep-current-tool (urgrep-get-tool))
diff --git a/urgrep.el b/urgrep.el
index 26081fb5ac..579e8b19fc 100644
--- a/urgrep.el
+++ b/urgrep.el
@@ -515,6 +515,22 @@ in `urgrep-tools'.  Otherwise, return TOOL as-is."
     ((and (pred symbolp) tool) (assq tool urgrep-tools))
     (tool tool)))
 
+(defun urgrep--guess-tool (command)
+  "Guess the urgrep tool from the specified COMMAND."
+  (catch 'found
+    (when-let ((args (split-string-shell-command
+                      ;; First, split by semicolon, since
+                      ;; `split-string-shell-command' only returns the *last*
+                      ;; command.
+                      (car (split-string command ";"))))
+               (command-name (file-name-nondirectory (car args))))
+      (dolist (tool urgrep-tools)
+        (when (string= command-name
+                       (car (ensure-list (urgrep--get-prop
+                                          'executable-name tool))))
+          (throw 'found tool))))
+    (error "Unable to guess urgrep tool from command")))
+
 (defun urgrep--get-best-syntax (syntax tool)
   "Return the regexp syntax closest to SYNTAX that TOOL supports."
   (let ((tool-syntaxes (urgrep--get-prop 'regexp-syntax tool)))
@@ -1170,17 +1186,21 @@ searched."
     (urgrep--start command full-query tool directory)))
 
 ;;;###autoload
-(defun urgrep-run-command (command directory tool)
+(cl-defun urgrep-run-command (command &key directory tool)
   "Recursively search in DIRECTORY using the given COMMAND.
 
 When called interactively, this behaves like `urgrep', but allows you
 to edit the command before running it."
   (interactive
    (let* ((directory (urgrep--read-directory current-prefix-arg))
-          (query (urgrep--read-query nil :directory directory)))
-     (list (urgrep--read-command (apply #'urgrep-command query))
-           directory (plist-get (cdr query) :tool))))
-  (urgrep--start command command (urgrep-get-tool tool) directory))
+          (query (urgrep--read-query nil :directory directory))
+          (command (urgrep--read-command (apply #'urgrep-command query)))
+          (tool (condition-case nil (urgrep--guess-tool command)
+                  (error (plist-get (cdr query) :tool)))))
+     (list command :directory directory :tool tool)))
+  (let ((tool (if tool (urgrep-get-tool tool)
+                (urgrep--guess-tool command))))
+    (urgrep--start command command tool directory)))
 
 (cl-eval-when (compile)
   (require 'esh-cmd)



reply via email to

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