*** empty log message ***
[m17n/m17n-lib-cs.git] / input.cs
index 4871598..18e513d 100644 (file)
--- a/input.cs
+++ b/input.cs
@@ -5,45 +5,89 @@ using System.Xml;
 using M17N;
 using M17N.Core;
 using M17N.Input;
+using Xex = System.Xml.Xexpression;
 
 public class Test
 {
-  public static void Main()
+  private static void print (char key, MText head, MText preedit, MText tail)
   {
-    //M17n.debug = true;
-    MDatabase.ApplicationDir = "/usr/local/share/m17n-xml";
-    MText desc, title;
-    MInputMethod.Variable[] vars;
-    MInputMethod.Command[] cmds;
+    Console.Write ("{0} -> \"{1}|{2}|{3}\" (", key, head, preedit, tail);
+    for (int j = 0; j < head.Length; j++)
+      Console.Write ("{0}U+{1:X4}", j == 0 ? "(" : " ", head[j]);
+    Console.Write ("|");
+    if (preedit != null)
+      for (int j = 0; j < preedit.Length; j++)
+       Console.Write ("{0}U+{1:X4}", j == 0 ? "" : " ", preedit[j]);
+    Console.Write ("|");
+    for (int j = 0; j < tail.Length; j++)
+      Console.Write ("{0}U+{1:X4}", j == 0 ? "" : " ", tail[j]);
+    Console.WriteLine (")");
+  }
+
+  public static void Main(string[] args)
+  {
+    // M17n.debug = true;
+    int argc = 0;
+    string appdir = "/usr/local/share/m17n";
+    int trace_depth = 0;
 
-#if false
-    MDatabase.Tag tag = new MDatabase.Tag ("input-method",
-                                          "t", "nil", "vi-base");
-    MDatabase mdb = MDatabase.Find (tag);
-    XmlNode node = mdb.Load ("map-alnum", "input-method", "map-list", "map");
+    while (argc < args.Length && args[argc][0] == '-')
+      {
+       if (args[argc] == "-trace")
+         trace_depth = 10;
+       else if (args[argc] == "-xml")
+         appdir = "/usr/local/share/m17n-xml";
+       argc++;
+      }
+    if (argc + 2 >= args.Length)
+      {
+       Console.WriteLine ("Usage: mono [--debug] input.exe"
+                          + " [-trace] [-xml] LANG NAME KEY-SEQUECE");
+       return;
+      }
 
-    Console.WriteLine (node.OuterXml);
+    Xex.TraceDepth = trace_depth;
+    MDatabase.ApplicationDir = appdir;
+    MInputMethod im = MInputMethod.Find (args[argc], args[argc + 1]);
+    MText mt = new MText ();
+    MInputMethod.Session session = new MInputMethod.Session (im, mt, 0);
 
-#elif true
-    MInputMethod im = MInputMethod.Find ("vi", "telex");
+    MText str = args[argc + 2];
+    MInputMethod.Key left = new MInputMethod.Key ("left");
+    MInputMethod.Key right = new MInputMethod.Key ("right");
 
-    if (im.Info (out desc, out title, out vars, out cmds))
+    for (int i = 0; i < str.Length; i++)
       {
-       Console.WriteLine ("{0}, {1}, {2}, {3}", desc, title, vars, cmds);
-       im.Open ();
+       MInputMethod.Key key
+         = (str[i] == '<' ? left
+            : str[i] == '>' ? right
+            : new MInputMethod.Key (str[i]));
+       bool result = session.HandleKey (ref key);
+       int pos = session.CurrentPos;
+       if (! result)
+         {
+           if (key == left)
+             {
+               if (pos > 0)
+                 session.CurrentPos = --pos;
+             }
+           else if (key == right)
+             {
+               if (pos < mt.Length)
+                 session.CurrentPos = ++pos;
+             }
+           else
+             {
+               mt.Ins (pos, str[i]);
+               session.CurrentPos = ++pos;
+             }
+         }
+       print ((char) str[i], mt[0, pos], session.Preedit, mt[pos, mt.Length]);
       }
-#else
-    MInputMethod[] ims = MInputMethod.List ();
-    foreach (MInputMethod im in ims)
+    if (! session.Close ())
       {
-
-       Console.Write ("(<{0}, {1}, {2}>", im.Language, im.Name, im.SubName);
-       if (im.Info (out desc, out title, out vars, out cmds))
-         Console.Write (" {0}, {1}, {2}, {3}", desc, title, vars, cmds);
-       if (! im.Open ())
-         Console.Write (" open fail");
-       Console.WriteLine (")");
+       int pos = session.CurrentPos;
+       print (' ', mt[0, pos], null, mt[pos, mt.Length]);
       }
-#endif
   }
 }