= new Dictionary<string, KeyModifier> ();
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;
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)
public int ToChar ()
{
- return (int) (key & 0x1FFFFF);
+ return (key & 0x3FFFFF) <= 0x1FFFFF ? (int) (key & 0x1FFFFF) : -1;
}
public override string ToString ()
MText mt = null;
if (c < 0x20)
foreach (KeyValuePair<string, uint> kv in keysyms)
- if ((uint) c == kv.Value)
+ if ((key & 0x3FFFFF) == kv.Value)
{
mt = kv.Key;
break;
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)
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)
}
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
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; } }
}
}
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;
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]);
}
}
}