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

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

[elpa] externals/ivy-hydra 32c097a 004/395: counsel.el (counsel-search):


From: Basil L. Contovounesios
Subject: [elpa] externals/ivy-hydra 32c097a 004/395: counsel.el (counsel-search): Rename from counsel-google and add DDG
Date: Thu, 25 Feb 2021 08:31:18 -0500 (EST)

branch: externals/ivy-hydra
commit 32c097a07c6691751d6c9efd8dff6c3dfce545bc
Author: Oleh Krehel <ohwoeowho@gmail.com>
Commit: Oleh Krehel <ohwoeowho@gmail.com>

    counsel.el (counsel-search): Rename from counsel-google and add DDG
    
    * counsel.el (counsel-search): Require request and json.
    
    Fixes #2281
---
 counsel.el | 83 ++++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 59 insertions(+), 24 deletions(-)

diff --git a/counsel.el b/counsel.el
index 8e11fd4..3fe9231 100644
--- a/counsel.el
+++ b/counsel.el
@@ -6103,38 +6103,73 @@ Additional actions:\\<ivy-minibuffer-map>
             :action #'counsel-M-x-action
             :caller 'counsel-major))
 
-;;* `counsel-google'
+;;* `counsel-search'
 (declare-function request "ext:request")
-(defun counsel-google-function (input)
-  "Create a request to Google with INPUT.
+
+(defcustom counsel-search-engine 'ddg
+  "The search engine choice in `counsel-search-engines-alist'."
+  :type '(choice
+          (const ddg)
+          (const google)))
+
+(defcustom counsel-search-engines-alist
+  '((google
+     "http://suggestqueries.google.com/complete/search";
+     "https://www.google.com/search?q=";
+     #'counsel--search-request-data-google)
+    (ddg
+     "https://duckduckgo.com/ac/";
+     "https://duckduckgo.com/html/?q=";
+     #'counsel--search-request-data-ddg))
+  "Search engine parameters for `counsel-search'."
+  :type '(list))
+
+(defun counsel--search-request-data-google (data)
+  (mapcar #'identity (aref data 1)))
+
+(defun counsel--search-request-data-ddg (data)
+  (mapcar #'cdar data))
+
+(defun counsel-search-function (input)
+  "Create a request to a search engine with INPUT.
 Return 0 tells `ivy--exhibit' not to update the minibuffer.
 We update it in the callback with `ivy-update-candidates'."
   (or
    (ivy-more-chars)
    (progn
-     (require 'request)
-     (require 'json)
-     (request
-      "http://suggestqueries.google.com/complete/search";
-      :type "GET"
-      :params (list
-               (cons "client" "firefox")
-               (cons "q" input))
-      :parser 'json-read
-      :success (cl-function
-                (lambda (&key data &allow-other-keys)
-                  (ivy-update-candidates
-                   (mapcar #'identity (aref data 1))))))
-     0)))
-
-(defun counsel-google ()
-  "Ivy interface for Google."
+     (let ((engine (cdr (assoc counsel-search-engine 
counsel-search-engines-alist))))
+       (request
+        (nth 0 engine)
+        :type "GET"
+        :params (list
+                 (cons "client" "firefox")
+                 (cons "q" input))
+        :parser 'json-read
+        :success (cl-function
+                  (lambda (&key data &allow-other-keys)
+                    (ivy-update-candidates
+                     (funcall (nth 2 engine) data)))))
+       0))))
+
+(defun counsel-search-action (x)
+  "Search for X."
+  (browse-url
+   (concat
+    (nth 2 (assoc counsel-search-engine counsel-search-engines-alist))
+    x)))
+
+(defun counsel-search ()
+  "Ivy interface for dynamically querying a search engine."
   (interactive)
-  (ivy-read "search: " #'counsel-google-function
-            :action (lambda (x)
-                      (browse-url (concat "https://www.google.com/search?q="; 
x)))
+  (require 'request)
+  (require 'json)
+  (ivy-read "search: " #'counsel-search-function
+            :action #'counsel-search-action
             :dynamic-collection t
-            :caller 'counsel-google))
+            :caller 'counsel-search))
+
+(define-obsolete-function-alias 'counsel-google
+    'counsel-search "<2019-10-17 Thu>")
 
 ;;* `counsel-mode'
 (defvar counsel-mode-map



reply via email to

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