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

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

[elpa] externals/pyim 8bc937c: 添加以词定字功能


From: ELPA Syncer
Subject: [elpa] externals/pyim 8bc937c: 添加以词定字功能
Date: Fri, 17 Dec 2021 22:57:28 -0500 (EST)

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

    添加以词定字功能
    
        * pyim.el (pyim-mode-map): bind F1-F4.
        (pyim-input-method-1, pyim-select-word:pinyin): deal with subword.
        (pyim-select-subword-by-number): New command.
    
        * pyim-process.el (pyim-process-select-subword-p)
        (pyim-process-toggle-set-subword-info): New functions.
        (pyim-process-get-outcome): add use-subword argument.
    
        * pyim-preview.el (pyim-preview-refresh): handle subword.
    
        * pyim-page.el (pyim-page-menu-create): handle 
pyim-outcome-subword-info.
    
        * README.org (常用快捷键): add F1-F4.
    
        * pyim-outcome.el (pyim-outcome-get-subword): New function.
        (pyim-outcome-subword-info): New variable.
---
 README.org      |  1 +
 pyim-outcome.el | 13 +++++++++++++
 pyim-page.el    | 10 +++++++---
 pyim-preview.el |  4 +++-
 pyim-process.el | 21 +++++++++++++++++----
 pyim.el         | 27 +++++++++++++++++++++------
 6 files changed, 62 insertions(+), 14 deletions(-)

diff --git a/README.org b/README.org
index ef3e9e5..c75f5e9 100644
--- a/README.org
+++ b/README.org
@@ -165,6 +165,7 @@ pyim 当前的默认的拼音词库是 pyim-basedict, 这个词库的词条量8
 | DEL 或 BACKSPACE      | 删除最后一个字符           |
 | C-DEL 或  C-BACKSPACE | 删除最后一个拼音           |
 | M-DEL 或  M-BACKSPACE | 删除最后一个拼音           |
+| F1,F2,F3,F4           | 以词定字                   |
 
 ** 使用双拼模式
 pyim 支持双拼输入模式,用户可以通过变量 `pyim-default-scheme' 来设定:
diff --git a/pyim-outcome.el b/pyim-outcome.el
index e7253c6..fe178af 100644
--- a/pyim-outcome.el
+++ b/pyim-outcome.el
@@ -114,6 +114,9 @@ pyim 使用函数 `pyim-process-outcome-handle-char' 来处理特殊功能触发
 5. 第三次选择:四
 6. 变量取值为: (\"一二三四\" \"一二三\" \"一二\")")
 
+(defvar pyim-outcome-subword-info nil
+  "在以词定字功能中,用来保存字的位置。")
+
 (pyim-register-local-variables '(pyim-outcome-history))
 
 ;; ** 选词框相关函数
@@ -121,6 +124,16 @@ pyim 使用函数 `pyim-process-outcome-handle-char' 来处理特殊功能触发
   "获取 outcome"
   (nth (or n 0) pyim-outcome-history))
 
+(defun pyim-outcome-get-subword (word)
+  "根据 `pyim-outcome-subword-info' 的信息,获取 WORD 的一个子词条。"
+  (if pyim-outcome-subword-info
+      (let (output)
+        (dolist (i (sort pyim-outcome-subword-info #'>))
+          (when (<= i (length word))
+            (push (substring word (- i 1) i) output)))
+        (string-join output))
+    word))
+
 (defun pyim-outcome-get-trigger ()
   "检查 `pyim-outcome-trigger' 是否为一个合理的 trigger char 。
 
diff --git a/pyim-page.el b/pyim-page.el
index aca628e..389fe81 100644
--- a/pyim-page.el
+++ b/pyim-page.el
@@ -362,9 +362,13 @@ page 的概念,比如,上面的 “nihao” 的 *待选词列表* 就可以
   "这个函数用于创建在 page 中显示的备选词条菜单。"
   (let ((i 0) result)
     (dolist (candidate candidates)
-      (let ((str (if (consp candidate)
-                     (concat (car candidate) (cdr candidate))
-                   candidate)))
+      (let ((str (substring-no-properties
+                  (if (consp candidate)
+                      (concat (car candidate) (cdr candidate))
+                    candidate))))
+        (dolist (n pyim-outcome-subword-info)
+          (when (<= n (length str))
+            (set-text-properties (- n 1) n '(face highlight) str)))
         (setq i (1+ i))
         ;; 高亮当前选择的词条,用于 `pyim-page-next-word'
         (push
diff --git a/pyim-preview.el b/pyim-preview.el
index ef7e27c..4f7f461 100644
--- a/pyim-preview.el
+++ b/pyim-preview.el
@@ -84,7 +84,9 @@ pyim 会使用 Emacs overlay 机制在 *待输入buffer* 光标处高亮显示
                    "'")))
         (when (string< "" rest)
           (setq preview (concat preview rest)))))
-    (setq preview (pyim-magic-convert preview))
+    (setq preview
+          (pyim-magic-convert
+           (pyim-outcome-get-subword preview)))
     ;; Delete old preview string.
     (pyim-preview-delete-string)
     ;; Insert new preview string.
diff --git a/pyim-process.el b/pyim-process.el
index ee68c65..c205373 100644
--- a/pyim-process.el
+++ b/pyim-process.el
@@ -346,11 +346,24 @@
 (defun pyim-process-get-imobjs ()
   pyim-imobjs)
 
-(defun pyim-process-get-outcome (&optional n magic-convert)
+(defun pyim-process-select-subword-p ()
+  pyim-outcome-subword-info)
+
+(defun pyim-process-toggle-set-subword-info (n)
+  (if (member n pyim-outcome-subword-info)
+      (setq pyim-outcome-subword-info
+            (remove n pyim-outcome-subword-info))
+    (push n pyim-outcome-subword-info)))
+
+(defun pyim-process-get-outcome (&optional n magic-convert use-subword)
   "PYIM 流程的输出"
-  (if magic-convert
-      (pyim-magic-convert (pyim-outcome-get n))
-    (pyim-outcome-get n)))
+  (let ((str (pyim-outcome-get n)))
+    (when use-subword
+      (setq str (pyim-outcome-get-subword str))
+      (setq pyim-outcome-subword-info nil))
+    (when magic-convert
+      (setq str (pyim-magic-convert str)))
+    str))
 
 (defun pyim-process-outcome-handle (type)
   "依照 TYPE, 获取 pyim 的 outcome,并将其加入 `pyim-outcome-history'."
diff --git a/pyim.el b/pyim.el
index 27b655c..2024785 100644
--- a/pyim.el
+++ b/pyim.el
@@ -97,6 +97,13 @@ Tip: 用户也可以利用 `pyim-outcome-trigger-function-default' 函数
       (setq i (1+ i)))
     (dolist (i (number-sequence ?0 ?9))
       (define-key map (char-to-string i) #'pyim-select-word-by-number))
+    (dolist (x '(("<f1>" . 1)
+                 ("<f2>" . 2)
+                 ("<f3>" . 3)
+                 ("<f4>" . 4)))
+      (define-key map (kbd (car x)) (lambda ()
+                                      (interactive)
+                                      (pyim-select-subword-by-number (cdr 
x)))))
     (define-key map " " #'pyim-select-word)
     (define-key map (kbd "C-SPC") #'pyim-select-word-simple)
     (define-key map [backspace] #'pyim-delete-backward-char)
@@ -212,7 +219,7 @@ Tip: 用户也可以利用 `pyim-outcome-trigger-function-default' 函数
               ;; (message "unread-command-events: %s" unread-command-events)
               (pyim-process-terminate))))
         ;; (message "return: %s" (pyim-process-get-outcome))
-        (pyim-process-get-outcome nil t))
+        (pyim-process-get-outcome nil t t))
     ;; Since KEY doesn't start any translation, just return it.
     ;; But translate KEY if necessary.
     (char-to-string key)))
@@ -548,8 +555,9 @@ FILE 的格式与 `pyim-dcache-export' 生成的文件格式相同,
     ;; 在全拼输入法中,这个假设大多数情况是成立的,但在型码输入法
     ;; 中,比如五笔输入法,就不成立,好在型码输入法一般不需要多次
     ;; 选择。
-    (if (or (< length-selected-word (length imobj)) ;是否有未转换的光标前字符串
-            (> (length (pyim-process-get-entered 'point-after)) 0)) ;是否有光标后字符串
+    (if (and (not (pyim-process-select-subword-p)) ;以词定字的时候,不连续选择,处理起来太复杂。
+             (or (< length-selected-word (length imobj)) ;是否有未转换的光标前字符串
+                 (> (length (pyim-process-get-entered 'point-after)) 0))) 
;是否有光标后字符串
         (progn
           (pyim-process-with-entered-buffer
             ;; 把光标前已转换的 entered 字符串, 从 entered字符串里面剪
@@ -569,9 +577,10 @@ FILE 的格式与 `pyim-dcache-export' 生成的文件格式相同,
       ;;    这样的话,一个新词要输入两遍之后才可能出现在第一位。
       ;; 3. pyim 在启动的时候,会使用词频信息,对个人词库作一次排序。
       ;;    用作 pyim 下一次使用。
-      (if (member (pyim-process-get-outcome) (pyim-process-get-candidates))
-          (pyim-process-create-word (pyim-process-get-outcome) t)
-        (pyim-process-create-word (pyim-process-get-outcome)))
+      (unless (pyim-process-select-subword-p) ;NOTE: 
以词定字的时候,到底应不应该保存词条呢,需要进一步研究。
+        (if (member (pyim-process-get-outcome) (pyim-process-get-candidates))
+            (pyim-process-create-word (pyim-process-get-outcome) t)
+          (pyim-process-create-word (pyim-process-get-outcome))))
 
       (pyim-process-terminate)
       ;; pyim 使用这个 hook 来处理联想词。
@@ -622,6 +631,12 @@ FILE 的格式与 `pyim-dcache-export' 生成的文件格式相同,
     ;; 不能用来选词了。
     (call-interactively #'pyim-self-insert-command)))
 
+(defun pyim-select-subword-by-number (&optional n)
+  "以词定字功能。"
+  (interactive)
+  (pyim-process-toggle-set-subword-info (or n 1))
+  (pyim-process-run t))
+
 ;; ** 取消当前输入功能
 (defun pyim-quit-clear ()
   "取消当前输入的命令."



reply via email to

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