branch: externals/urgrep commit 5b792fe0de735589327e641cb86f7076c5261050 Author: Jim Porter <jporterb...@gmail.com> Commit: Jim Porter <jporterb...@gmail.com>
Use symbols instead of strings for `urgrep-tools' keys --- README.md | 4 ++-- urgrep-tests.el | 57 +++++++++++++++++++++++++++++++++------------------------ urgrep.el | 44 +++++++++++++++++++++----------------------- 3 files changed, 56 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 8105880812..c3b45dceaa 100644 --- a/README.md +++ b/README.md @@ -55,14 +55,14 @@ To improve performance, you can restrict the set of tools to search for by setting `urgrep-preferred-tools`: ```elisp -(setq urgrep-preferred-tools '("git-grep" "grep")) +(setq urgrep-preferred-tools '(git-grep grep)) ``` This also works with connection-local variables: ```elisp (connection-local-set-profile-variables 'urgrep-ripgrep - '((urgrep-preferred-tools . ("ripgrep")))) + '((urgrep-preferred-tools . (ripgrep)))) (connection-local-set-profiles '(:application tramp :machine "coco") 'urgrep-ripgrep) diff --git a/urgrep-tests.el b/urgrep-tests.el index f61ddd6e2f..6c2e550126 100644 --- a/urgrep-tests.el +++ b/urgrep-tests.el @@ -48,7 +48,7 @@ expected-arguments " ")))) (ert-deftest urgrep-tests-command-ripgrep () - (let ((tool (assoc "ripgrep" urgrep-tools)) + (let ((tool (assq 'ripgrep urgrep-tools)) (common-args '("rg" "--color" "always" "--colors" "path:fg:magenta" "--colors" "match:fg:red" "--colors" "match:style:bold"))) @@ -116,7 +116,7 @@ (append '("rg" "--color" "never" "--heading" "-i" "-F" "--" "foo"))))) (ert-deftest urgrep-tests-command-ag () - (let ((tool (assoc "ag" urgrep-tools)) + (let ((tool (assq 'ag urgrep-tools)) (common-args '("ag" "--color-path" "35" "--color-match" "1;31"))) ;; String/case (urgrep-test--check-command @@ -183,7 +183,7 @@ (append '("ag" "--nocolor" "--group" "-i" "-Q" "--" "foo"))))) (ert-deftest urgrep-tests-command-ack () - (let ((tool (assoc "ack" urgrep-tools)) + (let ((tool (assq 'ack urgrep-tools)) (common-args '("ack" "--color-filename" "magenta" "--color-match" "bold red"))) ;; String/case @@ -251,7 +251,7 @@ (append '("ack" "--nocolor" "--group" "-i" "-Q" "--" "foo"))))) (ert-deftest urgrep-tests-command-git-grep () - (let ((tool (assoc "git-grep" urgrep-tools)) + (let ((tool (assq 'git-grep urgrep-tools)) (common-args '("git" "--no-pager" "-c" "color.grep.filename=magenta" "-c" "color.grep.match=bold red" "grep" "--color" "-n" "--recurse-submodules")) @@ -322,7 +322,7 @@ '("-i" "-F" "-e" "foo" "--"))))) (ert-deftest urgrep-tests-command-grep () - (let ((tool (assoc "grep" urgrep-tools))) + (let ((tool (assq 'grep urgrep-tools))) ;; String/case (should (string-match "^find \\. .*grep --color=always -i -F .*foo" (urgrep-command "foo" :tool tool))) @@ -380,35 +380,44 @@ (ert-deftest urgrep-tests-get-tool-default () (cl-letf (((symbol-function #'executable-find) #'always)) - (let* ((urgrep--host-defaults '()) + (let* ((urgrep--host-defaults) (tool (urgrep-get-tool))) - (should (equal (car tool) "ripgrep")) - (should (equal (urgrep-get-property tool 'executable-name) "rg")) - (should (equal urgrep--host-defaults '((localhost . "ripgrep"))))))) + (should (equal (car tool) 'ripgrep)) + (should (equal (urgrep--get-prop 'executable-name tool) "rg")) + (should (equal urgrep--host-defaults '((localhost . ripgrep))))))) (ert-deftest urgrep-tests-get-tool-default-cached () (cl-letf (((symbol-function #'executable-find) #'always)) - (let* ((urgrep--host-defaults '((localhost . "ag"))) + (let* ((urgrep--host-defaults '((localhost . ag))) (tool (urgrep-get-tool))) - (should (equal (car tool) "ag")) - (should (equal (urgrep-get-property tool 'executable-name) "ag")) - (should (equal urgrep--host-defaults '((localhost . "ag"))))))) + (should (equal (car tool) 'ag)) + (should (equal (urgrep--get-prop 'executable-name tool) "ag")) + (should (equal urgrep--host-defaults '((localhost . ag))))))) -(ert-deftest urgrep-tests-get-tool-string () +(ert-deftest urgrep-tests-get-tool-preferred () (cl-letf (((symbol-function #'executable-find) #'always)) - (let* ((urgrep--host-defaults '()) - (tool (urgrep-get-tool "ag"))) - (should (equal (car tool) "ag")) - (should (equal (urgrep-get-property tool 'executable-name) "ag")) - (should (equal urgrep--host-defaults '()))))) + (let* ((urgrep--host-defaults) + (urgrep-preferred-tools '(ag grep)) + (tool (urgrep-get-tool))) + (should (equal (car tool) 'ag)) + (should (equal (urgrep--get-prop 'executable-name tool) "ag")) + (should (equal urgrep--host-defaults '((localhost . ag))))))) + +(ert-deftest urgrep-tests-get-tool-key () + (cl-letf (((symbol-function #'executable-find) #'always)) + (let* ((urgrep--host-defaults) + (tool (urgrep-get-tool 'ag))) + (should (equal (car tool) 'ag)) + (should (equal (urgrep--get-prop 'executable-name tool) "ag")) + (should (equal urgrep--host-defaults nil))))) (ert-deftest urgrep-tests-get-tool-cons () (cl-letf (((symbol-function #'executable-find) #'always)) - (let* ((urgrep--host-defaults '()) - (tool (urgrep-get-tool '("goofy" (executable-name "gf"))))) - (should (equal (car tool) "goofy")) - (should (equal (urgrep-get-property tool 'executable-name) "gf")) - (should (equal urgrep--host-defaults '()))))) + (let* ((urgrep--host-defaults) + (tool (urgrep-get-tool '(goofy (executable-name "gf"))))) + (should (equal (car tool) 'goofy)) + (should (equal (urgrep--get-prop 'executable-name tool) "gf")) + (should (equal urgrep--host-defaults nil))))) (defun urgrep-tests--check-match-at-point () (let* ((line (string-to-number (current-word))) diff --git a/urgrep.el b/urgrep.el index 48374bc4ea..03adb009eb 100644 --- a/urgrep.el +++ b/urgrep.el @@ -172,7 +172,7 @@ as in `urgrep-command'." (pcase-dolist (`(,k . ,v) `((regexp-arguments . ,regexp) (case-fold-arguments . ,case-fold) (context-arguments . ,context))) - (when-let ((args (urgrep-get-property-pcase tool k v)) + (when-let ((args (urgrep--get-prop-pcase k tool v)) (args (mapconcat #'urgrep--maybe-shell-quote-argument args " ")) ((string-match "<C>" grep-find-template))) @@ -193,7 +193,7 @@ See also `grep-process-setup'." (setenv "GREP_COLORS" "mt=01;31:fn=:ln=:bn=:se=:sl=:cx=:ne")) (defvar urgrep-tools - `(("ripgrep" + `((ripgrep (executable-name "rg") (regexp-syntax (pcre)) (arguments (executable color file-wildcards group context case-fold regexp @@ -210,7 +210,7 @@ See also `grep-process-setup'." (file-wildcards-arguments (((and x (pred identity)) (flatten-list (mapcar (lambda (i) (cons "-g" i)) x)))))) - ("ag" + (ag (executable-name "ag") (regexp-syntax (pcre)) (arguments (executable color file-wildcards group context case-fold regexp @@ -226,7 +226,7 @@ See also `grep-process-setup'." (file-wildcards-arguments (((and x (pred identity)) (list "-G" (urgrep--wildcards-to-regexp x 'pcre)))))) - ("ack" + (ack (executable-name "ack") (regexp-syntax (pcre)) (arguments (executable color file-wildcards group context case-fold regexp @@ -242,7 +242,7 @@ See also `grep-process-setup'." (file-wildcards-arguments (((and x (pred identity)) (list "-G" (urgrep--wildcards-to-regexp x 'pcre)))))) - ("git-grep" + (git-grep (executable-name "git") (vc-backend "Git") (regexp-syntax (bre ere pcre)) @@ -263,7 +263,7 @@ See also `grep-process-setup'." (_ '("-F")))) (case-fold-arguments (((pred identity) '("-i")))) (file-wildcards-arguments ((x x)))) - ("grep" + (grep (executable-name "grep") (regexp-syntax (bre ere pcre)) (command-function ,#'urgrep--rgrep-command) @@ -294,14 +294,13 @@ or a list of tool names to try in descending order of preference." This is an alist of host symbols (`localhost' or a TRAMP host) and the default tool to use on that host.") -(defun urgrep-get-property (tool prop) +(defun urgrep--get-prop (prop tool) "Get the property PROP from TOOL, or nil if PROP is undefined." - (when-let ((prop-entry (assoc prop (cdr tool)))) - (cadr prop-entry))) + (cadr (assq prop (cdr tool)))) -(defun urgrep-get-property-pcase (tool prop value) +(defun urgrep--get-prop-pcase (prop tool value) "Get the property PROP from TOOL and use it as a `pcase' macro for VALUE." - (when-let ((cases (urgrep-get-property tool prop)) + (when-let ((cases (urgrep--get-prop prop tool)) (block (append `(,#'pcase ',value) cases))) (eval block t))) @@ -310,13 +309,13 @@ the default tool to use on that host.") This caches the default tool per-host in `urgrep--host-defaults'." (if-let ((host-id (intern (or (file-remote-p default-directory) "localhost"))) (cached-tool-name (alist-get host-id urgrep--host-defaults))) - (assoc cached-tool-name urgrep-tools) + (assq cached-tool-name urgrep-tools) (let ((vc-backend-name) (saw-vc-tool-p nil)) (cl-dolist (tool (or urgrep-preferred-tools urgrep-tools)) - (let* ((tool (if (stringp tool) (assoc tool urgrep-tools) tool)) - (tool-executable (urgrep-get-property tool 'executable-name)) - (tool-vc-backend (urgrep-get-property tool 'vc-backend))) + (let* ((tool (if (symbolp tool) (assq tool urgrep-tools) tool)) + (tool-executable (urgrep--get-prop 'executable-name tool)) + (tool-vc-backend (urgrep--get-prop 'vc-backend tool))) (setq saw-vc-tool-p (or saw-vc-tool-p tool-vc-backend)) ;; Cache the VC backend name if we need it. (when-let (((and tool-vc-backend (not vc-backend-name))) @@ -342,7 +341,7 @@ If TOOL is nil, get the default tool. If TOOL is a string, look it up in `urgrep-tools'. Otherwise, return TOOL as-is." (pcase tool ('nil (urgrep--get-default-tool)) - ((and (pred stringp) tool) (assoc tool urgrep-tools)) + ((and (pred symbolp) tool) (assq tool urgrep-tools)) (tool tool))) (defun urgrep--maybe-shell-quote-argument (argument) @@ -357,7 +356,7 @@ for MS shells." (defun urgrep--get-best-syntax (syntax tool) "Return the regexp syntax closest to SYNTAX that TOOL supports." - (let ((tool-syntaxes (urgrep-get-property tool 'regexp-syntax))) + (let ((tool-syntaxes (urgrep--get-prop 'regexp-syntax tool))) (cond ((not syntax) nil) ((memq syntax tool-syntaxes) syntax) ((and (eq syntax 'ere) (memq 'pcre tool-syntaxes)) 'pcre) @@ -397,7 +396,7 @@ COLOR: non-nil (the default) if the output should use color." (tool (urgrep-get-tool tool)) (tool-re-syntax (urgrep--get-best-syntax regexp-syntax tool)) (query (urgrep--convert-regexp query regexp-syntax tool-re-syntax)) - (cmd-fun (urgrep-get-property tool 'command-function))) + (cmd-fun (urgrep--get-prop 'command-function tool))) ;; Determine whether to search case-sensitively or not. (when (eq case-fold 'inherit) (setq case-fold (if case-fold-search 'smart nil))) @@ -408,8 +407,8 @@ COLOR: non-nil (the default) if the output should use color." (funcall cmd-fun query :tool tool :regexp regexp-syntax :case-fold case-fold :context context :files files :color color) - (let* ((executable (urgrep-get-property tool 'executable-name)) - (arguments (urgrep-get-property tool 'arguments))) + (let* ((executable (urgrep--get-prop 'executable-name tool)) + (arguments (urgrep--get-prop 'arguments tool))) (setq arguments (cl-substitute executable 'executable arguments)) (setq arguments (cl-substitute query 'query arguments)) ;; Fill in various options according to the tool's argument syntax. @@ -420,7 +419,7 @@ COLOR: non-nil (the default) if the output should use color." (file-wildcards . ,files) (color . ,color))) (let* ((prop (intern (concat (symbol-name k) "-arguments"))) - (args (urgrep-get-property-pcase tool prop v))) + (args (urgrep--get-prop-pcase prop tool v))) (setq arguments (cl-substitute args k arguments)))) ;; FIXME: Inside compile and dired buffers, `shell-quote-argument' ;; doesn't handle TRAMP right... @@ -601,8 +600,7 @@ See `compilation-error-regexp-alist' for format details.") (defun urgrep-process-setup () "Set up compilation variables for urgrep." - (when-let ((tool-setup (urgrep-get-property urgrep-current-tool - 'process-setup))) + (when-let ((tool-setup (urgrep--get-prop 'process-setup urgrep-current-tool))) (funcall tool-setup)) (setq-local urgrep-num-matches-found 0 compilation-exit-message-function 'urgrep-exit-message))