*** empty log message ***
[m17n/m17n-lib-cs.git] / input.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Xml;
5 using M17N;
6 using M17N.Core;
7 using M17N.Input;
8 using Xex = System.Xml.Xexpression;
9
10 public class Test
11 {
12   private static void print (char key, MText head, MText preedit, MText tail)
13   {
14     Console.Write ("{0} -> \"{1}|{2}|{3}\" (", key, head, preedit, tail);
15     for (int j = 0; j < head.Length; j++)
16       Console.Write ("{0}U+{1:X4}", j == 0 ? "(" : " ", head[j]);
17     Console.Write ("|");
18     if (preedit != null)
19       for (int j = 0; j < preedit.Length; j++)
20         Console.Write ("{0}U+{1:X4}", j == 0 ? "" : " ", preedit[j]);
21     Console.Write ("|");
22     for (int j = 0; j < tail.Length; j++)
23       Console.Write ("{0}U+{1:X4}", j == 0 ? "" : " ", tail[j]);
24     Console.WriteLine (")");
25   }
26
27   public static void Main(string[] args)
28   {
29     // M17n.debug = true;
30     int argc = 0;
31     string appdir = "/usr/local/share/m17n";
32     int trace_depth = 0;
33
34     while (argc < args.Length && args[argc][0] == '-')
35       {
36         if (args[argc] == "-trace")
37           trace_depth = 10;
38         else if (args[argc] == "-xml")
39           appdir = "/usr/local/share/m17n-xml";
40         argc++;
41       }
42     if (argc + 2 >= args.Length)
43       {
44         Console.WriteLine ("Usage: mono [--debug] input.exe"
45                            + " [-trace] [-xml] LANG NAME KEY-SEQUECE");
46         return;
47       }
48
49     Xex.TraceDepth = trace_depth;
50     MDatabase.ApplicationDir = appdir;
51     MInputMethod im = MInputMethod.Find (args[argc], args[argc + 1]);
52     MText mt = new MText ();
53     MInputMethod.Session session = new MInputMethod.Session (im, mt, 0);
54
55     MText str = args[argc + 2];
56     MInputMethod.Key left = new MInputMethod.Key ("left");
57     MInputMethod.Key right = new MInputMethod.Key ("right");
58
59     for (int i = 0; i < str.Length; i++)
60       {
61         MInputMethod.Key key
62           = (str[i] == '<' ? left
63              : str[i] == '>' ? right
64              : new MInputMethod.Key (str[i]));
65         bool result = session.HandleKey (ref key);
66         int pos = session.CurrentPos;
67         if (! result)
68           {
69             if (key == left)
70               {
71                 if (pos > 0)
72                   session.CurrentPos = --pos;
73               }
74             else if (key == right)
75               {
76                 if (pos < mt.Length)
77                   session.CurrentPos = ++pos;
78               }
79             else
80               {
81                 mt.Ins (pos, str[i]);
82                 session.CurrentPos = ++pos;
83               }
84           }
85         print ((char) str[i], mt[0, pos], session.Preedit, mt[pos, mt.Length]);
86       }
87     if (! session.Close ())
88       {
89         int pos = session.CurrentPos;
90         print (' ', mt[0, pos], null, mt[pos, mt.Length]);
91       }
92   }
93 }