[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/pyim 71fff00caa 1/4: 添加异步获取词条功能并应用于 cloudim.
From: |
ELPA Syncer |
Subject: |
[elpa] externals/pyim 71fff00caa 1/4: 添加异步获取词条功能并应用于 cloudim. |
Date: |
Mon, 6 Jun 2022 23:57:52 -0400 (EDT) |
branch: externals/pyim
commit 71fff00caa6e6bb6b0011581b56a90d898212886
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>
添加异步获取词条功能并应用于 cloudim.
* pyim-process.el (pyim-process-get-candidates-async): New function.
(pyim-process-run-1): Use pyim-process-get-candidates-async.
* pyim-cloudim.el (pyim-candidates-create-async): Added.
(pyim-cloudim-url-retrieve-sync): Removed.
(pyim-candidates-create-async): Added.
* pyim-candidates.el (pyim-candidates-cloud-search): Removed.
(pyim-candidates-create-async): Added.
---
pyim-candidates.el | 17 ++++-----
pyim-cloudim.el | 102 +++++++++++++----------------------------------------
pyim-process.el | 17 +++++++++
3 files changed, 50 insertions(+), 86 deletions(-)
diff --git a/pyim-candidates.el b/pyim-candidates.el
index da6f0b2b49..d3b84f4360 100644
--- a/pyim-candidates.el
+++ b/pyim-candidates.el
@@ -302,18 +302,10 @@
orig-candidates
;; NOTE: 让第一个词保持不变是不是合理,有待进一步的观察。
`(,(car orig-candidates)
- ,@(pyim-candidates-cloud-search str scheme)
,@(pyim-candidates-search-buffer
(pyim-cregexp-create str scheme 3 t))
,@(cdr orig-candidates)))))
-(cl-defgeneric pyim-candidates-cloud-search (string scheme)
- "云搜索 STRING, 返回候选词条列表.")
-
-(cl-defmethod pyim-candidates-cloud-search (_string _scheme)
- "云搜索 STRING, 返回候选词条列表."
- nil)
-
(defun pyim-candidates-search-buffer (regexp)
"在当前 buffer 中使用 REGEXP 搜索词条。"
(when (not (input-pending-p)) ;只有在用户输入停顿的时候才搜索 buffer.
@@ -342,6 +334,15 @@
;; `pyim-candidates-create-delay' 方法来处理 imobjs。
(cl-call-next-method imobjs (pyim-scheme-get 'quanpin) orig-candidates))
+(cl-defgeneric pyim-candidates-create-async (imobjs scheme callback)
+ "按照 SCHEME, 使用异步的方式从 IMOBJS 获得候选词条。
+
+获取到的词条后,需要将其做为参数,调用 CALLBACK 函数。")
+
+(cl-defmethod pyim-candidates-create-async (_imobjs _scheme _callback)
+ "按照 SCHEME, 使用异步的方式从 IMOBJS 获得候选词条,默认什么也不做。"
+ nil)
+
;; * Footer
(provide 'pyim-candidates)
diff --git a/pyim-cloudim.el b/pyim-cloudim.el
index e9e1192102..0422f509de 100644
--- a/pyim-cloudim.el
+++ b/pyim-cloudim.el
@@ -42,75 +42,19 @@
(const :tag "Use baidu cloud input method." baidu)
(const :tag "Use google cloud input method." google)))
-(cl-defmethod pyim-candidates-cloud-search
- (string (_scheme pyim-scheme-quanpin)
+(cl-defmethod pyim-candidates-create-async
+ (imobjs (scheme pyim-scheme-quanpin) callback
&context (pyim-cloudim (eql baidu)))
- "使用 baidu 云输入法引擎搜索 STRING, 获取词条列表。"
- (let ((buffer (pyim-cloudim-url-retrieve-sync
- (format "https://olime.baidu.com/py?py=%s" string)
- t nil 0.2)))
- (when (bufferp buffer)
- (with-current-buffer buffer
- (prog1 (pyim-cloudim-parse-baidu-buffer)
- (kill-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-asynchronous=%s" url url-asynchronous)
- (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 pyim-cloudim-url-retrieve-sync:
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 pyim-cloudim-url-retrieve-sync:
%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)))
+ "按照 SCHEME, 使用异步的方式从 IMOBJS 获得候选词条,用于全拼输入法。
+这里使用 baidu 提供的云输入法接口获取词条。"
+ (let ((str (string-join (pyim-codes-create (car imobjs) scheme))))
+ (unless (< (length str) 1)
+ (url-retrieve
+ (format "https://olime.baidu.com/py?py=%s" str)
+ (lambda (_)
+ (funcall callback (cons imobjs (pyim-cloudim-parse-baidu-buffer)))
+ (kill-buffer))
+ nil t))))
(defun pyim-cloudim-parse-baidu-buffer ()
"解析 `pyim-cloudim-url-retrieve-sync' 返回的 baidu buffer."
@@ -125,17 +69,19 @@
(when (> (length word) 0)
(list (propertize word :comment "(云)")))))
-(cl-defmethod pyim-candidates-cloud-search
- (string (_scheme pyim-scheme-quanpin)
+(cl-defmethod pyim-candidates-create-async
+ (imobjs (scheme pyim-scheme-quanpin) callback
&context (pyim-cloudim (eql google)))
- "使用 google 云输入法引擎搜索 STRING, 获取词条列表。"
- (let ((buffer (pyim-cloudim-url-retrieve-sync
- (format
"https://www.google.cn/inputtools/request?ime=pinyin&text=%s" string)
- t nil 0.3)))
- (when (bufferp buffer)
- (with-current-buffer buffer
- (prog1 (pyim-cloudim-parse-google-buffer)
- (kill-buffer))))))
+ "按照 SCHEME, 使用异步的方式从 IMOBJS 获得候选词条,用于全拼输入法。
+这里使用 google 提供的云输入法接口获取词条。"
+ (let ((str (string-join (pyim-codes-create (car imobjs) scheme))))
+ (unless (< (length str) 1)
+ (url-retrieve
+ (format "https://www.google.cn/inputtools/request?ime=pinyin&text=%s"
str)
+ (lambda (_)
+ (funcall callback (cons imobjs (pyim-cloudim-parse-baidu-buffer)))
+ (kill-buffer))
+ nil t))))
(defun pyim-cloudim-parse-google-buffer ()
"解析 `pyim-cloudim-url-retrieve-sync' 返回的 google buffer."
diff --git a/pyim-process.el b/pyim-process.el
index 4d6ee630f8..3836909b28 100644
--- a/pyim-process.el
+++ b/pyim-process.el
@@ -287,6 +287,7 @@ entered (nihaom) 的第一个候选词。
(setq pyim-candidates
(or (delete-dups (pyim-candidates-create pyim-imobjs scheme))
(list entered-to-translate)))
+ (pyim-process-get-candidates-async)
(pyim-process-get-candidates-delay)
;; 自动上屏功能
(let ((autoselector-results
@@ -383,6 +384,22 @@ entered (nihaom) 的第一个候选词。
(cancel-timer pyim-process-get-candidates-delay-timer)
(setq pyim-process-get-candidates-delay-timer nil)))
+(defun pyim-process-get-candidates-async ()
+ "使用异步的方式获取候选词条词条。"
+ (let ((buffer (current-buffer)))
+ (pyim-candidates-create-async
+ pyim-imobjs (pyim-scheme-current)
+ (lambda (async-return)
+ (with-current-buffer buffer
+ (when (and pyim-process-translating
+ (equal (car async-return) pyim-imobjs))
+ (setq pyim-candidates
+ (delete-dups
+ `(,(car pyim-candidates)
+ ,@(cdr async-return)
+ ,@(cdr pyim-candidates)))))
+ (pyim-process-ui-refresh))))))
+
(defun pyim-process-get-candidates ()
pyim-candidates)