[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/pyim 610a0736d7 1/2: Simplify pyim-convert-string-at-po
From: |
ELPA Syncer |
Subject: |
[elpa] externals/pyim 610a0736d7 1/2: Simplify pyim-convert-string-at-point |
Date: |
Wed, 29 Jun 2022 21:57:53 -0400 (EDT) |
branch: externals/pyim
commit 610a0736d71f91c242257d7dba15204d41696557
Author: Feng Shu <tumashu@163.com>
Commit: Feng Shu <tumashu@163.com>
Simplify pyim-convert-string-at-point
---
pyim-process.el | 50 ++++++++++++++++++++++++++++++++++++++++++++----
pyim.el | 55 +++++++----------------------------------------------
tests/pyim-tests.el | 13 +++++++------
3 files changed, 60 insertions(+), 58 deletions(-)
diff --git a/pyim-process.el b/pyim-process.el
index a8921a345c..728096f7bd 100644
--- a/pyim-process.el
+++ b/pyim-process.el
@@ -383,6 +383,52 @@ imobj 组合构成在一起,构成了 imobjs 这个概念。比如:
(cl-decf pos)))))
end-position)))
+(defun pyim-process--string-at-region-or-point ()
+ (if mark-active
+ (buffer-substring-no-properties
+ (region-beginning) (region-end))
+ (buffer-substring (point) (line-beginning-position))))
+
+(defun pyim-process-feed-entered-at-point-into-pyim ()
+ (let* ((entered-info (pyim-process-find-entered-at-point))
+ (entered (nth 0 entered-info))
+ (char-num-need-delete (nth 1 entered-info)))
+ (pyim-process--delete-region-or-chars char-num-need-delete)
+ (when (> (length entered) 0)
+ (pyim-add-unread-command-events entered)
+ (pyim-process--force-input-chinese))))
+
+(defun pyim-process-find-entered-at-point ()
+ "从光标处提取一个有效的 entered 字符串."
+ (let* ((case-fold-search nil)
+ (scheme (pyim-scheme-current))
+ (first-chars (pyim-scheme-first-chars scheme))
+ (rest-chars (pyim-scheme-rest-chars scheme))
+ (regexp-used-to-extract-entered
+ (format "[%s]+ *$"
+ (cl-delete-duplicates
+ (concat first-chars rest-chars "'-"))))
+ (string (pyim-process--string-at-region-or-point)))
+ (when (string-match regexp-used-to-extract-entered string)
+ (let* ((entered (match-string 0 string))
+ ;; 一些编程语言使用单引号做为字符串的标记,这里需要特殊处理。
+ (entered (replace-regexp-in-string "^[-']" "" entered))
+ (backward-delete-char-number (length entered))
+ (entered (replace-regexp-in-string " +" "" entered)))
+ (list entered backward-delete-char-number)))))
+
+(defun pyim-process--delete-region-or-chars (&optional num)
+ "删除 region 或者光标之前 NUM 个字符。"
+ (if mark-active
+ (delete-region
+ (region-beginning) (region-end))
+ (when (and (numberp num) (> num 0))
+ (backward-delete-char num))))
+
+(defun pyim-process--force-input-chinese ()
+ "让 pyim 强制输入中文,忽略所有探针函数。"
+ (setq pyim-process--force-input-chinese t))
+
;; ** 中英文切换相关
(defun pyim-process-toggle-input-ascii ()
"pyim 切换中英文输入模式, 同时调整标点符号样式。"
@@ -390,10 +436,6 @@ imobj 组合构成在一起,构成了 imobjs 这个概念。比如:
(setq pyim-process--input-ascii
(not pyim-process--input-ascii)))
-(defun pyim-process-force-input-chinese ()
- "让 pyim 强制输入中文,忽略所有探针函数。"
- (setq pyim-process--force-input-chinese t))
-
(defun pyim-process-input-chinese-p ()
"确定 pyim 是否需要启动中文输入模式."
(let* ((scheme (pyim-scheme-current))
diff --git a/pyim.el b/pyim.el
index e8f1d8b4c0..631b0e4fb3 100644
--- a/pyim.el
+++ b/pyim.el
@@ -579,54 +579,13 @@ FILE 的格式与 `pyim-dcache-export' 生成的文件格式相同,
(interactive "P")
(unless (equal input-method-function 'pyim-input-method)
(activate-input-method 'pyim))
- (let ((string (pyim--string-at-region-or-point)))
- (cond
- ((region-active-p) (pyim-create-word-from-selection))
- ((pyim-process-trigger-feature-run-p) nil)
- ((pyim--find-code string) (pyim--convert-string string))
- (t (message "Pyim: pyim-convert-string-at-point did nothing.")))))
-
-(defun pyim--string-at-region-or-point ()
- (if mark-active
- (buffer-substring-no-properties
- (region-beginning) (region-end))
- (buffer-substring (point) (line-beginning-position))))
-
-(defun pyim--find-code (string)
- "从 STRING 末尾提取一个有效的 code."
- (let* ((case-fold-search nil)
- (scheme (pyim-scheme-current))
- (first-chars (pyim-scheme-first-chars scheme))
- (rest-chars (pyim-scheme-rest-chars scheme))
- (regexp-used-to-extract-code
- (format "[%s]+ *$"
- (cl-delete-duplicates
- (concat first-chars rest-chars "'-")))))
- (when (string-match regexp-used-to-extract-code string)
- (let* ((code (match-string 0 string))
- ;; 一些编程语言使用单引号做为字符串的标记,这里需要特殊处理。
- (code (replace-regexp-in-string "^[-']" "" code))
- (backward-delete-char-number (length code))
- (code (replace-regexp-in-string " +" "" code)))
- (list code backward-delete-char-number)))))
-
-(defun pyim--convert-string (string)
- (let* ((code-info-at-point (pyim--find-code string))
- (code (nth 0 code-info-at-point))
- (char-num-need-delete
- (nth 1 code-info-at-point)))
- (pyim--delete-region-or-chars char-num-need-delete)
- (when (> (length code) 0)
- (pyim-add-unread-command-events code)
- (pyim-process-force-input-chinese))))
-
-(defun pyim--delete-region-or-chars (&optional num)
- "删除 region 或者光标之前 NUM 个字符。"
- (if mark-active
- (delete-region
- (region-beginning) (region-end))
- (when (and (numberp num) (> num 0))
- (backward-delete-char num))))
+ (cond
+ ((region-active-p) (pyim-create-word-from-selection))
+ ;; `pyim-process-trigger-feature-run-p' 函数本身就会做相应的操作。
+ ((pyim-process-trigger-feature-run-p) nil)
+ ((pyim-process-find-entered-at-point)
+ (pyim-process-feed-entered-at-point-into-pyim))
+ (t (message "PYIM: `pyim-convert-string-at-point' did nothing."))))
;; ** 编码反查功能
(defun pyim-search-word-code ()
diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el
index 8208e85867..a59d22d1f9 100644
--- a/tests/pyim-tests.el
+++ b/tests/pyim-tests.el
@@ -2230,13 +2230,14 @@ abc 这是")))
(insert ",")
(should (pyim-process--trigger-punctuation-to-half-width-p)))))
-;; pyim.el 单元测试
-(ert-deftest pyim-tests-pyim--find-code ()
- (equal (pyim--find-code "123abc'd ")
- '("abc'd" 8))
- (equal (pyim--find-code "123'abcd ")
- '("abcd" 7)))
+(ert-deftest pyim-tests-pyim-process-find-entered-at-point ()
+ (with-temp-buffer
+ (insert "123abc'd ")
+ (should (equal (pyim-process-find-entered-at-point) '("abc'd" 8))))
+ (with-temp-buffer
+ (insert "123'abcd ")
+ (should (equal (pyim-process-find-entered-at-point) '("abcd" 7)))))
(ert-run-tests-batch-and-exit)