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

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

[elpa] externals/kiwix c5b8f69 133/192: add a gender ping of kiwix-serve


From: Stefan Monnier
Subject: [elpa] externals/kiwix c5b8f69 133/192: add a gender ping of kiwix-serve before query
Date: Sat, 19 Dec 2020 00:41:47 -0500 (EST)

branch: externals/kiwix
commit c5b8f69fe67bff7e256bf333a0ae92cf6937f19f
Author: stardiviner <numbchild@gmail.com>
Commit: stardiviner <numbchild@gmail.com>

    add a gender ping of kiwix-serve before query
---
 kiwix.el | 49 +++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/kiwix.el b/kiwix.el
index 7c44345..896bd98 100644
--- a/kiwix.el
+++ b/kiwix.el
@@ -176,6 +176,9 @@
                ".html")))
     (browse-url url)))
 
+(defvar kiwix-server-available? nil
+  "The kiwix-server current available?")
+
 ;;;###autoload
 (defun kiwix-at-point (&optional interactively)
   "Search for the symbol at point with `kiwix-query'.
@@ -183,24 +186,34 @@
 Or When prefix argument `INTERACTIVELY' specified, then prompt
 for query string and library interactively."
   (interactive "P")
-  (let* ((library (if (or kiwix-search-interactively interactively)
-                      (kiwix-select-library)
-                    (kiwix--get-library-name kiwix-default-library)))
-         (query (if interactively
-                    (read-string "Kiwix Search: "
-                                 (if mark-active
-                                     (buffer-substring (region-beginning) 
(region-end))
-                                   (thing-at-point 'symbol)))
-                  (progn (if mark-active
-                             (buffer-substring (region-beginning) (region-end))
-                           (thing-at-point 'symbol))))))
-    (message (format "library: %s, query: %s" library query))
-    (if (or (null library)
-            (string-empty-p library)
-            (null query)
-            (string-empty-p query))
-        (error "Your query is invalid")
-      (kiwix-query query library))))
+  ;; ping kiwix-serve generally to make sure server available.
+  (if (> (length
+          (with-temp-buffer
+            (url-retrieve-synchronously kiwix-server-url)
+            (buffer-string)))
+         1)
+      (setq kiwix-server-available? nil)
+    (setq kiwix-server-available? t))
+  (if kiwix-server-available?
+      (let* ((library (if (or kiwix-search-interactively interactively)
+                          (kiwix-select-library)
+                        (kiwix--get-library-name kiwix-default-library)))
+             (query (if interactively
+                        (read-string "Kiwix Search: "
+                                     (if mark-active
+                                         (buffer-substring (region-beginning) 
(region-end))
+                                       (thing-at-point 'symbol)))
+                      (progn (if mark-active
+                                 (buffer-substring (region-beginning) 
(region-end))
+                               (thing-at-point 'symbol))))))
+        (message (format "library: %s, query: %s" library query))
+        (if (or (null library)
+                (string-empty-p library)
+                (null query)
+                (string-empty-p query))
+            (error "Your query is invalid")
+          (kiwix-query query library)))
+    (warn "kiwix-serve is not available, please start it at first.")))
 
 ;;;###autoload
 (defun kiwix-at-point-interactive ()



reply via email to

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