我想要在Emacs输入候选项时,获得AJAX API提供的实时补全选项,从中选取。好像Helm有 个插件有类似的功能,忘记是啥了。不知道有没有人知道怎么怎么实现?
插件中如下代码
(defun kiwix-ping-server ()
"Ping Kiwix server to set `kiwix-server-available?' global state variable."
(request kiwix-server-url
:type "GET"
:sync t
:parser (lambda () (libxml-parse-html-region (point-min) (point-max)))
:success (function* (lambda (&key data &allow-other-keys)
(setq kiwix-server-available? t)))
:error (function* (lambda (&rest args &key error-thrown &allow-other-keys)
(setq kiwix-server-available? nil)))))
(defun kiwix-ajax-search-hints (input)
"Instantly AJAX request to get available Kiwix entry keywords
list and return a list result."
(let* ((ajax-api "http://127.0.0.1:8089/suggest?content=wikipedia_zh_all_2015-11&term=")
(ajax-url (concat ajax-api input))
(data (request-response-data
(request ajax-url
:type "GET"
:sync t
:headers '(("Content-Type" . "application/json"))
:parser #'json-read
:success (function*
(lambda (&key data &allow-other-keys)
(print data)))))))
(if (vectorp data)
(mapcar 'cdar data))))
;;;###autoload
(defun kiwix-at-point (&optional interactively)
"Search for the symbol at point with `kiwix-query'.
Or When prefix argument `INTERACTIVELY' specified, then prompt
for query string and library interactively."
(interactive "P")
(kiwix-ping-server)
(if kiwix-server-available?
(let* ((library (if (or kiwix-search-interactively interactively)
(kiwix-select-library)
(kiwix--get-library-name kiwix-default-library)))
(query (completing-read ; 如何实现实时搜索补全显示结果?
"Kiwix related entries: "
(kiwix-ajax-search-hints
(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."))
(setq kiwix-server-available? nil))
在 “; 如何实现实时搜索补全显示结果?” 位置,这个要怎么实现呢?需要用到 Ivy 和
Helm 么? completing-read
需要的第二个参数是 COLLECTION
. 怎么样让他变成函数
呢?这样就可以使用 kiwix-ajax-search-hints
了。