forked from tumashu/pyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyim-autoselector.el
76 lines (61 loc) · 2.89 KB
/
pyim-autoselector.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;; pyim-autoselector.el --- autoselector for pyim. -*- lexical-binding: t; -*-
;; * Header
;; Copyright (C) 2021 Free Software Foundation, Inc.
;; Author: Feng Shu <[email protected]>
;; Maintainer: Feng Shu <[email protected]>
;; 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)
(require 'pyim-scheme)
(require 'pyim-process)
(defgroup pyim-autoselector nil
"Autoselector for pyim."
:group 'pyim)
(defun pyim-autoselector-xingma (&rest _args)
"适用于型码输入法的自动上屏器.
比如:五笔等型码输入法,重码率很低,90%以上的情况都是选择第一个词
条,自动选择可以减少按空格强制选词的机会。"
(let* ((scheme (pyim-scheme-current))
(split-length (pyim-scheme-xingma-code-split-length scheme))
(entered (pyim-process-get-entered 'point-before))
(candidates (pyim-process-get-candidates))
(last-candidates (pyim-process-get-last-candidates)))
(when (pyim-scheme-xingma-p scheme)
(pyim-autoselector--xingma
split-length entered candidates last-candidates))))
(defun pyim-autoselector--xingma (split-length entered candidates last-candidates)
"`pyim-autoselector-xingma' 内部使用的函数。"
(cond
((and (= (length entered) split-length)
(= (length candidates) 1)
;; 如果没有候选词,pyim 默认将用户输入当做候选词,这时不能自动上屏,
;; 因为这种情况往往是用户输入有误,自动上屏之后,调整输入就变得麻烦了。
(not (equal entered (car candidates))))
'(:select current))
((and (> (length entered) split-length)
(equal (substring entered 0 split-length)
(car last-candidates)))
;; 自动清除错误输入模式,类似微软五笔:敲第五个字母的时候,前面四个字母自
;; 动清除。
'(:select last :replace-with ""))
((> (length entered) split-length)
'(:select last))
(t nil)))
(cl-pushnew #'pyim-autoselector-xingma pyim-process-autoselector)
;; * Footer
(provide 'pyim-autoselector)
;;; pyim-autoselector.el ends here