[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/kiwix c871bef 07/18: refactor function kiwix-at-point t
From: |
Stefan Monnier |
Subject: |
[elpa] externals/kiwix c871bef 07/18: refactor function kiwix-at-point to separate into smaller functional functions |
Date: |
Wed, 4 Aug 2021 18:28:09 -0400 (EDT) |
branch: externals/kiwix
commit c871befcd37a37f182c36dcf94b9b93e2870e89e
Author: stardiviner <numbchild@gmail.com>
Commit: stardiviner <numbchild@gmail.com>
refactor function kiwix-at-point to separate into smaller functional
functions
---
kiwix.el | 133 ++++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 72 insertions(+), 61 deletions(-)
diff --git a/kiwix.el b/kiwix.el
index 5c0dffe..9279cee 100644
--- a/kiwix.el
+++ b/kiwix.el
@@ -192,7 +192,7 @@ Set it to ‘t’ will use Emacs built-in ‘completing-read’."
(defun kiwix-query (query &optional selected-library)
"Search `QUERY' in `LIBRARY' with Kiwix."
(let* ((library (or selected-library (kiwix--get-library-name
selected-library)))
- (url (concat (format "%s:%s" kiwix-server-url (number-to-string
kiwix-server-port))
+ (url (concat (format "%s:%s" kiwix-server-url kiwix-server-port)
"/search?content=" library "&pattern="
(url-hexify-string query)))
(browse-url-browser-function kiwix-default-browser-function))
(browse-url url)))
@@ -237,7 +237,7 @@ list and return a list result."
(let* ((library (or selected-library
(kiwix--get-library-name selected-library)))
(ajax-api (format "%s:%s/suggest?content=%s&term="
- kiwix-server-url (number-to-string
kiwix-server-port)
+ kiwix-server-url kiwix-server-port
library))
(ajax-url (concat ajax-api input))
(data (request-response-data
@@ -259,71 +259,82 @@ list and return a list result."
(region-beginning) (region-end))
(thing-at-point 'symbol)))
+(defun kiwix--ajax-select-available-hints (zim-library)
+ "AJAX search hints on the selected library and select one term from
available hints."
+ (pcase kiwix-default-completing-read
+ ('selectrum
+ (require 'selectrum)
+ (require 'consult)
+ (consult--read
+ (lambda (input)
+ (apply #'kiwix--ajax-search-hints
+ input `(,zim-library)))
+ :prompt "Kiwix related entries: "
+ :require-match nil))
+ ('ivy
+ (require 'ivy)
+ (ivy-read
+ "Kiwix related entries: "
+ (lambda (input)
+ (apply #'kiwix--ajax-search-hints
+ input `(,zim-library)))
+ :predicate nil
+ :require-match nil
+ :initial-input (kiwix--get-thing-at-point)
+ :preselect nil
+ :def nil
+ :history nil
+ :keymap nil
+ :update-fn 'auto
+ :sort t
+ :dynamic-collection t
+ :caller 'ivy-done))
+ ('helm
+ (require 'helm)
+ (helm
+ :source (helm-build-async-source "kiwix-helm-search-hints"
+ :candidates-process
+ (lambda (input)
+ (apply #'kiwix--ajax-search-hints
+ input `(,zim-library))))
+ :input (kiwix--get-thing-at-point)
+ :buffer "*helm kiwix completion candidates*"))
+ (_
+ (completing-read
+ "Kiwix related entries: "
+ ;; FIXME: This needs work!
+ (completion-table-dynamic
+ (lambda (input)
+ (apply #'kiwix--ajax-search-hints
+ input `(,zim-library))))
+ nil nil
+ (kiwix--get-thing-at-point)))))
+
+(defun kiwix-search-at-library (zim-library query)
+ (interactive (let ((zim-library (kiwix-select-library)))
+ (list zim-library (kiwix--ajax-select-available-hints
zim-library))))
+ (message (format "library: %s, query: %s" zim-library query))
+ (if (or (null zim-library)
+ (string-empty-p zim-library)
+ (null query)
+ (string-empty-p query))
+ (error "Your query is invalid")
+ (kiwix-query query zim-library)))
+
+(defun kiwix-search-full-context (query)
+ "Full context search QUERY in all kiwix ZIM libraries."
+ (interactive
+ (list (read-string "kiwix full context search in all libraries: ")))
+ (browse-url (format "%s:%s/search?pattern=" kiwix-server-url
kiwix-server-port query)))
+
;;;###autoload
(defun kiwix-at-point ()
- "Search for the symbol at point with `kiwix-query'."
+ "Search for the symbol at point with `kiwix-search-at-library'."
(interactive)
(unless (kiwix-ping-server)
(kiwix-launch-server))
(if kiwix-server-available?
- (progn
- (setq kiwix--selected-library (kiwix-select-library))
- (let* ((library kiwix--selected-library)
- (query (pcase kiwix-default-completing-read
- ('selectrum
- (require 'selectrum)
- (require 'consult)
- (consult--read
- (lambda (input)
- (apply #'kiwix-ajax-search-hints
- input `(,kiwix--selected-library)))
- :prompt "Kiwix related entries: "
- :require-match nil))
- ('ivy
- (require 'ivy)
- (ivy-read
- "Kiwix related entries: "
- (lambda (input)
- (apply #'kiwix-ajax-search-hints
- input `(,kiwix--selected-library)))
- :predicate nil
- :require-match nil
- :initial-input (kiwix--get-thing-at-point)
- :preselect nil
- :def nil
- :history nil
- :keymap nil
- :update-fn 'auto
- :sort t
- :dynamic-collection t
- :caller 'ivy-done))
- ('helm
- (require 'helm)
- (helm
- :source (helm-build-async-source
"kiwix-helm-search-hints"
- :candidates-process
- (lambda (input)
- (apply #'kiwix-ajax-search-hints
- input
`(,kiwix--selected-library))))
- :input (kiwix--get-thing-at-point)
- :buffer "*helm kiwix completion candidates*"))
- (_
- (completing-read
- "Kiwix related entries: "
- ;; FIXME: This needs work!
- (completion-table-dynamic
- (lambda (input)
- (apply #'kiwix-ajax-search-hints
- input `(,kiwix--selected-library))))
- nil nil
- (kiwix--get-thing-at-point))))))
- (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))))
+ (call-interactively 'kiwix-search-at-library)
(warn "kiwix-serve is not available, please start it at first."))
(setq kiwix-server-available? nil))
- [elpa] externals/kiwix updated (0c5e161 -> 71e770c), Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix c273429 06/18: refactor and remove the variable `kiwix--selected-library', Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix ca03a65 01/18: use Docker server by default, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix c871bef 07/18: refactor function kiwix-at-point to separate into smaller functional functions,
Stefan Monnier <=
- [elpa] externals/kiwix 7e0b82c 04/18: refresh ZIM libraries list once-time per package loading, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix 12e13c8 02/18: support remote Docker service, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix 423f607 05/18: Concentrate all kiwix library dir variables into only one, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix 716f3c4 12/18: Add condition to detect kiwix library path exist to fix GitHub Actions, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix e191c31 09/18: improve function kiwix-get-libraries to support remote Docker server, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix 0ec84f3 03/18: use dynamic ZIM library path, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix 3330d7e 08/18: Add extra library "elquery" for parsing HTTP query HTML result, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix a33c4a2 16/18: Fix kiwix-libraries variable not initialized by function kiwix-get-libraries, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix 0f85022 17/18: Add autoload cookies and improve docstring, Stefan Monnier, 2021/08/04
- [elpa] externals/kiwix f49557d 11/18: Fix format three fields, Stefan Monnier, 2021/08/04