[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/pyim 1284b68d06 2/3: Add pyim-cloudim-url-retrieve-sync
From: |
ELPA Syncer |
Subject: |
[elpa] externals/pyim 1284b68d06 2/3: Add pyim-cloudim-url-retrieve-sync. |
Date: |
Wed, 11 May 2022 00:57:50 -0400 (EDT) |
branch: externals/pyim
commit 1284b68d06e149f2b146505fc52f30b0b41634b3
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>
Add pyim-cloudim-url-retrieve-sync.
* pyim-cloudim.el (pyim-cloudim-url-retrieve-sync): New function.
(pyim-cloudim:baidu, pyim-cloudim:google): Use above function.
---
pyim-cloudim.el | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 60 insertions(+), 2 deletions(-)
diff --git a/pyim-cloudim.el b/pyim-cloudim.el
index fe3097f329..9334865f7c 100644
--- a/pyim-cloudim.el
+++ b/pyim-cloudim.el
@@ -57,11 +57,69 @@
"使用 baidu 云输入法引擎搜索 STRING, 获取词条列表。"
(when (and (equal scheme-name 'quanpin)
(functionp 'json-parse-buffer))
- (with-current-buffer (url-retrieve-synchronously
+ (with-current-buffer (pyim-cloudim-url-retrieve-sync
(format "https://olime.baidu.com/py?py=%s" string)
t nil 0.1)
(pyim-cloudim-parse-baidu-buffer))))
+(defun pyim-cloudim-url-retrieve-sync (url &optional silent inhibit-cookies
timeout)
+ "Pyim 版本的 `url-retrieve-synchronously'.
+
+只有在用户输入停顿的时候,才能运行这个函数,用户如果再次输入,这
+个函数马上停止执行。"
+ (when (not (input-pending-p)) ;只有在用户输入停顿的时候才搜索 buffer.
+ (url-do-setup)
+ (let* (url-asynchronous
+ data-buffer
+ (callback (lambda (&rest _args)
+ (setq data-buffer (current-buffer))
+ (url-debug 'retrieval
+ "Synchronous fetching done (%S)"
+ data-buffer)))
+ (start-time (current-time))
+ (proc-buffer (url-retrieve url callback nil silent
+ inhibit-cookies)))
+ (if (not proc-buffer)
+ (url-debug 'retrieval "Synchronous fetching unnecessary %s" url)
+ (unwind-protect
+ (catch 'done
+ (while (and (not (input-pending-p)) ;如果用户继续输入,就停止云搜索。
+ (not data-buffer))
+ (when (and timeout (time-less-p timeout
+ (time-since start-time)))
+ (url-debug 'retrieval "Timed out %s (after %ss)" url
+ (float-time (time-since start-time)))
+ (throw 'done 'timeout))
+ (url-debug 'retrieval
+ "Spinning in url-retrieve-synchronously: nil
(%S)"
+ proc-buffer)
+ (when-let ((redirect-buffer
+ (buffer-local-value 'url-redirect-buffer
+ proc-buffer)))
+ (unless (eq redirect-buffer proc-buffer)
+ (url-debug
+ 'retrieval "Redirect in url-retrieve-synchronously: %S ->
%S"
+ proc-buffer redirect-buffer)
+ (let (kill-buffer-query-functions)
+ (kill-buffer proc-buffer))
+ ;; Accommodate hack in commit 55d1d8b.
+ (setq proc-buffer redirect-buffer)))
+ (when-let ((proc (get-buffer-process proc-buffer)))
+ (when (memq (process-status proc)
+ '(closed exit signal failed))
+ ;; Process sentinel vagaries occasionally cause
+ ;; url-retrieve to fail calling callback.
+ (unless data-buffer
+ (url-debug 'retrieval "Dead process %s" url)
+ (throw 'done 'exception))))
+ ;; Querying over consumer internet in the US takes 100
+ ;; ms, so split the difference.
+ (accept-process-output nil 0.05)))
+ (unless (eq data-buffer proc-buffer)
+ (let (kill-buffer-query-functions)
+ (kill-buffer proc-buffer)))))
+ data-buffer)))
+
(defun pyim-cloudim-parse-baidu-buffer ()
"解析 `url-retrieve-synchronously' 返回的 baidu buffer."
(goto-char (point-min))
@@ -75,7 +133,7 @@
"使用 google 云输入法引擎搜索 STRING, 获取词条列表。"
(when (and (eq scheme-name 'quanpin)
(functionp 'json-parse-buffer))
- (with-current-buffer (url-retrieve-synchronously
+ (with-current-buffer (pyim-cloudim-url-retrieve-sync
(format
"https://www.google.cn/inputtools/request?ime=pinyin&text=%s" string)
t nil 0.1)
(pyim-cloudim-parse-google-buffer))))