Subject: Re: bug#56323: 29.0.50; [v2] Add new customisable phonetic Tamil input method

From: Visuwesh <[email-masked]>
Date: Fri, 01 Jul 2022 18:31:50 +0530

[வெள்ளி ஜூலை 01, 2022] Visuwesh wrote:

> [வியாழன் ஜூன் 30, 2022] Visuwesh wrote:
>
>> The second patch actually adds the new phonetic input method.  I will
>> leave the rationale for making it a _customisable_ input method in
>> footnote [1].  To reuse the existing code that calculates the various
>> tables for the tamil-itrans IM, I turned the code in defvars to defuns.
>> However, the definition of the almighty
>> quail-tamil-itrans-syllable-table is still huge since I needed to do a
>> whole lot to convert the indian-tml-base-table to a format that will
>> accepted by the new defun `quail-tamil-itrans-compute-syllable-table'.
>> [blah blah blah...]
>
> Here's a second revision of the second patch.
>
Here's a corrected patch with a really silly oversight fixed:

>From 8774f2f3dc1c5850e9ec6f6b0c178fd455221f17 Mon Sep 17 00:00:00 2001
From: Visuwesh <[email-masked]>
Date: Thu, 30 Jun 2022 17:01:07 +0530
Subject: [PATCH] Add new customizable phonetic Tamil input method

* lisp/leim/quail/indian.el
(quail-tamil-itrans-compute-syllable-table): New function extracted
from..
(quail-tamil-itrans-syllable-table): ... here.  Use above function.
(quail-tamil-itrans-compute-signs-table): Add new argument VARIOUS.
(quail-tamil-itrans-various-signs-and-digits-table)
(quail-tamil-itrans-various-signs-table): Adjust function call, and
add TAMIL OM sign translation.

(tamil): New phonetic Tamil input method.
(tamil-vowel-translation, tamil-consonant-translation)
(tamil-misc-translation, tamil-native-digits): New defcustoms to
change the translation rules of the input method.
(tamil-uyir-translation, tamil-mei-translation): Aliases to new
defcustom for better discoverability.
(tamil--syllable-table, tamil--signs-table, tamil--hashtables)
(tamil--vowel-signs): Internal variables used by the Tamil input
method.
(tamil--make-tables): Function to produce vowels, consonants, and
their translations.
(tamil--update-quail-rules): Function to update the translation rules
for the Tamil input method.

* lisp/language/indian.el ("Tamil"): Change the default input method
of the Tamil language environment to the phonetic input method.
---
 lisp/language/indian.el   |   2 +-
 lisp/leim/quail/indian.el | 302 +++++++++++++++++++++++++++++---------
 2 files changed, 236 insertions(+), 68 deletions(-)

diff --git a/lisp/language/indian.el b/lisp/language/indian.el
index 2887d410ad..91ad818533 100644
--- a/lisp/language/indian.el
+++ b/lisp/language/indian.el
@@ -109,7 +109,7 @@ 'devanagari
  "Tamil" '((charset unicode)
 	   (coding-system utf-8)
 	   (coding-priority utf-8)
-	   (input-method . "tamil-itrans")
+	   (input-method . "tamil")
            (sample-text . "Tamil (தமிழ்)	வணக்கம்")
 	   (documentation . "\
 South Indian Language Tamil is supported in this language environment."))
diff --git a/lisp/leim/quail/indian.el b/lisp/leim/quail/indian.el
index 8fffcc3511..ebc04c518b 100644
--- a/lisp/leim/quail/indian.el
+++ b/lisp/leim/quail/indian.el
@@ -127,47 +127,19 @@ "\\''"
  indian-mlm-itrans-v5-hash "malayalam-itrans" "Malayalam" "MlmIT"
  "Malayalam transliteration by ITRANS method.")
 
-(defvar quail-tamil-itrans-syllable-table
-  (let ((vowels
-	 '(("அ" nil "a")
-	   ("ஆ" "ா" "A")
-	   ("இ" "ி" "i")
-	   ("ஈ" "ீ" "I")
-	   ("உ" "ு" "u")
-	   ("ஊ" "ூ" "U")
-	   ("எ" "ெ" "e")
-	   ("ஏ" "ே" "E")
-	   ("ஐ" "ை" "ai")
-	   ("ஒ" "ொ" "o")
-	   ("ஓ" "ோ" "O")
-	   ("ஔ" "ௌ" "au")))
-	(consonants
-	 '(("க" "k")			; U+0B95
-	   ("ங" "N^")			; U+0B99
-	   ("ச" "ch")			; U+0B9A
-	   ("ஞ" "JN")			; U+0B9E
-	   ("ட" "T")			; U+0B9F
-	   ("ண" "N")			; U+0BA3
-	   ("த" "t")			; U+0BA4
-	   ("ந" "n")			; U+0BA8
-	   ("ப" "p")			; U+0BAA
-	   ("ம" "m")			; U+0BAE
-	   ("ய" "y")			; U+0BAF
-	   ("ர" "r")			; U+0BB0
-	   ("ல" "l")			; U+0BB2
-	   ("வ" "v")			; U+0BB5
-	   ("ழ" "z")			; U+0BB4
-	   ("ள" "L")			; U+0BB3
-	   ("ற" "rh")			; U+0BB1
-	   ("ன" "nh")			; U+0BA9
-	   ("ஜ" "j")			; U+0B9C
-	   ("ஶ" nil)			; U+0BB6
-	   ("ஷ" "Sh")			; U+0BB7
-	   ("ஸ" "s")			; U+0BB8
-	   ("ஹ" "h")			; U+0BB9
-	   ("க்ஷ" "x" )			; U+0B95
-	   ))
-	(virama #x0BCD)
+;; FIXME: This only accepts a single translation for vowels.  Ideally,
+;; we want it to support mutliple translation just like consonants.
+;; This also does not sort by vowel or consonant.  :(
+(defun quail-tamil-itrans-compute-syllable-table (vowels consonants)
+  "Return the syllable table for the input method as a string.
+VOWELS is a list of (VOWEL SIGN TRANS) where VOWEL is a string or
+character representing the Tamil vowel character, SIGN is the
+vowel sign corresponding to VOWEL or nil for none, and TRANS is
+the input sequence to insert VOWEL.
+CONSONANTS is a list of (CONSONANT TRANS...) where CONSONANT is
+the Tamil consonant character, and TRANS is one or more strings
+that describe how to insert CONSONANT."
+  (let ((virama #x0BCD)
 	clm)
     (with-temp-buffer
       (insert "\n")
@@ -197,21 +169,42 @@ quail-tamil-itrans-syllable-table
 	  (insert (propertize "\t" 'display (list 'space :align-to clm))
 		  (car c) (or (nth 1 v) ""))
 	  (setq clm (+ clm 6)))
-	(insert "\n" (or (nth 1 c) "")
-		(propertize "\t" 'display '(space :align-to 4))
-		"|")
-	(setq clm 6)
-
-	(dolist (v vowels)
-	  (apply #'insert (propertize "\t" 'display (list 'space :align-to clm))
-		 (if (nth 1 c) (list (nth 1 c) (nth 2 v)) (list "")))
-	  (setq clm (+ clm 6))))
+        (dolist (ct (cdr c))
+	  (insert "\n" (or ct "")
+		  (propertize "\t" 'display '(space :align-to 4))
+		  "|")
+	  (setq clm 6)
+          (dolist (v vowels)
+	    (apply #'insert (propertize "\t" 'display (list 'space :align-to clm))
+		   (if ct (list ct (nth 2 v)) (list "")))
+	    (setq clm (+ clm 6)))))
       (insert "\n")
       (insert "----+")
       (insert-char ?- 74)
       (insert "\n")
       (buffer-string))))
 
+(defvar quail-tamil-itrans-syllable-table
+  (quail-tamil-itrans-compute-syllable-table
+   (let ((vowels (car indian-tml-base-table))
+         trans v ret)
+     (dotimes (i (length vowels))
+       (when (setq v (nth i vowels))
+         (setq trans (nth i (car indian-itrans-v5-table-for-tamil)))
+         (push (append v (list (if (listp trans) (car trans) trans)))
+               ret)))
+     (setq ret (nreverse ret))
+     ret)
+   (let ((consonants (cadr indian-tml-base-table))
+         trans c ret)
+     (dotimes (i (length consonants))
+       (when (setq c (nth i consonants))
+         (setq trans (nth i (cadr indian-itrans-v5-table-for-tamil)))
+         (push (cons c (if (listp trans) trans (list trans)))
+               ret)))
+     (setq ret (nreverse ret))
+     ret)))
+
 (defvar quail-tamil-itrans-numerics-and-symbols-table
   (let ((numerics '((?௰ "பத்து") (?௱ "நூறு") (?௲ "ஆயிரம்")))
 	(symbols '((?௳ "நாள்") (?௴ "மாதம்") (?௵ "வருடம்")
@@ -244,25 +237,28 @@ quail-tamil-itrans-numerics-and-symbols-table
       (insert "\n")
       (buffer-string))))
 
-(defun quail-tamil-itrans-compute-signs-table (digitp)
+(defun quail-tamil-itrans-compute-signs-table (digitp various)
   "Compute the signs table for the tamil-itrans input method.
-If DIGITP is non-nil, include the digits translation as well."
-  (let ((various '((?ஃ . "H") ("ஸ்ரீ" . "srii") (?ௐ)))
-	(digits "௦௧௨௩௪௫௬௭௮௯")
+If DIGITP is non-nil, include the digits translation as well.
+If VARIOUS is non-nil, then it should a list of (CHAR TRANS)
+where CHAR is the character/string to translate and TRANS is
+CHAR's translation."
+  (let ((digits "௦௧௨௩௪௫௬௭௮௯")
 	(width 6) clm)
     (with-temp-buffer
-      (insert "\n" (make-string 18 ?-) "+")
-      (when digitp (insert (make-string 60 ?-)))
+      (insert "\n" (make-string 18 ?-))
+      (when digitp
+        (insert "+" (make-string 60 ?-)))
       (insert "\n")
       (insert
        (propertize "\t" 'display '(space :align-to 5)) "various"
-       (propertize "\t" 'display '(space :align-to 18)) "|")
+       (propertize "\t" 'display '(space :align-to 18)))
       (when digitp
         (insert
-         (propertize "\t" 'display '(space :align-to 45)) "digits"))
-      (insert "\n" (make-string 18 ?-) "+")
+          "|" (propertize "\t" 'display '(space :align-to 45)) "digits"))
+      (insert "\n" (make-string 18 ?-))
       (when digitp
-        (insert (make-string 60 ?-)))
+        (insert "+" (make-string 60 ?-)))
       (insert "\n")
       (setq clm 0)
 
@@ -270,7 +266,8 @@ quail-tamil-itrans-compute-signs-table
 	(insert (propertize "\t" 'display (list 'space :align-to clm))
 		(car (nth i various)))
 	(setq clm (+ clm width)))
-      (insert (propertize "\t" 'display '(space :align-to 18)) "|")
+      (when digitp
+        (insert (propertize "\t" 'display '(space :align-to 18)) "|"))
       (setq clm 20)
       (when digitp
         (dotimes (i 10)
@@ -281,25 +278,28 @@ quail-tamil-itrans-compute-signs-table
       (setq clm 0)
       (dotimes (i (length various))
 	(insert (propertize "\t" 'display (list 'space :align-to clm))
-		(or (cdr (nth i various)) ""))
+		(or (cadr (nth i various)) ""))
 	(setq clm (+ clm width)))
-      (insert (propertize "\t" 'display '(space :align-to 18)) "|")
+      (when digitp
+        (insert (propertize "\t" 'display '(space :align-to 18)) "|"))
       (setq clm 20)
       (when digitp
         (dotimes (i 10)
 	  (insert (propertize "\t" 'display (list 'space :align-to clm))
 		  (format "%d" i))
 	  (setq clm (+ clm width))))
-      (insert "\n" (make-string 18 ?-) "+")
+      (insert "\n" (make-string 18 ?-))
       (when digitp
-        (insert (make-string 60 ?-) "\n"))
+        (insert "+" (make-string 60 ?-) "\n"))
       (buffer-string))))
 
 (defvar quail-tamil-itrans-various-signs-and-digits-table
-  (quail-tamil-itrans-compute-signs-table t))
+  (quail-tamil-itrans-compute-signs-table
+   t '((?ஃ "H") ("ஸ்ரீ" "srii") (?ௐ "OM"))))
 
 (defvar quail-tamil-itrans-various-signs-table
-  (quail-tamil-itrans-compute-signs-table nil))
+  (quail-tamil-itrans-compute-signs-table
+   nil '((?ஃ "H") ("ஸ்ரீ" "srii") (?ௐ "OM"))))
 
 (if nil
     (quail-define-package "tamil-itrans" "Tamil" "TmlIT" t "Tamil ITRANS"))
@@ -347,6 +347,174 @@ quail-tamil-itrans-various-signs-table
 
 Full key sequences are listed below:")
 
+;;;
+;;; Tamil phonetic input method
+;;;
+
+(defvaralias 'tamil-uyir-translation 'tamil-vowel-translation)
+(defcustom tamil-vowel-translation
+  '(("அ" "a") ("ஆ" "aa") ("இ" "i") ("ஈ" "ii")
+    ("உ" "u") ("ஊ" "uu") ("எ" "e") ("ஏ" "ee")
+    ("ஐ" "ai") ("ஒ" "o") ("ஓ" "oo") ("ஔ" "au" "ow"))
+  "List of input sequences to translate to Tamil vowels.
+Each element should be (VOWEL . TRANSLATIONS) where VOWEL is the
+Tamil vowel character (உயிரெழுத்து) and TRANSLATIONS is the
+list of input sequences to translate to that vowel."
+  :group 'leim
+  :version "29.1"
+  :type '(alist :key string :value-type (repeat string))
+  :options (delq nil
+                 (mapcar (lambda (x) (and (consp x) (string (car x))))
+                         (car indian-tml-base-table))))
+
+(defvaralias 'tamil-mei-translation 'tamil-consonant-translation)
+(defcustom tamil-consonant-translation
+  '(("க்" "k" "g") ("ங்" "ng") ("ச்" "ch" "s") ("ஞ்" "nj") ("ட்" "t" "d")
+    ("ண்" "N") ("த்" "th" "dh") ("ந்" "nh") ("ப்" "p" "b") ("ம்" "m")
+    ("ய்" "y") ("ர்" "r") ("ல்" "l") ("வ்" "v") ("ழ்" "z" "zh")
+    ("ள்" "L") ("ற்" "rh") ("ன்" "n")
+    ;; Sanskrit.
+    ("ஜ்" "j") ("ஸ்" "S") ("க்ஷ்" "ksH") ("ஷ்" "sh") ("ஹ்" "h")
+    ("க்‌ஷ்" "ksh") ("ஶ்" "Z"))
+  "List of input sequences to translate to Tamil consonants.
+Each element should be (VOWEL . TRANSLATIONS) where VOWEL is the
+Tamil consonant character (மெய் எழுத்து) and TRANSLATIONS is a list
+of input sequences to translate to that consonant."
+  :group 'leim
+  :version "29.1"
+  :type '(alist :key string :value-type (repeat string))
+  :options (delq nil
+                 (mapcar (lambda (x) (if (stringp x)
+                                         (concat x "்")
+                                       ;; #x0BCD = pulli/virama.
+                                       (and x (string x #x0BCD))))
+                         (cadr indian-tml-base-table))))
+
+(defcustom tamil-misc-translation
+  ;; ஃ is not a vowel or a consonant.
+  '(("ஃ" "F" "q")
+    ("ௐ" "OM"))
+  "List of input sequences to translate to various Tamil characters.
+Each should element should be (CHARACTER . TRANSLATIONS) where
+CHARACTER may be any string and TRANSLATIONS is a list of input
+sequences to translate to that CHARACTER."
+  :group 'leim
+  :version "29.1"
+  :type '(alist :key string :value-type (repeat string)))
+
+(defcustom tamil-native-digits nil
+  "When non-nil, use Tamil native digits instead of Arabic ones."
+  :group 'leim
+  :version "29.1"
+  :type 'boolean)
+
+(defvar tamil--syllable-table nil)
+(defvar tamil--signs-table nil)
+(defvar tamil--hashtables
+  (cons (make-hash-table :test #'equal)
+        (make-hash-table :test #'equal)))
+(defvar tamil--vowel-signs
+  '(("அ" . nil) ("ஆ" . ?ா) ("இ" . ?ி) ("ஈ" . ?ீ)
+    ("உ" . ?ு) ("ஊ" . ?ூ) ("எ" . ?ெ) ("ஏ" . ?ே)
+    ("ஐ" . ?ை) ("ஒ" . ?ொ) ("ஓ" . ?ோ) ("ஔ" . ?ௌ)))
+
+(defun tamil--make-tables ()
+  "Return vowels, consonants, and their translation rules.
+The returned table is a list (VOWELS CONSONANTS VTRANS CTRANS)
+where VOWELS is a list of (VOWEL VOWEL-SIGN) where VOWEL is the
+Tamil vowel character, and VOWEL-SIGN is its sign, CONSONANTS is
+a list of consonants that have translation rules.
+VTRANS is a list of translation rules for vowels in VOWELS in the
+order they appear, CTRANS is a list of translation rules for
+consonants in CONSONANTS."
+  (let (c-table v-table v-trans c-trans)
+    (dolist (v tamil-vowel-translation)
+      (push (list (car v) (assoc-default (car v) tamil--vowel-signs)) v-table)
+      (push (cdr v) v-trans))
+    (dolist (c tamil-consonant-translation)
+      (push (cdr c) c-trans)
+      ;; Remove pulli/virama from consonant entry.
+      (push (substring (car c) 0 -1) c-table))
+    ;; FIXME: Remove when sorting is done properly!
+    (setq c-table (nreverse c-table)
+          v-table (nreverse v-table)
+          c-trans (nreverse c-trans)
+          v-trans (nreverse v-trans))
+    (list v-table c-table
+          v-trans c-trans)))
+
+(defun tamil--update-quail-rules ()
+  ;; This function does pretty much what `indian-make-hash' does
+  ;; except that we don't try to copy the structure of
+  ;; `indian-tml-base-table' which leads to less code hassle.
+  (let* ((quail-current-package (assoc-default "tamil" quail-package-alist))
+         (tables (tamil--make-tables))
+         (vowels (nth 0 tables))
+         (vowels-trans (nth 2 tables))
+         (consonants (nth 1 tables))
+         (consonants-trans (nth 3 tables))
+         (pulli (string #x0BCD)))
+    (clrhash (car tamil--hashtables))
+    (clrhash (cdr tamil--hashtables))
+    (indian--puthash-v vowels vowels-trans tamil--hashtables)
+    (indian--puthash-c consonants consonants-trans pulli tamil--hashtables)
+    (indian--puthash-cv consonants consonants-trans
+                        vowels vowels-trans tamil--hashtables)
+    (indian--puthash-m (mapcar #'car tamil-misc-translation)
+                       (mapcar #'cdr tamil-misc-translation)
+                       tamil--hashtables)
+    (when tamil-native-digits
+      (indian--puthash-m (nth 3 indian-tml-base-digits-table)
+                         '("0" "1" "2" "3" "4" "5" "6" "7" "8" "9")
+                         tamil--hashtables))
+    ;; Now override the current translation rules.
+    ;; Empty quail map is '(list nil)'.
+    (setf (nth 2 quail-current-package) '(()))
+    (maphash (lambda (k v)
+               (quail-defrule k (if (length= v 1)
+                                    (string-to-char v)
+                                  (vector v))))
+             (cdr tamil--hashtables))
+    (setq  tamil--syllable-table
+           (quail-tamil-itrans-compute-syllable-table
+            (mapcar (lambda (v) (append v (pop vowels-trans))) vowels)
+            (mapcar (lambda (c) (cons c (pop consonants-trans))) consonants))
+           tamil--signs-table
+           ;; FIXME: This should also show how to input ஸ்ரீ (default: Srii).
+           (quail-tamil-itrans-compute-signs-table
+            tamil-native-digits tamil-misc-translation))))
+
+(quail-define-package
+ "tamil" "Tamil" "ழ" t
+ "Customisable Tamil phonetic input method.
+To change the translation of vowels (உயிரெத்துக்கள்), customize `tamil-vowel-translation'.
+To change the translation of consonants (மெய் எழுத்துக்கள்), customize
+ `tamil-consonant-translation'.
+To input miscellaneous characters (including ஃ), customize
+ `tamil-misc-translation'.
+To use native Tamil digits, customize `tamil-native-digits'.
+
+To end the current translation process, say \\<quail-translation-keymap>\\[quail-select-current] (defined in
+`quail-translation-keymap').  This is useful when there's a
+ conflict between two possible translation.
+
+The current input scheme is:
+
+### Basic syllables (உயிர்மெய் எழுத்துக்கள்) ###
+\\=\\<tamil--syllable-table>
+
+### Miscellaneous ####
+\\=\\<tamil--signs-table>
+
+The following characters have NO input sequence associated with
+them by default.  Their descriptions are included for easy
+reference.
+\\=\\<quail-tamil-itrans-numerics-and-symbols-table>
+
+Full key sequences are listed below:"
+ nil nil nil nil nil nil t)
+(tamil--update-quail-rules)
+
 ;;;
 ;;; Input by Inscript
 ;;;
-- 
2.35.1

Sorry for the noise.