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

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

[elpa] externals/pyim 336c7ab 29/36: New file: pyim-outcome.el


From: ELPA Syncer
Subject: [elpa] externals/pyim 336c7ab 29/36: New file: pyim-outcome.el
Date: Thu, 22 Apr 2021 22:57:20 -0400 (EDT)

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

    New file: pyim-outcome.el
---
 pyim-outcome.el | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 pyim.el         | 196 +-----------------------------------------------
 2 files changed, 230 insertions(+), 195 deletions(-)

diff --git a/pyim-outcome.el b/pyim-outcome.el
new file mode 100644
index 0000000..c931d8b
--- /dev/null
+++ b/pyim-outcome.el
@@ -0,0 +1,229 @@
+;;; pyim-outcome.el --- outcome handle tools for pyim.        -*- 
lexical-binding: t; -*-
+
+;; * Header
+;; Copyright (C) 2021 Free Software Foundation, Inc.
+
+;; Author: Feng Shu <tumashu@163.com>
+;; Maintainer: Feng Shu <tumashu@163.com>
+;; URL: https://github.com/tumashu/pyim
+;; Keywords: convenience, Chinese, pinyin, input-method
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+;; * 代码                                                           :code:
+(require 'cl-lib)
+
+(defvar pyim-outcome-history nil
+  "记录 pyim outcome 的变化的历史
+
+在 pyim 中 outcome 代表用户通过输入法选择,并最终插入到 buffer
+的字符串。
+
+“一次确认就生成的词条” , 当前变量一般只有一个元素,比如:
+1. 输入: nihao
+2. 输出: 你好
+2. 变量取值为: (\"你好\")
+
+“多次确认才能生成词条” , 当前变量记录了选择的历史,比如:
+
+1. 输入: yiersansi
+2. 输出: 一二三四
+3. 第一次选择:一二
+4. 第二次选择:三
+5. 第三次选择:四
+6. 变量取值为: (\"一二三四\" \"一二三\" \"一二\")")
+
+;; ** 选词框相关函数
+(defun pyim-outcome-get (&optional n)
+  "获取 outcome"
+  (nth (or n 0) pyim-outcome-history))
+
+(defun pyim-outcome-handle (type)
+  "依照 TYPE, 获取 pyim 的 outcome,并将其加入 `pyim-outcome-history'."
+  (cond ((not enable-multibyte-characters)
+         (pyim-entered-erase-buffer)
+         (setq pyim-outcome-history nil)
+         (error "Can't input characters in current unibyte buffer"))
+        ((equal type "")
+         (setq pyim-outcome-history nil))
+        ((eq type 'last-char)
+         (push
+          (concat (pyim-outcome-get)
+                  (pyim-translate last-command-event))
+          pyim-outcome-history))
+        ((eq type 'candidate)
+         (let* ((candidate
+                 (pyim-candidate-parse
+                  (nth (1- pyim-candidate-position)
+                       pyim-candidates))))
+           (push
+            (concat (pyim-outcome-get) candidate)
+            pyim-outcome-history)))
+        ((eq type 'candidate-and-last-char)
+         (let* ((candidate
+                 (pyim-candidate-parse
+                  (nth (1- pyim-candidate-position)
+                       pyim-candidates))))
+           (push
+            (concat (pyim-outcome-get)
+                    candidate
+                    (pyim-translate last-command-event))
+            pyim-outcome-history)))
+        ((eq type 'pyim-entered)
+         (push (pyim-entered-get 'point-before) pyim-outcome-history))
+        (t (error "Pyim: invalid outcome"))))
+
+(defun pyim-translate-get-trigger-char ()
+  "检查 `pyim-translate-trigger-char' 是否为一个合理的 trigger char 。
+
+pyim 的 translate-trigger-char 要占用一个键位,为了防止用户
+自定义设置与输入法冲突,这里需要检查一下这个键位设置的是否合理,
+如果不合理,就返回输入法默认设定。"
+  (let* ((user-trigger-char pyim-translate-trigger-char)
+         (user-trigger-char
+          (if (characterp user-trigger-char)
+              (char-to-string user-trigger-char)
+            (when (= (length user-trigger-char) 1)
+              user-trigger-char)))
+         (first-char (pyim-scheme-get-option
+                      (pyim-scheme-name)
+                      :first-chars))
+         (prefer-trigger-chars (pyim-scheme-get-option
+                                (pyim-scheme-name)
+                                :prefer-trigger-chars)))
+    (if (pyim-string-match-p (regexp-quote user-trigger-char) first-char)
+        (progn
+          ;; (message "注意:pyim-translate-trigger-char 设置和当前输入法冲突,使用推荐设置:\"%s\""
+          ;;          prefer-trigger-chars)
+          prefer-trigger-chars)
+      user-trigger-char)))
+
+(defun pyim-translate (char)
+  "Pyim 字符转换函数,主要用于处理标点符号.
+
+pyim 在运行过程中调用这个函数来进行标点符号格式的转换。
+
+常用的标点符号数量不多,所以 pyim 没有使用文件而是使用一个变量
+`pyim-punctuation-dict' 来设置标点符号对应表,这个变量是一个
+alist 列表。"
+  (let* ((str (char-to-string char))
+         ;; 注意:`str' 是 *待输入* 的字符对应的字符串。
+         (str-before-1 (pyim-char-before-to-string 0))
+         (str-before-2 (pyim-char-before-to-string 1))
+         (str-before-3 (pyim-char-before-to-string 2))
+         ;; 从标点词库中搜索与 `str' 对应的标点列表。
+         (punc-list (assoc str pyim-punctuation-dict))
+         ;; 从标点词库中搜索与 `str-before-1' 对应的标点列表。
+         (punc-list-before-1
+          (cl-some (lambda (x)
+                     (when (member str-before-1 x) x))
+                   pyim-punctuation-dict))
+         ;; `str-before-1' 在其对应的标点列表中的位置。
+         (punc-posit-before-1
+          (cl-position str-before-1 punc-list-before-1
+                       :test #'equal))
+         (trigger-str (pyim-translate-get-trigger-char)))
+    (cond
+     ;; 空格之前的字符什么也不输入。
+     ((< char ? ) "")
+
+     ;; 这个部份与标点符号处理无关,主要用来快速删除用户自定义词条。
+     ;; 比如:在一个中文字符串后输入 2-v,可以将光标前两个中文字符
+     ;; 组成的字符串,从个人词库删除。
+     ((and (eq (char-before) ?-)
+           (pyim-string-match-p "[0-9]" str-before-2)
+           (pyim-string-match-p "\\cc" str-before-3)
+           (equal str trigger-str))
+      (delete-char -2)
+      (pyim-delete-word-at-point
+       (string-to-number str-before-2))
+      "")
+     ;; 这个部份与标点符号处理无关,主要用来快速保存用户自定义词条。
+     ;; 比如:在一个中文字符串后输入 2v,可以将光标前两个中文字符
+     ;; 组成的字符串,保存到个人词库。
+     ((and (member (char-before) (number-sequence ?2 ?9))
+           (pyim-string-match-p "\\cc" str-before-2)
+           (equal str trigger-str))
+      (delete-char -1)
+      (pyim-create-word-at-point
+       (string-to-number str-before-1))
+      "")
+
+     ;; 光标前面的字符为中文字符时,按 v 清洗当前行的内容。
+     ((and (not (numberp punc-posit-before-1))
+           (pyim-string-match-p "\\cc" str-before-1)
+           (equal str trigger-str))
+      (funcall pyim-wash-function)
+      "")
+
+     ;; 关闭标点转换功能时,只插入英文标点。
+     ((not (pyim-punctuation-full-width-p))
+      str)
+
+     ;; 当用户使用 org-mode 以及 markdown 等轻量级标记语言撰写文档时,
+     ;; 常常需要输入数字列表,比如:
+
+     ;; 1. item1
+     ;; 2. item2
+     ;; 3. item3
+
+     ;; 在这种情况下,数字后面输入句号必须是半角句号而不是全角句号,
+     ;; pyim 调用 `pyim-translate' 时,会检测光标前面的字符,如果这个
+     ;; 字符属于 `pyim-punctuation-escape-list' ,pyim 将输入半角标点,
+     ;; 具体细节见:`pyim-translate'
+     ((member (char-before)
+              pyim-punctuation-escape-list)
+      str)
+
+     ;; 当 `pyim-punctuation-half-width-functions' 中
+     ;; 任意一个函数返回值为 t 时,插入英文标点。
+     ((cl-some #'(lambda (x)
+                   (if (functionp x)
+                       (funcall x char)
+                     nil))
+               pyim-punctuation-half-width-functions)
+      str)
+
+     ;; 当光标前面为英文标点时, 按 `pyim-translate-trigger-char'
+     ;; 对应的字符后, 自动将其转换为对应的中文标点。
+     ((and (numberp punc-posit-before-1)
+           (= punc-posit-before-1 0)
+           (equal str trigger-str))
+      (pyim-punctuation-translate 'full-width)
+      "")
+
+     ;; 当光标前面为中文标点时, 按 `pyim-translate-trigger-char'
+     ;; 对应的字符后, 自动将其转换为对应的英文标点。
+     ((and (numberp punc-posit-before-1)
+           (> punc-posit-before-1 0)
+           (equal str trigger-str))
+      (pyim-punctuation-translate 'half-width)
+      "")
+
+     ;; 正常输入标点符号。
+     (punc-list
+      (pyim-punctuation-return-proper-punct punc-list))
+
+     ;; 当输入的字符不是标点符号时,原样插入。
+     (t str))))
+
+;; * Footer
+(provide 'pyim-outcome)
+
+;;; pyim-outcome.el ends here
diff --git a/pyim.el b/pyim.el
index 9a7d96e..d9e367f 100644
--- a/pyim.el
+++ b/pyim.el
@@ -588,6 +588,7 @@
 (require 'pyim-entered)
 (require 'pyim-candidates)
 (require 'pyim-preview)
+(require 'pyim-outcome)
 (require 'pyim-autoselector)
 
 (defgroup pyim nil
@@ -688,26 +689,6 @@ pyim 使用函数 `pyim-translate' 来处理特殊功能触发字符。当待输
 ;;;###autoload
 (defvar pyim-titles '("PYIM " "PYIM-EN " "PYIM-AU ") "Pyim 在 mode-line 
中显示的名称.")
 
-(defvar pyim-outcome-history nil
-  "记录 pyim outcome 的变化的历史
-
-在 pyim 中 outcome 代表用户通过输入法选择,并最终插入到 buffer
-的字符串。
-
-“一次确认就生成的词条” , 当前变量一般只有一个元素,比如:
-1. 输入: nihao
-2. 输出: 你好
-2. 变量取值为: (\"你好\")
-
-“多次确认才能生成词条” , 当前变量记录了选择的历史,比如:
-
-1. 输入: yiersansi
-2. 输出: 一二三四
-3. 第一次选择:一二
-4. 第二次选择:三
-5. 第三次选择:四
-6. 变量取值为: (\"一二三四\" \"一二三\" \"一二\")")
-
 (defvar pyim-input-ascii nil
   "是否开启 pyim 英文输入模式.")
 
@@ -1300,46 +1281,6 @@ Return the input string.
           (not pyim-assistant-scheme-enable))
     (pyim-entered-refresh)))
 
-;; ** 选词框相关函数
-(defun pyim-outcome-get (&optional n)
-  "获取 outcome"
-  (nth (or n 0) pyim-outcome-history))
-
-(defun pyim-outcome-handle (type)
-  "依照 TYPE, 获取 pyim 的 outcome,并将其加入 `pyim-outcome-history'."
-  (cond ((not enable-multibyte-characters)
-         (pyim-entered-erase-buffer)
-         (setq pyim-outcome-history nil)
-         (error "Can't input characters in current unibyte buffer"))
-        ((equal type "")
-         (setq pyim-outcome-history nil))
-        ((eq type 'last-char)
-         (push
-          (concat (pyim-outcome-get)
-                  (pyim-translate last-command-event))
-          pyim-outcome-history))
-        ((eq type 'candidate)
-         (let* ((candidate
-                 (pyim-candidate-parse
-                  (nth (1- pyim-candidate-position)
-                       pyim-candidates))))
-           (push
-            (concat (pyim-outcome-get) candidate)
-            pyim-outcome-history)))
-        ((eq type 'candidate-and-last-char)
-         (let* ((candidate
-                 (pyim-candidate-parse
-                  (nth (1- pyim-candidate-position)
-                       pyim-candidates))))
-           (push
-            (concat (pyim-outcome-get)
-                    candidate
-                    (pyim-translate last-command-event))
-            pyim-outcome-history)))
-        ((eq type 'pyim-entered)
-         (push (pyim-entered-get 'point-before) pyim-outcome-history))
-        (t (error "Pyim: invalid outcome"))))
-
 (define-obsolete-function-alias 'pyim-page-select-word-simple 
'pyim-select-word-simple "3.0")
 (defun pyim-select-word-simple ()
   "从选词框中选择当前词条.
@@ -1481,141 +1422,6 @@ Return the input string.
     ;; 不能用来选词了。
     (call-interactively #'pyim-self-insert-command)))
 
-;; ** 处理标点符号
-(defun pyim-translate-get-trigger-char ()
-  "检查 `pyim-translate-trigger-char' 是否为一个合理的 trigger char 。
-
-pyim 的 translate-trigger-char 要占用一个键位,为了防止用户
-自定义设置与输入法冲突,这里需要检查一下这个键位设置的是否合理,
-如果不合理,就返回输入法默认设定。"
-  (let* ((user-trigger-char pyim-translate-trigger-char)
-         (user-trigger-char
-          (if (characterp user-trigger-char)
-              (char-to-string user-trigger-char)
-            (when (= (length user-trigger-char) 1)
-              user-trigger-char)))
-         (first-char (pyim-scheme-get-option
-                      (pyim-scheme-name)
-                      :first-chars))
-         (prefer-trigger-chars (pyim-scheme-get-option
-                                (pyim-scheme-name)
-                                :prefer-trigger-chars)))
-    (if (pyim-string-match-p (regexp-quote user-trigger-char) first-char)
-        (progn
-          ;; (message "注意:pyim-translate-trigger-char 设置和当前输入法冲突,使用推荐设置:\"%s\""
-          ;;          prefer-trigger-chars)
-          prefer-trigger-chars)
-      user-trigger-char)))
-
-(defun pyim-translate (char)
-  "Pyim 字符转换函数,主要用于处理标点符号.
-
-pyim 在运行过程中调用这个函数来进行标点符号格式的转换。
-
-常用的标点符号数量不多,所以 pyim 没有使用文件而是使用一个变量
-`pyim-punctuation-dict' 来设置标点符号对应表,这个变量是一个
-alist 列表。"
-  (let* ((str (char-to-string char))
-         ;; 注意:`str' 是 *待输入* 的字符对应的字符串。
-         (str-before-1 (pyim-char-before-to-string 0))
-         (str-before-2 (pyim-char-before-to-string 1))
-         (str-before-3 (pyim-char-before-to-string 2))
-         ;; 从标点词库中搜索与 `str' 对应的标点列表。
-         (punc-list (assoc str pyim-punctuation-dict))
-         ;; 从标点词库中搜索与 `str-before-1' 对应的标点列表。
-         (punc-list-before-1
-          (cl-some (lambda (x)
-                     (when (member str-before-1 x) x))
-                   pyim-punctuation-dict))
-         ;; `str-before-1' 在其对应的标点列表中的位置。
-         (punc-posit-before-1
-          (cl-position str-before-1 punc-list-before-1
-                       :test #'equal))
-         (trigger-str (pyim-translate-get-trigger-char)))
-    (cond
-     ;; 空格之前的字符什么也不输入。
-     ((< char ? ) "")
-
-     ;; 这个部份与标点符号处理无关,主要用来快速删除用户自定义词条。
-     ;; 比如:在一个中文字符串后输入 2-v,可以将光标前两个中文字符
-     ;; 组成的字符串,从个人词库删除。
-     ((and (eq (char-before) ?-)
-           (pyim-string-match-p "[0-9]" str-before-2)
-           (pyim-string-match-p "\\cc" str-before-3)
-           (equal str trigger-str))
-      (delete-char -2)
-      (pyim-delete-word-at-point
-       (string-to-number str-before-2))
-      "")
-     ;; 这个部份与标点符号处理无关,主要用来快速保存用户自定义词条。
-     ;; 比如:在一个中文字符串后输入 2v,可以将光标前两个中文字符
-     ;; 组成的字符串,保存到个人词库。
-     ((and (member (char-before) (number-sequence ?2 ?9))
-           (pyim-string-match-p "\\cc" str-before-2)
-           (equal str trigger-str))
-      (delete-char -1)
-      (pyim-create-word-at-point
-       (string-to-number str-before-1))
-      "")
-
-     ;; 光标前面的字符为中文字符时,按 v 清洗当前行的内容。
-     ((and (not (numberp punc-posit-before-1))
-           (pyim-string-match-p "\\cc" str-before-1)
-           (equal str trigger-str))
-      (funcall pyim-wash-function)
-      "")
-
-     ;; 关闭标点转换功能时,只插入英文标点。
-     ((not (pyim-punctuation-full-width-p))
-      str)
-
-     ;; 当用户使用 org-mode 以及 markdown 等轻量级标记语言撰写文档时,
-     ;; 常常需要输入数字列表,比如:
-
-     ;; 1. item1
-     ;; 2. item2
-     ;; 3. item3
-
-     ;; 在这种情况下,数字后面输入句号必须是半角句号而不是全角句号,
-     ;; pyim 调用 `pyim-translate' 时,会检测光标前面的字符,如果这个
-     ;; 字符属于 `pyim-punctuation-escape-list' ,pyim 将输入半角标点,
-     ;; 具体细节见:`pyim-translate'
-     ((member (char-before)
-              pyim-punctuation-escape-list)
-      str)
-
-     ;; 当 `pyim-punctuation-half-width-functions' 中
-     ;; 任意一个函数返回值为 t 时,插入英文标点。
-     ((cl-some #'(lambda (x)
-                   (if (functionp x)
-                       (funcall x char)
-                     nil))
-               pyim-punctuation-half-width-functions)
-      str)
-
-     ;; 当光标前面为英文标点时, 按 `pyim-translate-trigger-char'
-     ;; 对应的字符后, 自动将其转换为对应的中文标点。
-     ((and (numberp punc-posit-before-1)
-           (= punc-posit-before-1 0)
-           (equal str trigger-str))
-      (pyim-punctuation-translate 'full-width)
-      "")
-
-     ;; 当光标前面为中文标点时, 按 `pyim-translate-trigger-char'
-     ;; 对应的字符后, 自动将其转换为对应的英文标点。
-     ((and (numberp punc-posit-before-1)
-           (> punc-posit-before-1 0)
-           (equal str trigger-str))
-      (pyim-punctuation-translate 'half-width)
-      "")
-
-     ;; 正常输入标点符号。
-     (punc-list
-      (pyim-punctuation-return-proper-punct punc-list))
-
-     ;; 当输入的字符不是标点符号时,原样插入。
-     (t str))))
-
 (defun pyim-wash-current-line-function ()
   "清理当前行的内容,比如:删除不必要的空格,等。"
   (interactive)



reply via email to

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