emacs/lisp/leim/quail/sisheng.el
2024-01-02 09:47:10 +08:00

290 lines
7.6 KiB
EmacsLisp

;;; sisheng.el --- sisheng input method for Chinese pinyin transliteration -*- lexical-binding: t; -*-
;; Copyright (C) 2004-2024 Free Software Foundation, Inc.
;; Author: Werner LEMBERG <wl@gnu.org>
;; Keywords: multilingual, input method, Chinese, pinyin, sisheng
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'quail)
(defconst sisheng-regexp
"[āēīōūǖ]\\|üē")
;; First element is the key,
;; second element is the vowel used for the input sequence,
;; last four elements are the resulting tones.
;;
(defconst sisheng-vowel-table
'(("ā" "a" "ā" "á" "ǎ" "à")
("ē" "e" "ē" "é" "ě" "è")
("ī" "i" "ī" "í" "ǐ" "ì")
("ō" "o" "ō" "ó" "ǒ" "ò")
("ū" "u" "ū" "ú" "ǔ" "ù")
("ǖ" "v" "ǖ" "ǘ" "ǚ" "ǜ")
("üē" "ve" "üē" "üé" "üě" "üè")))
;; All possible syllables in Mandarin Chinese, presented in the first
;; tone. Note that make-sisheng-rules always constructs rules for all
;; four tones even if some of those tones aren't used in Mandarin.
;;
(defconst sisheng-syllable-table
'("ā" "āi" "ān" "āng" "āo"
"" "bāi" "bān" "bāng" "bāo"
"bēi" "bēn" "bēng"
"" "biān" "biāo" "biē" "bīn" "bīng"
""
""
"" "cāi" "cān" "cāng" "cāo"
"" "cēn" "cēng"
""
"cōng" "cōu"
"" "cuān" "cuī" "cūn" "cuō"
"chā" "chāi" "chān" "chāng" "chāo"
"chē" "chēn" "chēng"
"chī"
"chōng" "chōu"
"chū" "chuā" "chuāi" "chuān" "chuāng" "chuī" "chūn" "chuō"
"" "dāi" "dān" "dāng" "dāo"
"" "dēi" "dēn" "dēng"
"" "diān" "diāo" "diē" "dīng" "diū"
"dōng" "dōu"
"" "duān" "duī" "dūn" "duō"
"ē" "ēi" "ēn" "ēng" "ēr"
"" "fān" "fāng"
"fēi" "fēn" "fēng"
"fiāo"
"" "fōu"
""
"" "gāi" "gān" "gāng" "gāo"
"" "gēi" "gēn" "gēng"
"gōng" "gōu"
"" "guā" "guāi" "guān" "guāng" "guī" "gūn" "guō"
"" "hāi" "hān" "hāng" "hāo"
"" "hēi" "hēn" "hēng"
"hōng" "hōu"
"" "huā" "huāi" "huān" "huāng" "huī" "hūn" "huō"
"" "jiā" "jiān" "jiāng" "jiāo" "jiē" "jīn" "jīng" "jiōng" "jiū"
"" "juān" "juē" "jūn"
"" "kāi" "kān" "kāng" "kāo"
"" "kēi" "kēn" "kēng"
"kōng" "kōu"
"" "kuā" "kuāi" "kuān" "kuāng" "kuī" "kūn" "kuō"
"" "lāi" "lān" "lāng" "lāo"
"" "lēi" "lēng"
"" "liā" "liān" "liāng" "liāo" "liē" "līn" "līng" "liū"
"lōng" "lōu"
"" "luān" "lūn" "luō"
"" "lüē"
"" "māi" "mān" "māng" "māo"
"" "mēi" "mēn" "mēng"
"" "miān" "miāo" "miē" "mīn" "mīng" "miū"
"" "mōu"
""
"" "nāi" "nān" "nāng" "nāo"
"" "nēi" "nēn" "nēng"
"" "niān" "niāng" "niāo" "niē" "nīn" "nīng" "niū"
"nōng" "nōu"
"" "nuān" "nuō"
"" "nüē"
"ō" "ōu"
"" "pāi" "pān" "pāng" "pāo"
"pēi" "pēn" "pēng"
"" "piān" "piāo" "piē" "pīn" "pīng"
"" "pōu"
""
"" "qiā" "qiān" "qiāng" "qiāo" "qiē" "qīn" "qīng" "qiōng" "qiū"
"" "quān" "quē" "qūn"
"rān" "rāng" "rāo"
"" "rēn" "rēng"
""
"rōng" "rōu"
"" "ruā" "ruān" "ruī" "rūn" "ruō"
"" "sāi" "sān" "sāng" "sāo"
"" "sēn" "sēng"
""
"sōng" "sōu"
"" "suān" "suī" "sūn" "suō"
"shā" "shāi" "shān" "shāng" "shāo"
"shē" "shēi" "shēn" "shēng"
"shī"
"shōu"
"shū" "shuā" "shuāi" "shuān" "shuāng" "shuī" "shūn" "shuō"
"" "tāi" "tān" "tāng" "tāo"
"" "tēi" "tēng"
"" "tiān" "tiāo" "tiē" "tīng"
"tōng" "tōu"
"" "tuān" "tuī" "tūn" "tuō"
"" "wāi" "wān" "wāng"
"wēi" "wēn" "wēng"
""
""
"" "xiā" "xiān" "xiāng" "xiāo" "xiē" "xīn" "xīng" "xiōng" "xiū"
"" "xuān" "xuē" "xūn"
"" "yān" "yāng" "yāo"
""
"" "yīn" "yīng"
"" "yōng" "yōu"
"" "yuān" "yuē" "yūn"
"" "zāi" "zān" "zāng" "zāo"
"" "zēi" "zēn" "zēng"
""
"zōng" "zōu"
"" "zuān" "zuī" "zūn" "zuō"
"zhā" "zhāi" "zhān" "zhāng" "zhāo"
"zhē" "zhēi" "zhēn" "zhēng"
"zhī"
"zhōng" "zhōu"
"zhū" "zhuā" "zhuāi" "zhuān" "zhuāng" "zhuī" "zhūn" "zhuō"))
;; This function converts e.g.
;;
;; "zhuō"
;;
;; into
;;
;; (("zhuo4" ["zhuò"])
;; ("zhuo3" ["zhuǒ"])
;; ("zhuo2" ["zhuó"])
;; ("zhuo1" ["zhuō"]))
;;
(defun quail-make-sisheng-rules (syllable)
(let ((case-fold-search t)
vowel-match
vowel-list
input-vowel
base-key
key
value
key-value-list
(i 1))
(string-match sisheng-regexp syllable)
(setq vowel-match (downcase (match-string 0 syllable)))
(setq vowel-list
(cdr (assoc-string vowel-match sisheng-vowel-table)))
(setq input-vowel (car vowel-list))
(setq base-key (replace-match input-vowel nil nil syllable))
(while (<= i 4)
(setq key (concat base-key (number-to-string i)))
(setq value (vector (replace-match (nth i vowel-list) nil nil syllable)))
(push (list key value) key-value-list)
(setq i (1+ i)))
key-value-list))
;; Set up sisheng input method.
;;
(quail-define-package
"chinese-sisheng" ; name
"Chinese" ; language
"ǚ" ; title
t ; guidance
"Sìshēng input method for pīnyīn transliteration of Chinese.
Examples: shuang1 -> shuāng
Lv3 -> Lǚ
AN4 -> ÀN
Use the fifth (unstressed) tone for syllables containing `ü'
without a tone mark.
Example: nve5 -> nüe
" ; docstring
nil ; translation-keys
t ; forget-last-selection
nil ; deterministic
nil ; kbd-translate
nil ; show-layout
nil ; create-decode-map
nil ; maximum-shortest
nil ; overlay-plist
nil ; update-translation-function
nil ; conversion-keys
t ; simple
)
;; Call quail-make-sisheng-rules for all syllables in sisheng-syllable-table.
;;
(let (sisheng-list)
(dolist (syllable sisheng-syllable-table)
(setq sisheng-list
(append (quail-make-sisheng-rules syllable)
sisheng-list)))
(dolist (syllable sisheng-syllable-table)
(setq sisheng-list
(append (quail-make-sisheng-rules (upcase-initials syllable))
sisheng-list)))
(dolist (syllable sisheng-syllable-table)
(setq sisheng-list
(append (quail-make-sisheng-rules (upcase syllable))
sisheng-list)))
(eval `(quail-define-rules
,@sisheng-list
("lv5" [""])
("lve5" ["lüe"])
("nv5" [""])
("nve5" ["nüe"])
("Lv5" [""])
("Lve5" ["Lüe"])
("Nv5" [""])
("Nve5" ["Nüe"])
("LV5" [""])
("LVE5" ["LÜE"])
("NV5" [""])
("NVE5" ["NÜE"]))))
;; Local Variables:
;; coding: utf-8
;; End:
;;; sisheng.el ends here