From b252898df8894c51008cbed2cfcf2eb9890d318f Mon Sep 17 00:00:00 2001 From: handa Date: Wed, 21 Oct 2009 07:02:12 +0000 Subject: [PATCH] *** empty log message *** --- MInputMethod.cs | 78 ++++++++++++++++++++++++++++++++++++++++++------------- input.cs | 62 +++++++++++++++++++++++++++++++------------ 2 files changed, 106 insertions(+), 34 deletions(-) diff --git a/MInputMethod.cs b/MInputMethod.cs index d08f7c2..2b7ad2c 100644 --- a/MInputMethod.cs +++ b/MInputMethod.cs @@ -138,10 +138,11 @@ namespace M17N.Input = new Dictionary (); private static uint keysym_base = 0x200000; private static uint char_mask = ~((uint) KeyModifier.All); - public static Key Reload; + public static readonly Key Reload; static Key () { + keysyms["null"] = 0x00; keysyms["bs"] = keysyms["backspace"] = 0x08; keysyms["tab"] = 0x09; keysyms["lf"] = keysyms["linefeed"] = 0x10; @@ -161,8 +162,7 @@ namespace M17N.Input keymodifiers["altgr"] = KeyModifier.AltGr; keymodifiers["super"] = KeyModifier.Super; keymodifiers["hyper"] = KeyModifier.Hyper; - Reload = new Key (keysym_base); - keysyms["-reload"] = keysym_base++; + Reload = new Key ((MSymbol) "-reload"); } private static uint decode_keysym (MSymbol keysym) @@ -312,7 +312,7 @@ namespace M17N.Input public int ToChar () { - return (int) (key & 0x1FFFFF); + return (key & 0x3FFFFF) <= 0x1FFFFF ? (int) (key & 0x1FFFFF) : -1; } public override string ToString () @@ -321,7 +321,7 @@ namespace M17N.Input MText mt = null; if (c < 0x20) foreach (KeyValuePair kv in keysyms) - if ((uint) c == kv.Value) + if ((key & 0x3FFFFF) == kv.Value) { mt = kv.Key; break; @@ -2630,9 +2630,12 @@ namespace M17N.Input internal void commit () { - Candidates.Detach (this); - produced.Cat (preedit); - preedit_replace (0, preedit.Length, null, null); + if (preedit.Length > 0) + { + Candidates.Detach (this); + produced.Cat (preedit); + preedit_replace (0, preedit.Length, null, null); + } } internal void shift (State state) @@ -2727,8 +2730,7 @@ namespace M17N.Input private bool handle_key () { - Console.WriteLine ("{0}:key='{1}'", state.name, - new MText (keys.keyseq[key_head].ToChar ())); + Console.WriteLine ("{0}:key='{1}'", state.name, keys.keyseq[key_head]); Keymap sub = keymap.Lookup (keys, ref key_head); if (sub != keymap) @@ -2852,6 +2854,34 @@ namespace M17N.Input } return (! key_unhandled && produced.Length == 0); } + + public bool Filter () + { + changed = ChangedStatus.None; + produced.Del (); + preceding_text.Del (); + following_text.Del (); + + commit (); + if ((changed & ChangedStatus.Preedit) != ChangedStatus.None + && PreeditChanged != null) + { + callback_arg.Set (MSymbol.mtext, preedit); + PreeditChanged (this, callback_arg); + } + if ((changed & ChangedStatus.StateTitle) != ChangedStatus.None + && StatusChanged != null) + { + callback_arg.Set (MSymbol.mtext, status); + StatusChanged (this, callback_arg); + } + if ((changed & ChangedStatus.Candidate) != ChangedStatus.None + && CandidateChanged != null) + { + CandidateChanged (this, callback_arg); + } + return (produced.Length == 0); + } } public class Session @@ -2892,25 +2922,37 @@ namespace M17N.Input return true; } - public bool HandleKey (Key key) + public bool HandleKey (ref Key key) { - bool result = ic.Filter (key); - - if (! result) + if (! ic.Filter (key)) { MText produced = ic.Produced; mt.Ins (pos, produced); pos += produced.Length; - if (ic.UnhandledKey (out key)) + Key unhandled; + if (ic.UnhandledKey (out unhandled)) { - mt.Ins (pos, key.ToChar ()); - pos++; + key = unhandled; + return false; } } + return true; + } + + public bool Close () + { + bool result = ic.Filter (); + if (! result) + { + mt.Ins (pos, ic.Produced); + pos += ic.Produced.Length; + } + ic = null; + mt = null; return result; } - public int CurrentPos { get { return pos; } } + public int CurrentPos { get { return pos; } set { pos = value; } } public MText Preedit { get { return ic.Preedit; } } } } diff --git a/input.cs b/input.cs index ca3f7e4..aa5e9d8 100644 --- a/input.cs +++ b/input.cs @@ -9,6 +9,21 @@ using Xex = System.Xml.Xexpression; public class Test { + private static void print (char key, MText head, MText preedit, MText tail) + { + 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; @@ -38,25 +53,40 @@ public class Test MInputMethod.Session session = new MInputMethod.Session (im, mt, 0); MText str = args[argc + 2]; + MInputMethod.Key left = new MInputMethod.Key ("left"); + MInputMethod.Key right = new MInputMethod.Key ("right"); + for (int i = 0; i < str.Length; i++) { - MInputMethod.Key key = new MInputMethod.Key (str[i]); - session.HandleKey (key); + MInputMethod.Key key + = (str[i] == '<' ? left + : str[i] == '>' ? right + : new MInputMethod.Key (str[i])); + int pos = session.CurrentPos; + if (! session.HandleKey (ref key)) + { + 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]); + } + if (! session.Close ()) + { int pos = session.CurrentPos; - MText head = mt[0, pos]; - MText preedit = session.Preedit; - MText tail = mt[pos, mt.Length]; - Console.Write ("{0} -> \"{1}|{2}|{3}\" (", - str[i, i + 1], head, preedit, tail); - for (int j = 0; j < head.Length; j++) - Console.Write ("{0}U+{1:X4}", j == 0 ? "(" : " ", head[j]); - Console.Write ("|"); - 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 (")"); + print (' ', mt[0, pos], null, mt[pos, mt.Length]); } } } -- 1.7.10.4