emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/pyim 89d75395f1 1/3: Add cregexp-support-p to scheme.


From: ELPA Syncer
Subject: [elpa] externals/pyim 89d75395f1 1/3: Add cregexp-support-p to scheme.
Date: Sat, 4 Jun 2022 21:57:48 -0400 (EDT)

branch: externals/pyim
commit 89d75395f15868b07529fb60c7493605a3b6df3e
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>

    Add cregexp-support-p to scheme.
    
            * tests/pyim-tests.el (pyim-tests-pyim-cregexp): Update.
    
            * pyim-scheme.el (pyim-scheme, quanpin, wubi, cangjie)
            (pyim-shuangpin, microsoft-shuangpin, xiaohe-shuangpin): Add 
:cregexp-support-p.
    
            * pyim-liberime.el (rime): Add :cregexp-support-p.
    
            * pyim-cregexp.el (pyim-cregexp-build): Add scheme argument.
            (pyim-cregexp-scheme): New function.
            (pyim-cregexp-build-1): Add scheme
---
 pyim-cregexp.el     | 24 ++++++++++++++++--------
 pyim-liberime.el    |  3 ++-
 pyim-scheme.el      | 16 ++++++++++++----
 tests/pyim-tests.el |  6 ++++++
 4 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/pyim-cregexp.el b/pyim-cregexp.el
index 9140b94ade..0769d62d7f 100644
--- a/pyim-cregexp.el
+++ b/pyim-cregexp.el
@@ -52,7 +52,7 @@
       (max (min num 4) 1)
     4))
 
-(defun pyim-cregexp-build (string &optional char-level-num chinese-only)
+(defun pyim-cregexp-build (string &optional char-level-num chinese-only scheme)
   "根据 STRING 构建一个中文 regexp, 用于 \"拼音搜索汉字\".
 
 比如:\"nihao\" -> \"[你呢...][好号...] \\| nihao\"
@@ -71,6 +71,7 @@ regexp, 所以搜索单字的时候一般可以搜到生僻字,但搜索句子
 就无法搜索生僻字了。"
   ;; NOTE: (rx-to-string "") will return "\\(?:\\)",
   ;; While I want (pyim-cregexp-build "") return just "".
+  (setq scheme (pyim-cregexp-scheme scheme))
   (if (equal string "")
       string
     (let ((num (pyim-cregexp-char-level-num char-level-num))
@@ -82,13 +83,25 @@ regexp, 所以搜索单字的时候一般可以搜到生僻字,但搜索句子
                      (pyim-cregexp-build-from-rx
                       (lambda (x)
                         (if (stringp x)
-                            (xr (pyim-cregexp-build-1 x num chinese-only))
+                            (xr (pyim-cregexp-build-1 x num chinese-only 
scheme))
                           x))
                       (xr string))))
                   string))
         (setq num (1- num)))
       rx-string)))
 
+(defun pyim-cregexp-scheme (&optional scheme)
+  "返回一个支持 cregexp 的 scheme.
+
+这个函数同时考虑 SCHEME, current scheme 和
+`pyim-cregexp-fallback-scheme'."
+  (let ((current-scheme (pyim-scheme-current)))
+    (cond
+     ((and scheme (pyim-scheme-cregexp-support-p scheme)) scheme)
+     ((and current-scheme (pyim-scheme-cregexp-support-p current-scheme))
+      current-scheme)
+     (t (pyim-scheme-get pyim-cregexp-fallback-scheme)))))
+
 (defun pyim-cregexp-valid-p (cregexp)
   "Return t when cregexp is a valid regexp."
   (and cregexp
@@ -109,9 +122,8 @@ regexp, 所以搜索单字的时候一般可以搜到生僻字,但搜索句子
              rx-form))
     (_ (funcall fn rx-form))))
 
-(defun pyim-cregexp-build-1 (str &optional char-level-num chinese-only)
+(defun pyim-cregexp-build-1 (str &optional char-level-num chinese-only scheme)
   (let* ((num (pyim-cregexp-char-level-num char-level-num))
-         (scheme (pyim-scheme-current))
          (code-prefix (pyim-scheme-code-prefix scheme))
          (sep "#####&&&&#####")
          (lst (remove "" (split-string
@@ -120,10 +132,6 @@ regexp, 所以搜索单字的时候一般可以搜到生僻字,但搜索句子
                           sep))))
     ;; 确保 pyim 词库加载
     (pyim-dcache-init-variables)
-    (unless (or (pyim-scheme-quanpin-p scheme)
-                (pyim-scheme-shuangpin-p scheme)
-                (pyim-scheme-xingma-p scheme))
-      (setq scheme (pyim-scheme-get pyim-cregexp-fallback-scheme)))
     (mapconcat
      (lambda (string)
        (if (or (pyim-string-match-p "[^a-z']+" string)
diff --git a/pyim-liberime.el b/pyim-liberime.el
index 70efcbe71b..84f5050ef1 100644
--- a/pyim-liberime.el
+++ b/pyim-liberime.el
@@ -67,7 +67,8 @@
    :code-prefix-history ("&")
    :first-chars "abcdefghijklmnopqrstuvwxyz"
    :rest-chars "abcdefghijklmnopqrstuvwxyz'-a"
-   :prefer-triggers nil))
+   :prefer-triggers nil
+   :cregexp-support-p nil))
 
 (pyim-scheme-add
  '(rime-quanpin
diff --git a/pyim-scheme.el b/pyim-scheme.el
index 718ad82bae..ffe81b6a7f 100644
--- a/pyim-scheme.el
+++ b/pyim-scheme.el
@@ -63,7 +63,8 @@
   first-chars
   rest-chars
   code-prefix
-  prefer-triggers)
+  prefer-triggers
+  cregexp-support-p)
 
 (cl-defstruct (pyim-scheme-quanpin
                (:include pyim-scheme)
@@ -157,7 +158,8 @@
    :class quanpin
    :first-chars "abcdefghijklmnopqrstuwxyz"
    :rest-chars "vmpfwckzyjqdltxuognbhsrei'-a"
-   :prefer-triggers ("v")))
+   :prefer-triggers ("v")
+   :cregexp-support-p t))
 
 (pyim-scheme-add
  '(wubi
@@ -169,7 +171,8 @@
    :code-prefix-history (".") ;五笔词库以前使用 "." 做为 code-prefix.
    :code-split-length 4 ;默认将用户输入切成 4 个字符长的 code 列表(不计算 code-prefix)
    :code-maximum-length 4 ;五笔词库中,code 的最大长度(不计算 code-prefix)
-   :prefer-triggers nil))
+   :prefer-triggers nil
+   :cregexp-support-p t))
 
 (pyim-scheme-add
  '(cangjie
@@ -181,7 +184,8 @@
    :code-prefix-history ("@") ;仓颉输入法词库曾经使用过的 code-prefix
    :code-split-length 5 ;默认将用户输入切成 5 个字符长的 code 列表(不计算 code-prefix)
    :code-maximum-length 5 ;仓颉词库中,code 的最大长度(不计算 code-prefix)
-   :prefer-triggers nil))
+   :prefer-triggers nil
+   :cregexp-support-p t))
 
 (pyim-scheme-add
  '(pyim-shuangpin
@@ -190,6 +194,7 @@
    :first-chars "abcdefghijklmnpqrstuvwxyz"
    :rest-chars "abcdefghijklmnopqrstuvwxyz"
    :prefer-triggers ("o")
+   :cregexp-support-p t
    :keymaps
    (("a" "a" "a")
     ("b" "b" "in")
@@ -238,6 +243,7 @@
    :first-chars "abcdefghijklmnopqrstuvwxyz"
    :rest-chars "abcdefghijklmnopqrstuvwxyz;"
    :prefer-triggers nil
+   :cregexp-support-p t
    :keymaps
    (("a" "a" "a")
     ("b" "b" "ou")
@@ -286,6 +292,7 @@
    :first-chars "abcdefghjklmnopqrstvwxyz"
    :rest-chars "abcdefghijklmnopqrstuvwxyz"
    :prefer-triggers nil
+   :cregexp-support-p t
    :keymaps
    (("q" "q" "ei")
     ("w" "w" "ian")
@@ -333,6 +340,7 @@
    :first-chars "abcdefghijklmnopqrstuvwxyz"
    :rest-chars "abcdefghijklmnopqrstuvwxyz"
    :prefer-triggers nil
+   :cregexp-support-p t
    :keymaps
    (("a" "a" "a")
     ("b" "b" "in")
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 50a964ebad..54cb1a5297 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -827,6 +827,12 @@
 
 ;; ** pyim-cregexp 相关单元测试
 (ert-deftest pyim-tests-pyim-cregexp ()
+  (let ((wubi (pyim-scheme-get 'wubi))
+        (pyim-default-scheme 'quanpin)
+        (pyim-cregexp-fallback-scheme 'wubi))
+    (should (equal (pyim-scheme-name (pyim-cregexp-scheme)) 'quanpin))
+    (should (equal (pyim-scheme-name (pyim-cregexp-scheme wubi)) 'wubi)))
+
   (let ((regexp (pyim-cregexp-build "nihao")))
     (should (string-match-p regexp "nihao"))
     (should (string-match-p regexp "anihaob"))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]