branch: externals/pyim commit 069ff6b6b039b16ccc135a4288eebfcd76a0fbd2 Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
Test function json-parse-buffer exist or not. * pyim-cloudim.el (pyim-cloudim-parse-baidu-buffer) (pyim-cloudim-parse-google-buffer): test function json-parse-buffer. --- README.org | 12 ++++++++++-- pyim-cloudim.el | 10 ++++++---- tests/pyim-tests.el | 34 ++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.org b/README.org index 50ad30fec3..002610c981 100644 --- a/README.org +++ b/README.org @@ -162,14 +162,22 @@ pyim 默认使用 pyim-basedict 词库, 这个词库的词条量8万左右,是 | C-DEL 或 C-BACKSPACE | 删除最后一个拼音 | | M-DEL 或 M-BACKSPACE | 删除最后一个拼音 | | F1,F2,F3,F4 | 以词定字 | -** 使用云拼音 -pyim 可以使用搜索引擎提供的云拼音服务,比如: +** 使用云输入法 +pyim 可以使用搜索引擎提供的云输入法服务,比如: #+begin_example (setq pyim-cloudim 'baidu) ;; (setq pyim-cloudim 'google) #+end_example +不过,由于 json 解析库的原因,Emacs 旧版本可能无法使用云输入法,可以通过下面的函 +数测试一下。 + +#+begin_example +(pyim-cloudim:baidu "nihao" 'pinyin) +#+end_example + + ** 使用双拼模式 pyim 支持双拼输入模式,用户可以通过变量 `pyim-default-scheme' 来设定: diff --git a/pyim-cloudim.el b/pyim-cloudim.el index cf8d347581..59498c5b2b 100644 --- a/pyim-cloudim.el +++ b/pyim-cloudim.el @@ -54,7 +54,8 @@ (defun pyim-cloudim:baidu (string input-method) "使用 baidu 云 INPUT-METHOD 输入法引擎搜索 STRING, 获取词条列表。" - (when (equal input-method 'pinyin) + (when (and (equal input-method 'pinyin) + (functionp 'json-parse-buffer)) (with-current-buffer (url-retrieve-synchronously (format "https://olime.baidu.com/py?py=%s" string) t nil 0.5) @@ -65,12 +66,13 @@ (goto-char (point-min)) (search-forward "\n\n" nil t) (delete-region (point-min) (point)) - (let ((data (json-parse-buffer))) + (let ((data (funcall 'json-parse-buffer))) (list (elt (elt (elt (gethash "0" data) 0) 0) 0)))) (defun pyim-cloudim:google (string input-method) "使用 google 云 INPUT-METHOD 输入法引擎搜索 STRING, 获取词条列表。" - (when (eq input-method 'pinyin) + (when (and (eq input-method 'pinyin) + (functionp 'json-parse-buffer)) (with-current-buffer (url-retrieve-synchronously (format "https://www.google.cn/inputtools/request?ime=pinyin&text=%s" string) t nil 0.5) @@ -81,7 +83,7 @@ (goto-char (point-min)) (search-forward "\n\n" nil t) (delete-region (point-min) (point)) - (let ((data (json-parse-buffer))) + (let ((data (funcall 'json-parse-buffer))) (list (elt (elt (elt (elt data 1) 0) 1) 0)))) ;; * Footer diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index 2d2e1a68fc..ead64cebce 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -939,17 +939,18 @@ yin-xing 因行 (should (eq (length words) 51)))) (ert-deftest pyim-tests-pyim-cloudim () - (with-temp-buffer - (insert "HTTP/1.1 200 OK + (when (functionp 'json-parse-buffer) + (with-temp-buffer + (insert "HTTP/1.1 200 OK Content-Length: 88 Content-Type: text/plain; charset=utf-8 Date: Sun, 08 May 2022 00:56:13 GMT {\"0\":[[[\"你好\",5,{\"pinyin\":\"ni'hao\",\"type\":\"IMEDICT\"}]]],\"1\":\"ni'hao\",\"result\":[null]}") - (should (equal (pyim-cloudim-parse-baidu-buffer) '("你好")))) + (should (equal (pyim-cloudim-parse-baidu-buffer) '("你好")))) - (with-temp-buffer - (insert "HTTP/1.1 200 OK + (with-temp-buffer + (insert "HTTP/1.1 200 OK Date: Sun, 08 May 2022 03:33:56 GMT Pragma: no-cache Expires: -1 @@ -965,22 +966,23 @@ Alt-Svc: h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma Transfer-Encoding: chunked [\"SUCCESS\",[[\"nihao\",[\"你好\"],[],{\"annotation\":[\"ni hao\"],\"candidate_type\":[0],\"lc\":[\"16 16\"]}]]]") - (should (equal (pyim-cloudim-parse-google-buffer) '("你好")))) + (should (equal (pyim-cloudim-parse-google-buffer) '("你好")))) - (should (equal (pyim-cloudim:baidu "nihao" 'pinyin) '("你好"))) - (should (equal (pyim-cloudim:google "nihao" 'pinyin) '("你好"))) + (when (not noninteractive) + (should (equal (pyim-cloudim:baidu "nihao" 'pinyin) '("你好"))) + (should (equal (pyim-cloudim:google "nihao" 'pinyin) '("你好"))) - (let ((pyim-cloudim 'baidu)) - (should (equal (pyim-cloudim "nihao" 'pinyin) '("你好")))) + (let ((pyim-cloudim 'baidu)) + (should (equal (pyim-cloudim "nihao" 'pinyin) '("你好")))) - (let ((pyim-cloudim 'google)) - (should (equal (pyim-cloudim "nihao" 'pinyin) '("你好")))) + (let ((pyim-cloudim 'google)) + (should (equal (pyim-cloudim "nihao" 'pinyin) '("你好")))) - (let ((pyim-cloudim 'xxx)) - (should (not (pyim-cloudim "nihao" 'pinyin)))) + (let ((pyim-cloudim 'xxx)) + (should (not (pyim-cloudim "nihao" 'pinyin)))) - (let ((pyim-cloudim nil)) - (should (not (pyim-cloudim "nihao" 'pinyin))))) + (let ((pyim-cloudim nil)) + (should (not (pyim-cloudim "nihao" 'pinyin))))))) (ert-run-tests-batch-and-exit) ;; * Footer