branch: externals/hyperbole commit 678f62a79dcf6c5247757ebfe5a81686fab13861 Author: Bob Weiner <r...@gnu.org> Commit: Bob Weiner <r...@gnu.org>
hyperbole-web-search: when service is a func, let it prompt for term --- ChangeLog | 6 ++++++ hsettings.el | 38 ++++++++++++++++++++++++-------------- test/hsettings-test.el | 17 +++++------------ 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1f318178bf..03c9b62577 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2024-01-20 Bob Weiner <r...@gnu.org> + +* hsettings.el (hyperbole-web-search, hyperbole-read-web-search-arguments): Update + to not prompt for search-term if the service is a function rather than a string. + In such cases, the function should prompt for the search term. + 2024-01-20 Mats Lidell <ma...@gnu.org> * Cleanup using flycheck resolving info and warning messages. diff --git a/hsettings.el b/hsettings.el index 87de9e9e4e..2a51793411 100644 --- a/hsettings.el +++ b/hsettings.el @@ -3,7 +3,7 @@ ;; Author: Bob Weiner ;; ;; Orig-Date: 15-Apr-91 at 00:48:49 -;; Last-Mod: 20-Jan-24 at 20:18:53 by Mats Lidell +;; Last-Mod: 20-Jan-24 at 15:05:31 by Bob Weiner ;; ;; SPDX-License-Identifier: GPL-3.0-or-later ;; @@ -160,22 +160,31 @@ ignores current line and always scrolls up or down a windowful." "Read from the keyboard a list of (web-search-service-string search-term-string). With optional non-empty SERVICE-NAME and SEARCH-TERM arguments, use those instead of reading from the keyboard." - (let ((completion-ignore-case t)) + (let ((completion-ignore-case t) + cmd-or-url) (while (or (not (stringp service-name)) (equal service-name "")) (setq service-name (completing-read "Search service: " hyperbole-web-search-alist nil t))) - (while (or (not (stringp search-term)) (equal search-term "")) - (setq search-term (read-string (format "Search %s for: " service-name) - (hyperbole-default-web-search-term)))) + (setq cmd-or-url (cdr (assoc service-name hyperbole-web-search-alist))) + (unless (functionp cmd-or-url) + (while (or (not (stringp search-term)) (equal search-term "")) + (setq search-term (read-string (format "Search %s for: " service-name) + (hyperbole-default-web-search-term))))) (list service-name search-term))) (defun hyperbole-web-search (&optional service-name search-term return-search-expr-flag) - "Search web SERVICE-NAME for SEARCH-TERM. -Both arguments are optional and are prompted for when not given or when null. -With optional RETURN-SEARCH-EXPR-FLAG return the search expression. -Uses `hyperbole-web-search-alist' to match each service to its search url. -Uses `hyperbole-web-search-browser-function' and the `browse-url' -package to display search results." + "Search web SERVICE-NAME for SEARCH-TERM (both arguments are optional). +With optional RETURN-SEARCH-EXPR-FLAG, return the search expression. + +Use `hyperbole-web-search-alist' to match each service to its search +url or function. +Use `hyperbole-web-search-browser-function' and the `browse-url' +package to display search results. + +If SERVICE-NAME is not given or is null and it is associated with +a function rather than a search url in `hyperbole-web-search-alist', +don't prompt for SEARCH-TERM; the function will prompt for that when +run." (interactive) (cl-multiple-value-bind (service-name search-term) (hyperbole-read-web-search-arguments service-name search-term) @@ -183,17 +192,18 @@ package to display search results." (search-pat (cdr (assoc service-name hyperbole-web-search-alist (lambda (service1 service2) (equal (downcase service1) (downcase service2))))))) - (setq search-term (browse-url-url-encode-chars search-term "[*\"()',=;?% ]")) + (unless (null search-term) + (setq search-term (browse-url-url-encode-chars search-term "[*\"()',=;?% ]"))) (if return-search-expr-flag (cond ((stringp search-pat) (format search-pat search-term)) ((functionp search-pat) - (list search-pat search-term)) + (list search-pat)) (t (user-error "(Hyperbole): Invalid web search service `%s'" service-name))) (cond ((stringp search-pat) (browse-url (format search-pat search-term))) ((functionp search-pat) - (funcall search-pat search-term)) + (funcall search-pat)) (t (user-error "(Hyperbole): Invalid web search service `%s'" service-name))))))) ;; This must be defined before the defcustom `inhbit-hyperbole-messaging'. diff --git a/test/hsettings-test.el b/test/hsettings-test.el index 380cdfcf3d..f319d58492 100644 --- a/test/hsettings-test.el +++ b/test/hsettings-test.el @@ -3,7 +3,7 @@ ;; Author: Mats Lidell ;; ;; Orig-Date: 20-Jan-24 at 12:28:01 -;; Last-Mod: 20-Jan-24 at 14:25:44 by Mats Lidell +;; Last-Mod: 20-Jan-24 at 15:12:46 by Bob Weiner ;; ;; SPDX-License-Identifier: GPL-3.0-or-later ;; @@ -25,22 +25,15 @@ "Verify `hyperbole-web-search´." (mocklet (((browse-url "http://www.google.com/search?q=hyperbole") => "return")) (should (string= (hyperbole-web-search "google" "hyperbole" nil) "return"))) - (should (string= (hyperbole-web-search "google" "hyperbole" t) "http://www.google.com/search?q=hyperbole")) + (should (string= (hyperbole-web-search "google" "hyperbole" t) + "http://www.google.com/search?q=hyperbole")) (should-error (hyperbole-web-search "unknown" "hyperbole" nil)) (should-error (hyperbole-web-search "unknown" "hyperbole" t)) - ;; Jump - ;; Fails due to being called with argument: See test below. - ;; (mocklet (((webjump) => "return")) - ;; (should (string= (hyperbole-web-search "Jump" "arg" nil) "return"))) - (should (equal (hyperbole-web-search "Jump" "arg" t) '(webjump "arg")))) - -(ert-deftest hsettings-test--hyperbole-web-search-webjump-called-with-arg () - "Verify `hyperbole-web-search´." - :expected-result :failed ;; Jump (mocklet (((webjump) => "return")) - (should (string= (hyperbole-web-search "Jump" "arg" nil) "return")))) + (should (string= (hyperbole-web-search "Jump" "arg" nil) "return"))) + (should (equal (hyperbole-web-search "Jump" "arg" t) '(webjump)))) (provide 'hsettings-test) ;;; hsettings-test.el ends here