zero-input-table.el 3.57 KB
Newer Older
1
;;; zero-input-table.el --- a demo table based input method based on zero.el -*- no-byte-compile: t; lexical-binding: t -*-
Yuanle Song's avatar
Yuanle Song committed
2
3
4
5

;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
6
;;
Yuanle Song's avatar
Yuanle Song committed
7
8
9
10
11
12
13
14
15
16
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

;;; Commentary:

17
;; when you type the key in `zero-input-table-table', IM will insert the
18
19
20
;; corresponding value.
;;
;; To use this demo IM,
Yuanle Song's avatar
Yuanle Song committed
21
;;   (add-to-list 'load-path "~/fromsource/zero")
22
23
24
;;   (require 'zero-input-table)
;;   (zero-input-set-default-im 'zero-input-table) ; set as default IM
;;   or (zero-input-set-im 'zero-input-table)      ; set as current buffer's IM
Yuanle Song's avatar
Yuanle Song committed
25

Yuanle Song's avatar
Yuanle Song committed
26
27
;;; Code:

28
29
30
31
;;==============
;; dependencies
;;==============

32
(require 'zero-input-framework)
33

Yuanle Song's avatar
Yuanle Song committed
34
35
36
37
;;===============================
;; basic data and emacs facility
;;===============================

38
39
(defvar zero-input-table-table nil
  "The table used by zero-input-table input method, map string to string.")
40
41
(defvar zero-input-table-sequence-initials nil
  "Used in `zero-input-table-can-start-sequence'.")
Yuanle Song's avatar
Yuanle Song committed
42
43
44
45
46

;;=====================
;; key logic functions
;;=====================

47
(defun zero-input-table-sort-key (lhs rhs)
48
  "A predicate function to sort candidates.  Return t if LHS should sort before RHS."
Yuanle Song's avatar
Yuanle Song committed
49
50
  (string< (car lhs) (car rhs)))

51
52
(defun zero-input-table-build-candidates (preedit-str &optional _fetch-size)
  "Build candidates by looking up PREEDIT-STR in `zero-input-table-table'."
53
  (mapcar 'cdr (sort (cl-remove-if-not #'(lambda (pair) (string-prefix-p preedit-str (car pair))) zero-input-table-table) 'zero-input-table-sort-key)))
Yuanle Song's avatar
Yuanle Song committed
54

55
56
57
58
;; (defun zero-input-table-build-candidates-async (preedit-str)
;;   "build candidate list, when done show it via `zero-input-table-show-candidates'"
;;   (zero-input-table-debug "building candidate list\n")
;;   (let ((candidates (zero-input-table-build-candidates preedit-str)))
Yuanle Song's avatar
Yuanle Song committed
59
;;     ;; update cache to make SPC and digit key selection possible.
60
61
;;     (setq zero-input-table-candidates candidates)
;;     (zero-input-table-show-candidates candidates)))
Yuanle Song's avatar
Yuanle Song committed
62

63
(defun zero-input-table-can-start-sequence (ch)
64
  "Return t if char CH can start a preedit sequence."
65
  (member (make-string 1 ch) zero-input-table-sequence-initials))
Yuanle Song's avatar
Yuanle Song committed
66

Yuanle Song's avatar
Yuanle Song committed
67
68
69
;;===============================
;; register IM to zero framework
;;===============================
Yuanle Song's avatar
Yuanle Song committed
70

71
(zero-input-register-im
72
 "zero-input-table"
73
74
 '((:build-candidates . zero-input-table-build-candidates)
   (:can-start-sequence . zero-input-table-can-start-sequence)))
Yuanle Song's avatar
Yuanle Song committed
75
76
77
78
79

;;============
;; public API
;;============

80
(defun zero-input-table-set-table (alist)
81
  "Set the conversion table.
Yuanle Song's avatar
Yuanle Song committed
82

83
84
the ALIST should be a list of (key . value) pairs.  when user type
\(part of) key, the IM will show all matching value.
Yuanle Song's avatar
Yuanle Song committed
85

86
87
88
89
90
91
92
To use demo data, you can call:
\(zero-input-table-set-table
 \\='((\"phone\" . \"18612345678\")
   (\"mail\" . \"foo@example.com\")
   (\"map\" . \"https://ditu.amap.com/\")
   (\"m\" . \"https://msdn.microsoft.com/en-us\")
   (\"address\" . \"123 Happy Street\")))"
93
94
  (setq zero-input-table-table alist)
  (setq zero-input-table-sequence-initials
95
	(delete-dups (mapcar #'(lambda (pair) (substring (car pair) 0 1))
96
			     zero-input-table-table))))
Yuanle Song's avatar
Yuanle Song committed
97

98
(provide 'zero-input-table)
Yuanle Song's avatar
Yuanle Song committed
99

100
;;; zero-input-table.el ends here