2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
14 using Xex = System.Xml.Expression.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (MInputContext ic, MPlist args);
22 public static Callback PreeditStart, PreeditDone, PreeditDraw;
23 public static Callback StatusStart, StatusDone, StatusDraw;
24 public static Callback CandidateStart, CandidateDone, CandidateDraw;
25 public static Callback SetSpot;
26 public static Callback Toggle;
27 public static Callback Reset;
28 public static Callback GetSurroundingText;
29 public static Callback DeleteSurroundingText;
31 internal static Xex.Domain im_domain = new Xex.Domain (null);
32 private static MSymbol Minput_method = "input-method";
33 private static MSymbol Mdescription = "description";
34 private static MSymbol Mvariable = "variable";
35 private static MSymbol Mcommand = "command";
36 private static MSymbol Mmodule = "module";
37 private static MSymbol Mmodule_list = "module-list";
38 private static MSymbol Mtitle = "title";
39 private static MSymbol Minclude = "include";
40 private static MSymbol Mmacro = "macro";
41 private static MSymbol Mmacro_list = "macro-list";
42 private static MSymbol Mmap = "map";
43 private static MSymbol Mmap_list = "map-list";
44 private static MSymbol Mstate = "state";
45 private static MSymbol Mstate_list = "state-list";
46 internal static MSymbol Mcandidates = "candidates";
47 private static MSymbol Minsert = "insert";
48 private static MSymbol Mdelete = "delete";
49 private static MSymbol Mmove = "move";
50 private static MSymbol Mmark = "mark";
51 private static MSymbol Mmarker = "marker";
52 private static MSymbol Madd = "add";
53 private static MSymbol Msub = "sub";
54 private static MSymbol Mmul = "mul";
55 private static MSymbol Mdiv = "div";
56 private static MSymbol Mif = "if";
57 private static MSymbol Mcond = "cond";
58 private static MSymbol Mchar_at = "char-at";
59 private static MSymbol Msurrounding_text_p = "surrounding-text-p";
60 private static MSymbol Mpushback = "pushback";
61 private static MSymbol Mkeyseq = "keyseq";
63 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
64 = new Dictionary<MDatabase.Tag, MInputMethod> ();
67 private class Exception : System.Exception
71 public Exception (string msg) : base (msg)
76 public Exception (string fmt, params object[] args)
77 : base (String.Format (fmt, args))
82 public Exception (string msg, bool error) : base (msg)
89 private enum LoadStatus
99 public enum KeyModifier
102 Shift_L = 0x00400000,
103 Shift_R = 0x00800000,
105 Control_L = 0x01000000,
106 Control_R = 0x02000000,
107 Control = 0x03000000,
122 private static Dictionary<string, uint> keysyms
123 = new Dictionary<string, uint> ();
124 private static Dictionary<string, KeyModifier> keymodifiers
125 = new Dictionary<string, KeyModifier> ();
126 private static uint keysym_base = 0x200000;
127 private static uint char_mask = ~((uint) KeyModifier.All);
131 keysyms["bs"] = keysyms["backspace"] = 0x08;
132 keysyms["tab"] = 0x09;
133 keysyms["lf"] = keysyms["linefeed"] = 0x10;
134 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
135 keysyms["esc"] = keysyms["escape"] = 0x1B;
136 keysyms["spc"] = keysyms["space"] = 0x20;
137 keysyms["del"] = keysyms["delete"] = 0x7F;
138 keymodifiers["shift-l"] = KeyModifier.Shift_L;
139 keymodifiers["shift-r"] = KeyModifier.Shift_R;
140 keymodifiers["shift"] = KeyModifier.Shift;
141 keymodifiers["control-l"] = KeyModifier.Control_L;
142 keymodifiers["control-r"] = KeyModifier.Control_R;
143 keymodifiers["control"] = KeyModifier.Control;
144 keymodifiers["alt-l"] = KeyModifier.Alt_L;
145 keymodifiers["alt-r"] = KeyModifier.Alt_R;
146 keymodifiers["alt"] = KeyModifier.Alt;
147 keymodifiers["altgr"] = KeyModifier.AltGr;
148 keymodifiers["super"] = KeyModifier.Super;
149 keymodifiers["hyper"] = KeyModifier.Hyper;
152 private static uint decode_keysym (MSymbol keysym)
155 string name = keysym.Name;
157 if (name.Length == 1)
159 name = name.ToLower ();
160 if (! keysyms.TryGetValue (name, out key))
161 keysyms[name] = key = keysym_base++;
165 private static uint combine_modifiers (uint c, KeyModifier modifiers)
167 if (c < 0x7F && c != 0x20)
169 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
170 && Char.IsLower ((char) c))
172 modifiers &= ~KeyModifier.Shift;
173 c = Char.ToUpper ((char) c);
175 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
177 modifiers &= ~KeyModifier.Control;
181 return c | (uint) modifiers;
189 public Key (uint c, KeyModifier modifiers)
191 key = combine_modifiers (c, modifiers);
194 public Key (MSymbol keysym, KeyModifier modifiers)
196 key = combine_modifiers (decode_keysym (keysym), modifiers);
199 public Key (MSymbol keysym)
201 string str = keysym.Name;
202 int len = str.Length;
204 KeyModifier modifiers = KeyModifier.None;
206 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
209 modifiers |= KeyModifier.Shift;
210 else if (str[i] == 'C')
211 modifiers |= KeyModifier.Control;
212 else if (str[i] == 'A')
213 modifiers |= KeyModifier.Alt;
214 else if (str[i] == 'G')
215 modifiers |= KeyModifier.AltGr;
216 else if (str[i] == 's')
217 modifiers |= KeyModifier.Super;
218 else if (str[i] == 'H')
219 modifiers |= KeyModifier.Hyper;
222 key = combine_modifiers (str[i], modifiers);
224 key = combine_modifiers (decode_keysym (keysym), modifiers);
227 public Key (MPlist plist)
229 KeyModifier modifiers = KeyModifier.None;
232 for (p = plist; ! p.IsEmpty; p = p.next)
236 if (! p.next.IsEmpty)
237 throw new Exception ("Invalid Key: " + plist);
240 else if (! p.IsSymbol)
241 throw new Exception ("Invalid Key: " + plist);
244 string name = p.Symbol.Name.ToLower ();
247 if (! keymodifiers.TryGetValue (name, out m))
252 if (p.IsEmpty || ! p.next.IsEmpty)
253 throw new Exception ("Invalid Key: " + plist);
255 key = combine_modifiers ((uint) p.Integer, modifiers);
257 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
260 public bool HasModifier
262 get { return ((key & (uint) KeyModifier.All) != 0); }
265 public bool Match (Key k)
269 if ((k.key & char_mask) != (key & char_mask))
271 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
272 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
273 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
274 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
275 && (m2 & KeyModifier.Shift) != KeyModifier.None))
276 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
277 || ((m1 & KeyModifier.Control) == KeyModifier.Control
278 && (m2 & KeyModifier.Control) != KeyModifier.None))
279 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
280 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
281 && (m2 & KeyModifier.Alt) != KeyModifier.None))
282 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
285 public override string ToString ()
287 string str = Char.ToString ((char) key);
288 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
290 if (m != KeyModifier.None)
292 if ((m & KeyModifier.Shift) != KeyModifier.None)
294 if ((m & KeyModifier.Control) != KeyModifier.None)
296 if ((m & KeyModifier.Alt) != KeyModifier.None)
298 if ((m & KeyModifier.AltGr) != KeyModifier.None)
300 if ((m & KeyModifier.Super) != KeyModifier.None)
302 if ((m & KeyModifier.Hyper) != KeyModifier.None)
309 public class KeySeq : Xex.Object
311 List<Key> keyseq = new List<Key> ();
315 public KeySeq (MPlist plist)
317 foreach (MPlist p in plist)
320 keyseq.Add (new Key (p.Symbol));
321 else if (p.IsInteger)
322 keyseq.Add (new Key ((char) p.Integer));
324 keyseq.Add (new Key (p.Plist));
326 throw new Exception ("Invalid Key Sequence: " + plist);
330 public KeySeq (MText mt) : base ()
332 for (int i = 0; i < mt.Length; i++)
333 keyseq.Add (new Key ((uint) mt[i]));
336 private static uint parse_integer (string str)
338 if (Char.IsDigit (str[0]))
340 if (str[0] == '0' && str.Length > 2 && str[1] == 'x')
343 for (int idx = 2; idx < str.Length; idx++)
346 if (c >= '0' && c <= '9')
347 i = i * 16 + (c - '0');
348 else if (c >= 'A' && c <= 'F')
349 i = i * 16 + 10 + (c - 'A');
350 else if (c >= 'a' && c <= 'f')
351 i = i * 16 + 10 + (c - 'a');
357 return UInt32.Parse (str);
359 else if (str[0] == '?')
364 public KeySeq (XmlNode node)
366 XmlAttributeCollection acol = node.Attributes;
369 if (acol != null && (n = acol["keys"]) != null)
371 foreach (char c in n.Value)
372 keyseq.Add (new Key ((uint) c));
375 for (node = node.FirstChild; node != null; node = node.NextSibling)
377 if (node.Name == "key-event")
378 keyseq.Add (new Key ((MSymbol) node.InnerText));
380 keyseq.Add (new Key (parse_integer (node.InnerText)));
384 public KeySeq (List<Xex.Term> list)
386 foreach (Xex.Term term in list)
389 keyseq.Add (new Key ((MSymbol) term.Nameval.name));
391 keyseq.Add (new Key (term.Strval));
395 public override string ToString ()
399 foreach (Key key in this)
403 foreach (Key k in this)
404 str += " " + k.ToString ();
408 foreach (Key key in this)
409 str += key.ToString ();
414 public class Variable
417 public MText description;
420 public object[] candidates;
422 public Variable (MPlist p)
426 description = parse_description (p);
427 if (description == null)
428 description = new MText ("No description");
431 type = (p.IsMText ? typeof (MText)
432 : p.IsInteger ? typeof (int)
433 : p.IsSymbol ? typeof (MSymbol)
437 candidates = new object[p.Count];
438 for (int i = 0; ! p.IsEmpty; i++, p = p.next)
439 candidates[i] = p.val;
442 private static Type parse_value (XmlNode node, out object value)
444 string typename = node.Attributes["type"].Value;
447 if (typename == "integer")
450 if (! Int32.TryParse (node.InnerText, out i))
455 else if (typename == "string")
457 MText mt = node.InnerText;
459 type = typeof (MText);
461 else if (typename == "symbol")
463 MSymbol sym = node.InnerText;
465 type = typeof (MSymbol);
470 type = typeof (object);
475 public Variable (XmlNode node)
477 name = node.Attributes["id"].Value;
478 for (node = node.FirstChild; node != null; node = node.NextSibling)
479 if (node.NodeType == XmlNodeType.Element)
481 if (node.Name == "description")
482 description = parse_description (node);
483 else if (node.Name == "value")
484 type = parse_value (node, out value);
485 else if (node.Name == "valiable-value-candidate")
487 XmlNodeList n_list = node.ChildNodes;
488 candidates = new object[n_list.Count];
489 for (int i = 0; i < n_list.Count; i++)
492 parse_value (n_list[i], out val);
499 public override string ToString ()
501 return ("(" + name + " \"" + (string) description
502 + "\" " + type + " " + value + " " + candidates + ")");
509 public MText description;
510 public List<KeySeq> keys;
512 public Command (MPlist p)
516 description = parse_description (p);
517 if (description == null)
518 description = "No description";
519 keys = new List<KeySeq> ();
520 for (p = p.next; ! p.IsEmpty; p = p.next)
523 keys.Add (new KeySeq (p.Text));
525 keys.Add (new KeySeq (p.Plist));
529 public Command (XmlNode node)
531 name = node.Attributes["id"].Value;
532 keys = new List<KeySeq> ();
533 for (node = node.FirstChild; node != null; node = node.NextSibling)
535 if (node.Name == "description")
536 description = parse_description (node);
537 else if (node.Name == "keyseq")
538 keys.Add (new KeySeq (node));
542 public override string ToString ()
544 string str = "(" + name + " \"" + (string) description;
545 foreach (KeySeq keyseq in keys)
551 internal class Plugin
554 public Assembly assembly;
555 public MPlist methods;
557 public override string ToString ()
559 string str = "(" + name;
560 for (MPlist p = methods; ! p.IsEmpty; p = p.next)
569 public Dictionary<Key, Map> submaps;
572 public void Add (KeySeq keys, int index, Xex actions)
577 submaps = new Dictionary<Key, Map> ();
579 submaps.TryGetValue (keys[index], out sub);
582 Key key = keys[index];
583 submaps[key] = sub = new Map ();
585 if (index + 1 < keys.Count)
586 sub.Add (keys, index + 1, actions);
588 this.actions = actions;
591 public Xex Lookup (KeySeq keys, int index)
595 if (index + 1 == keys.Count)
597 if (submaps.TryGetValue (keys[index], out sub))
598 return sub.Lookup (keys, index + 1);
602 private void describe (MText mt, KeySeq keyseq)
604 if (keyseq.Count > 0)
606 mt.Cat (" (").Cat (keyseq.ToString ());
608 mt.Cat (' ').Cat (actions.ToString ());
612 foreach (KeyValuePair<Key, Map> kv in submaps)
615 kv.Value.describe (mt, keyseq);
616 keyseq.RemoveAt (keyseq.Count - 1);
620 public override string ToString ()
622 MText mt = "(" + name.Name;
623 KeySeq keyseq = new KeySeq ();
625 describe (mt, keyseq);
635 public MPlist branches = new MPlist ();
637 public State (MSymbol name)
642 public override string ToString ()
644 MText mt = "(" + name.Name;
647 mt.Cat (" \"" + title + "\"");
648 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
649 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
650 return (string) mt + ")";
655 internal Xex.Domain domain;
657 private LoadStatus load_status = LoadStatus.None;
658 private MDatabase.Tag tag;
659 private MDatabase mdb;
661 private MText description;
662 internal MText title;
663 internal Command[] commands;
664 internal Xex.Name[] var_names;
665 internal Dictionary<MSymbol, Plugin> plugins;
666 internal Dictionary<MSymbol, Map> maps;
667 internal MPlist states;
669 static MInputMethod ()
671 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
672 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
673 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
674 im_domain.DefSubr (Fselect, "select", true, 1, 1);
675 im_domain.DefSubr (Fshow, "show", true, 0, 0);
676 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
677 im_domain.DefSubr (Fmove, "move", true, 1, 1);
678 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
679 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
680 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
681 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
682 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
683 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
684 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
685 im_domain.DefSubr (Fcall, "call", true, 2, -1);
686 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
687 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
688 im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
690 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
691 List<MDatabase> list = MDatabase.List (tag);
692 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
693 foreach (MDatabase mdb in list)
694 im_table[mdb.tag] = new MInputMethod (mdb.tag);
698 private MInputMethod (MDatabase.Tag tag)
703 // Instance Properties
704 public MSymbol Language { get { return tag[1]; } }
705 public MSymbol Name { get { return tag[2]; } }
706 public MSymbol SubName { get { return tag[3]; } }
708 public bool Info (out MText description,
710 out Xex.Variable[] variables,
711 out Command[] commands)
713 if ((load_status & LoadStatus.Header) != LoadStatus.Header
722 description = this.description;
724 variables = new Xex.Variable[var_names.Length];
726 foreach (Xex.Name name in var_names)
727 variables[i++] = domain.GetVar (name);
728 commands = this.commands;
732 public static MInputMethod Find (MSymbol language, MSymbol name)
734 return Find (language, name, MSymbol.nil);
737 public static MInputMethod Find (MSymbol language, MSymbol name,
740 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
744 return (im_table.TryGetValue (tag, out im) ? im : null);
749 return ((load_status == LoadStatus.Full) || load_body ());
752 public static MInputMethod[] List ()
754 MInputMethod[] array = new MInputMethod[im_table.Count];
757 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
758 array[i++] = kv.Value;
762 private bool load_header ()
764 mdb = MDatabase.Find (tag);
768 MSymbol format = mdb.Format;
770 if (format == MSymbol.plist)
771 load ((MPlist) mdb.Load (Mmap), false);
774 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
775 load (doc.DocumentElement, false);
777 } catch (Exception e) {
778 Console.WriteLine ("{0}\n", e);
779 load_status = LoadStatus.Error;
782 load_status |= LoadStatus.Header;
786 private bool load_body ()
788 domain = new Xex.Domain (domain, null);
789 mdb = MDatabase.Find (tag);
793 object obj = mdb.Load ();
795 load ((MPlist) obj, true);
797 load ((XmlDocument) obj, true);
798 } catch (Exception e) {
799 Console.WriteLine (e);
800 load_status = LoadStatus.Error;
803 load_status = LoadStatus.Full;
807 private void load (MPlist plist, bool full)
809 maps = new Dictionary<MSymbol, Map> ();
810 states = new MPlist ();
812 for (; ! plist.IsEmpty; plist = plist.next)
815 MPlist pl = plist.Plist;
818 MSymbol sym = pl.Symbol;
821 if (sym == Mdescription)
823 description = parse_description (pl);
824 if (description == null)
825 description = new MText ("No description");
827 else if (sym == Mtitle)
832 else if (sym == Mvariable)
833 parse_variables (pl);
834 else if (sym == Mcommand)
840 else if (sym == Minclude)
842 else if (sym == Mmacro)
844 else if (sym == Mmap)
846 else if (sym == Mstate)
851 if (description == null)
852 description = (MText) "No description";
854 title = new MText (tag[2].Name);
855 if (variables == null)
856 variables = new Variable[0];
857 if (commands == null)
858 commands = new Command[0];
863 State state = new State ((MSymbol) "init");
864 plist = new MPlist ();
865 foreach (KeyValuePair<MSymbol, Map>kv in maps)
866 state.branches.Add (kv.Key, new Xex (plist, domain));
867 states.Add (state.name, state);
871 private void load (XmlNode node, bool full)
873 bool skip_header = load_status == LoadStatus.Header;
875 maps = new Dictionary<MSymbol, Map> ();
876 states = new MPlist ();
878 if (node.NodeType == XmlNodeType.Document)
879 node = node.FirstChild;
880 while (node.NodeType != XmlNodeType.Element)
881 node = node.NextSibling;
882 for (node = node.FirstChild; node != null; node = node.NextSibling)
884 if (node.NodeType != XmlNodeType.Element)
888 if (node.Name == "description")
889 description = parse_description (node);
890 else if (node.Name == "title")
891 title = parse_title (node);
892 else if (node.Name == "variable-list")
893 parse_variables (node);
894 else if (node.Name == "command-list")
895 parse_commands (node);
899 if (node.Name == "module-list")
900 parse_plugins (node);
901 else if (node.Name == "macro-list")
903 else if (node.Name == "map-list")
905 else if (node.Name == "state-list")
909 if (description == null)
910 description = (MText) "No description";
912 title = new MText (tag[2].Name);
913 if (variables == null)
914 variables = new Variable[0];
915 if (commands == null)
916 commands = new Command[0];
921 State state = new State ((MSymbol) "init");
922 MPlist plist = new MPlist ();
923 foreach (KeyValuePair<MSymbol, Map>kv in maps)
924 state.branches.Add (kv.Key, new Xex (plist, domain));
925 states.Add (state.name, state);
929 private static void transform (MPlist plist)
931 for (; ! plist.IsEmpty; plist = plist.next)
935 MPlist p = new MPlist ();
936 p.Add (MSymbol.symbol, Minsert);
937 p.Add (MSymbol.mtext, plist.Text);
938 plist.Set (MSymbol.plist, p);
940 else if (plist.IsInteger)
942 MPlist p = new MPlist ();
943 p.Add (MSymbol.symbol, Minsert);
944 p.Add (MSymbol.integer, plist.Integer);
945 plist.Set (MSymbol.plist, p);
947 else if (plist.IsPlist)
949 MPlist pl = plist.Plist;
953 if (pl.Symbol == Madd)
954 pl.Set (MSymbol.symbol, (MSymbol) "+=");
955 else if (pl.Symbol == Msub)
956 pl.Set (MSymbol.symbol, (MSymbol) "-=");
957 else if (pl.Symbol == Mmul)
958 pl.Set (MSymbol.symbol, (MSymbol) "*=");
959 else if (pl.Symbol == Mdiv)
960 pl.Set (MSymbol.symbol, (MSymbol) "/=");
961 else if (pl.Symbol == Minsert)
963 // (insert (CANDIDATES ...))
964 // => (candidates CANDIDATES ...)
967 pl.Set (MSymbol.symbol, Mcandidates);
970 pl.Set (p.key, p.val);
971 for (p = p.next; ! p.IsEmpty; p = p.next);
972 pl.Add (p.key, p.val);
975 else if (pl.Symbol == Mif)
981 else if (pl.Symbol == Mcond)
983 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
994 else if (pl.Symbol == Mdelete
995 || pl.Symbol == Mmove
996 || pl.Symbol == Mmark)
1001 MSymbol sym = pl.Symbol;
1002 MPlist p = new MPlist ();
1003 p.Add (MSymbol.symbol, Mmarker);
1004 p.Add (MSymbol.symbol, sym);
1005 pl.Set (MSymbol.plist, p);
1008 else if (pl.Symbol == Mpushback)
1012 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1015 else if (pl.IsMText)
1017 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1018 pl.Push (MSymbol.symbol, Mcandidates);
1021 else if (plist.IsSymbol)
1023 MSymbol sym = plist.Symbol;
1025 if (sym.Name.Length >= 3
1026 && sym.Name[0] == '@'
1027 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1029 int pos = int.Parse (sym.Name.Substring (1));
1030 MPlist p = new MPlist ();
1034 p.Add (MSymbol.symbol, Msurrounding_text_p);
1038 if (sym.Name[1] == '+')
1040 p.Add (MSymbol.symbol, Mchar_at);
1041 p.Add (MSymbol.integer, pos);
1043 plist.Set (MSymbol.plist, p);
1049 private static MText parse_description (MPlist plist)
1055 plist = plist.Plist;
1056 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1057 && plist.next.IsMText)
1058 return plist.next.Text;
1063 private static MText parse_description (XmlNode node)
1065 if (node.HasChildNodes)
1066 node = node.FirstChild;
1067 return node.InnerText;
1070 private static MText parse_title (XmlNode node)
1072 return node.InnerText;
1075 private void new_variable (Xex.Name name, string desc, int val,
1076 MPlist pl, Xex.Variable vari)
1078 Xex.Term term = new Xex.Term (val);
1085 range = new int[pl.Count * 2];
1086 for (int i = 0; i < range.Length; i++)
1090 MPlist p = pl.Plist;
1092 if (! p.IsInteger || ! p.next.IsInteger)
1093 throw new Exception ("Invalid range: " + p);
1094 range[i * 2] = p.Integer;
1095 range[i * 2 + 1] = p.next.Integer;
1097 else if (pl.IsInteger)
1098 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1100 throw new Exception ("Invalid range: " + p);
1104 domain.Defvar (new Xex.Variable.Int (name, desc, term, range));
1108 vari.DefaultValue = term;
1113 private void new_variable (Xex.Name name, string desc, MText val,
1114 MPlist pl, Xex.Variable vari)
1116 Xex.Term term = new Xex.Term ((string) val);
1123 range = new string[pl.Count * 2];
1124 for (int i = 0; i < range.Length; i++)
1129 throw new Exception ("Invalid range: " + p);
1133 domain.Defvar (new Xex.Variable.Str (name, desc, term, range));
1137 vari.DefaultValue = term;
1142 private void new_variable (Xex.Name name, string desc, MSymbol val,
1143 MPlist pl, Xex.Variable vari)
1145 Xex.Term term = new Xex.Term ((Xex.Name) val.name);
1152 range = new Xex.Name[pl.Count * 2];
1153 for (int i = 0; i < range.Length; i++)
1156 range[i] = pl.Symbol.name;
1158 throw new Exception ("Invalid range: " + p);
1162 domain.Defvar (new Xex.Variable.Sym (name, desc, term, range));
1166 vari.DefaultValue = term;
1171 private void parse_variables (MPlist plist)
1173 variables = new Variable[plist.Count];
1175 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1176 if (plist.IsPlist && plist.Plist.IsSymbol)
1178 MPlist p = plist.Plist;
1179 Xex.Name name = (Name) p.Symbol.name;
1181 string desc = parse_description (p);
1182 Xex.Variable vari = null;
1185 if (im_global.domain.GetVar (name, out vari))
1186 domain.Defvar (vari);
1192 new_variable (name, desc, p.Integer, p.next, vari);
1194 new_variable (name, desc, p.Text, p.next, vari);
1195 else if (p.IsSymbol)
1196 new_variable (name, desc, p.Symbol, p.next, vari);
1198 throw new Exception ("Unknown type: " + p.val);
1203 private void parse_variables (XmlNode node)
1205 XmlNodeList node_list = node.ChildNodes;
1207 variables = new Variable[node_list.Count];
1208 for (int i = 0; i < node_list.Count; i++)
1209 if (node_list[i].NodeType == XmlNodeType.Element)
1210 variables[i] = new Variable (node_list[i]);
1213 private void parse_commands (MPlist plist)
1215 commands = new Command[plist.Count];
1217 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1218 if (plist.IsPlist && plist.Plist.IsSymbol)
1219 commands[i++] = new Command (plist.Plist);
1222 private void parse_commands (XmlNode node)
1224 XmlNodeList node_list = node.ChildNodes;
1226 commands = new Command[node_list.Count];
1227 for (int i = 0; i < node_list.Count; i++)
1229 if (node_list[i].NodeType == XmlNodeType.Element)
1230 commands[i] = new Command (node_list[i]);
1234 private void parse_plugins (MPlist plist)
1236 plugins = new Dictionary<MSymbol, Plugin> ();
1238 for (; ! plist.IsEmpty; plist = plist.Next)
1240 MPlist p = plist.Plist;
1241 MSymbol sym = p.Symbol;
1242 Plugin plugin = new Plugin ();
1244 plugin.name = sym.Name;
1245 plugin.methods = new MPlist ();
1246 for (p = p.next; ! p.IsEmpty; p = p.next)
1247 plugin.methods.Add (p.Symbol, null);
1248 plugins.Add (sym, plugin);
1252 private void parse_plugins (XmlNode node)
1254 plugins = new Dictionary<MSymbol, Plugin> ();
1256 foreach (XmlNode n in node.ChildNodes)
1258 Plugin plugin = new Plugin ();
1259 plugin.name = n.Attributes["id"].Value;
1260 plugin.methods = new MPlist ();
1261 foreach (XmlNode nn in n.ChildNodes)
1262 plugin.methods.Add ((MSymbol) nn.Attributes["id"].Value,
1264 plugins.Add (plugin.name, plugin);
1268 private void parse_macros (XmlNode node)
1270 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1271 if (nn.NodeType == XmlNodeType.Element
1272 && nn.Name == "xi:include")
1274 XmlNode n = nn.FirstChild.FirstChild;
1275 MSymbol language = n.InnerText;
1277 MSymbol name = n.InnerText;
1279 MSymbol subname = (n != null ? n.InnerText : MSymbol.nil);
1280 n = n.ParentNode.NextSibling;
1281 MSymbol section = n.InnerText;
1283 MSymbol id = (n != null ? n.InnerText : MSymbol.nil);
1285 MInputMethod im = MInputMethod.Find (language, name, subname);
1286 if (im == null || ! im.Open ())
1288 if (id == MSymbol.nil)
1289 im.domain.CopyFunc (domain);
1291 im.domain.CopyFunc (domain, id);
1293 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1294 if (nn.NodeType == XmlNodeType.Element
1295 && nn.Name != "xi:include")
1296 domain.Defun ((MSymbol) node.GetAttribute ("id"));
1297 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1298 if (nn.NodeType == XmlNodeType.Element
1299 && nn.Name != "xi:include")
1300 domain.Defun ((MSymbol) node.GetAttribute ("id"), null,
1304 private void parse_maps (XmlNode node)
1308 private void parse_states (XmlNode node)
1312 private void parse_include (MPlist plist)
1314 if (! plist.IsPlist)
1316 MPlist p = plist.Plist;
1317 MSymbol language, name, subname;
1318 language = p.Symbol;
1321 name = subname = MSymbol.nil;
1327 subname = MSymbol.nil;
1332 MInputMethod im = MInputMethod.Find (language, name, subname);
1338 if (! plist.IsSymbol)
1340 MSymbol target_type = plist.Symbol;
1342 MSymbol target_name = MSymbol.nil;
1344 target_name = plist.Symbol;
1345 if (target_type == Mmacro)
1347 if (target_name == MSymbol.nil)
1348 im.domain.CopyFunc (domain);
1350 im.domain.CopyFunc (domain, target_name);
1352 else if (target_type == Mmap)
1354 if (target_name == MSymbol.nil)
1356 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1357 maps[kv.Key] = kv.Value;
1362 if (im.maps.TryGetValue (target_name, out map))
1363 maps[target_name] = map;
1366 else if (target_type == Mstate)
1368 if (target_name == MSymbol.nil)
1370 for (p = im.states; ! p.IsEmpty; p = p.next)
1371 states.Add (p.key, p.val);
1375 object state = im.states.Get (target_name);
1377 states.Add (target_name, state);
1382 private void parse_macros (MPlist plist)
1384 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1387 MPlist p = pl.Plist;
1391 domain.Defun (p.Symbol, null, null);
1393 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1396 MPlist p = pl.Plist;
1401 domain.Defun (p.Symbol, null, p.next);
1405 private void parse_maps (MPlist plist)
1407 for (; ! plist.IsEmpty; plist = plist.next)
1410 MPlist pl = plist.Plist;
1414 Map map = new Map ();
1415 map.name = pl.Symbol;
1416 maps[map.name] = map;
1417 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1421 MPlist p = pl.Plist;
1424 keys = new KeySeq (p.Text);
1426 keys = new KeySeq (p.Plist);
1433 MXex expr = new MXex (p, domain);
1434 map.Add (keys, 0, expr);
1439 private void parse_states (MPlist plist)
1441 for (; ! plist.IsEmpty; plist = plist.next)
1444 MPlist pl = plist.Plist;
1455 State state = new State (pl.Symbol);
1456 state.title = title;
1458 states = new MPlist ();
1459 states.Add (state.name, state);
1460 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1464 MPlist p = pl.Plist;
1467 MSymbol map_name = p.Symbol;
1470 state.branches.Add (map_name,
1471 new MXex (p, domain));
1476 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1479 ((MInputContext) domain.context).insert (args[0]);
1483 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1487 ((MInputContext) domain.context).insert_candidates (args[0]);
1491 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1494 MSymbol sym = (MSymbol) args[0].Args[0].Val;
1496 return ((MInputContext) domain.context).marker (sym);
1499 private static Xex.Term char_at (Xex.Domain domain, Xex.Variable vari,
1502 return ((MInputContext) domain.context).char_at ((int) args[0].Val);
1505 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1508 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1512 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1515 MInputContext ic = (MInputContext) domain.context;
1518 ic.select (args[0].Intval);
1520 ic.select ((MSYmbol) ((string) args[0].Nameval));
1524 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1527 ((MInputContext) domain.context).show ();
1531 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1534 ((MInputContext) domain.context).hide ();
1538 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1541 ((MInputContext) domain.context).move (args[0].Intval);
1545 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1548 ((MInputContext) domain.context).mark (args[0]);
1552 private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
1555 return new KeySeq (args[0].Listval);
1558 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1561 MInputContext ic = (MInputContext) domain.context;
1564 ic.pushback (args[0].Intval);
1565 else if (args[0].IsStr)
1566 ic.pushback (new KeySeq (args[0].Strval));
1568 ic.pushback ((KeySeq) args[0].Objval);
1572 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1575 ((MInputContext) domain.context).pop ();
1579 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1582 int n = args.Length == 0 ? -2 : args[0].Intval;
1583 ((MInputContext) domain.context).undo (n);
1587 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1590 ((MInputContext) domain.context).commit ();
1594 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1597 ((MInputContext) domain.context).commit ();
1601 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1604 ((MInputContext) domain.context).shift (args[0].Symval);
1608 private static Xex.Term Fcall (Xex.Domain domain, Xex.Variable vari, Xex.Term[] args)
1610 MSymbol module = (MSymbol) args[0].Args[0].Val;
1611 MSymbol method = (MSymbol) args[1].Args[0].Val;
1612 MPlist arglist = new MPlist ();
1614 for (int i = 2; i < args.Length; i++)
1616 object val = args[i].Eval (domain);
1619 arglist.Add (MSymbol.integer, val);
1620 else if (val is MSymbol)
1621 arglist.Add (MSymbol.symbol, val);
1622 else if (val is MText)
1623 arglist.Add (MSymbol.mtext, val);
1624 else if (val is MPlist)
1625 arglist.Add (MSymbol.plist, val);
1627 throw new Exception ("Invalid argument to {0}/{1}: {2}",
1628 module, method, val);
1630 return ((MInputContext) domain.context).call (module, method, arglist);
1633 public override string ToString ()
1635 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1636 if (commands != null)
1638 str += " (commands";
1639 foreach (Command cmd in commands)
1643 if (variables != null)
1645 str += " (variables";
1646 foreach (Variable var in variables)
1650 if (plugins != null)
1653 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1654 str += " " + kv.Value;
1658 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1659 str += " " + kv.Value;
1661 foreach (MPlist p in states)
1667 public class MInputContext
1669 internal static MSymbol Mcandidates_group_size = "candidates-group-size";
1670 private static MSymbol Mat_less_than = "@<";
1671 private static MSymbol Mat_greater_than = "@>";
1672 private static MSymbol Mat_minus = "@-";
1673 private static MSymbol Mat_plus = "@+";
1674 private static MSymbol Mat_open_square_bracket = "@[";
1675 private static MSymbol Mat_close_square_bracket = "@]";
1677 public MInputMethod im;
1678 private MText produced;
1679 private bool active;
1680 private MText status;
1681 private bool status_changed;
1682 private MText preedit;
1683 private bool preedit_changed;
1684 private int cursor_pos;
1685 private bool cursor_pos_changed;
1686 private Candidates candidates;
1687 private MPlist candidate_group;
1688 private int candidate_index;
1689 private int candidate_from, candidate_to;
1690 private bool candidate_show;
1691 private bool candidate_changed;
1693 private Stack<MInputMethod.State> states;
1694 internal MInputMethod.KeySeq keys;
1695 private int key_head;
1696 private int state_key_head;
1697 private MPlist state_boundary;
1698 private int commit_key_head;
1699 private MText state_preedit;
1700 private int state_pos;
1701 internal MPlist markers = new MPlist ();
1702 private MPlist vars;
1703 private MPlist vars_saved;
1704 internal MText preceding_text = new MText ();
1705 internal MText following_text = new MText ();
1706 private bool key_unhandled;
1708 internal Xex.Domain domain;
1710 public MInputContext (MInputMethod im)
1713 domain = new Xex.Domain (im.domain, this);
1714 states = new Stack<MInputMethod.State> ();
1715 states.Push ((MInputMethod.State) im.states.val);
1716 keys = new MInputMethod.KeySeq ();
1719 private void adjust_markers (int from, int to, object inserted)
1721 int ins = (inserted == null ? 0
1722 : inserted is int ? 1
1723 : ((MText) inserted).Length);
1724 int diff = ins - (to - from);
1726 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1728 int pos = plist.Integer;
1732 plist.val = pos + diff;
1737 if (cursor_pos >= to)
1739 else if (cursor_pos > from)
1743 private void preedit_replace (int from, int to, int c)
1745 preedit.Del (from, to);
1746 preedit.Ins (from, c);
1747 adjust_markers (from, to, c);
1750 private void preedit_replace (int from, int to, MText mt)
1752 preedit[from, to] = mt;
1753 adjust_markers (from, to, mt);
1756 internal void insert (Xex.Term arg)
1758 if (arg.IsInt is int)
1759 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1761 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1762 preedit_changed = true;
1763 cursor_pos_changed = true;
1766 private class Candidates
1773 public Block (int index, MPlist plist)
1784 get { return (Data is MText
1785 ? ((MText) Data).Length
1786 : ((MPlist) Data).Count); }
1789 public object this[int i]
1792 if (Data is MText) return ((MText) Data)[i];
1793 return ((MPlist) Data)[i];
1798 private Block[] blocks;
1799 private int row = 0;
1800 private int index = 0;
1801 public object[] group;
1803 private bool IsFixed { get { return group != null; } }
1806 Block last = blocks[blocks.Length - 1];
1807 return last.Index + last.Count; }
1811 get { return (IsFixed ? index % group.Length
1812 : index - blocks[row].Index); }
1815 public object Group {
1816 get { return (IsFixed ? group : blocks[row].Data); }
1819 public int GroupLength
1824 int nitems = group.Length;
1825 int start = index - (index % nitems);
1827 return (start + nitems <= total ? nitems : total - start);
1829 return blocks[row].Count;
1833 public object Current {
1835 return (IsFixed ? group[index % group.Length]
1836 : blocks[row][index - blocks[row].Index]);
1840 public Candidates (MPlist list, int column)
1842 int nblocks = list.Count;
1844 blocks = new Block[nblocks];
1845 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
1846 start += (blocks[i] = new Block (index, list)).Count;
1848 group = new object[column];
1851 public Candidates (List<Xex.Term> list, int column)
1853 int nblocks = list.Count;
1855 blocks = new Block[nblocks];
1856 for (int i = 0, start = 0; i < nblocks; i++)
1857 start += (blocks[i] = new Block (index, list[i])).Count;
1859 group = new object[column];
1862 public static void Detach (MInputContext ic)
1864 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
1865 ic.candidates = null;
1866 ic.preedit_changed = true;
1867 ic.cursor_pos_changed = true;
1868 ic.candidate_changed = true;
1871 // Fill the array "group" by candidates stating from INDEX.
1872 // INDEX must be a multiple of "column". Set NTIMES to the
1873 // number of valid candidates in "group". Update "block" if
1874 // necessary. Return "group".
1876 private int fill_group (int start)
1878 int nitems = group.Length;
1880 Block b = blocks[r];
1882 if (start < b.Index)
1883 while (start < b.Index)
1886 while (start >= b.Index + b.Count)
1890 int count = b.Count;
1892 for (int i = 0; i < nitems; i++, start++)
1897 if (r == blocks.Length)
1903 group[i] = b[start];
1908 // Update "row" to what contains the first candidate of
1909 // the previous candidate-group, update "current_index", and
1910 // update "group" if necessary. Return the previous
1911 // candidate-group. Set NITEMS to the number of valid
1912 // candidates contained in that group.
1914 public int PrevGroup ()
1921 nitems = group.Length;
1922 if ((index -= col + nitems) < 0)
1923 index = (Total / nitems) * nitems;
1924 nitems = fill_group (index);
1928 row = row > 0 ? row-- : blocks.Length - 1;
1929 nitems = blocks[row].Count;
1930 index = blocks[row].Index;
1932 index += col < nitems ? col : nitems - 1;
1936 public int NextGroup ()
1943 nitems = group.Length;
1944 if ((index += nitems - col) >= Total)
1946 nitems = fill_group (index);
1950 row = row < blocks.Length - 1 ? row + 1 : 0;
1951 nitems = blocks[row].Count;
1952 index = blocks[row].Count;
1954 index += col < nitems ? col : nitems - 1;
1964 int nitems = PrevGroup ();
1965 index += col < nitems - 1 ? col : nitems - 1;
1974 int nitems = GroupLength;
1976 if (col == nitems - 1)
1978 nitems = NextGroup ();
1985 public void First ()
1992 index += GroupLength - (Column + 1);
1995 public void Select (int col)
1997 int maxcol = GroupLength - 1;
2000 index = index - Column + col;
2004 private void update_candidate ()
2006 object candidate = candidates.Current;
2008 if (candidate is MText)
2010 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2011 candidate_to = candidate_from + ((MText) candidate).Length;
2015 preedit_replace (candidate_from, candidate_to, (int) candidate);
2016 candidate_to = candidate_from + 1;
2018 preedit.PushProp (candidate_from, candidate_to,
2019 MInputMethod.Mcandidates, this);
2020 cursor_pos = candidate_from;
2021 preedit_changed = true;
2022 cursor_pos_changed = true;
2023 candidate_changed = true;
2026 internal void insert_candidates (Xex.Term arg)
2030 if (domain.IsBound (Mcandidates_group_size))
2032 object val = domain.GetValue (Mcandidates_group_size);
2036 candidates = new Candidates (arg.Listval, column);
2037 candidate_from = candidate_to = cursor_pos;
2038 update_candidate ();
2041 internal void select (int n)
2043 if (candidates != null)
2045 candidates.Select (n);
2046 update_candidate ();
2050 internal void select (MSymbol sym)
2052 if (candidates != null)
2054 if (sym == Mat_less_than)
2055 candidates.First ();
2056 else if (sym == Mat_greater_than)
2058 else if (sym == Mat_minus)
2060 else if (sym == Mat_plus)
2062 else if (sym == Mat_open_square_bracket)
2063 candidates.PrevGroup ();
2064 else if (sym == Mat_close_square_bracket)
2065 candidates.NextGroup ();
2069 internal int marker (MSymbol sym)
2071 int pos = cursor_pos;
2073 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2075 switch (sym.Name[0])
2077 case '<': pos = 0; break;
2078 case '>': pos = preedit.Length; break;
2079 case '-': pos = cursor_pos - 1; break;
2080 case '+': pos = cursor_pos + 1; break;
2085 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2092 if (cursor_pos < preedit.Length - 1)
2095 preedit.FindProp (MInputMethod.Mcandidates, pos,
2099 pos = preedit.Length;
2102 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2107 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2109 pos = int.Parse (sym.Name.Substring (2));
2113 object val = markers.Get (sym);
2121 internal int char_at (int pos)
2128 if (preceding_text.Length < -pos)
2130 MPlist plist = new MPlist ();
2131 plist.Push (MSymbol.integer, pos);
2132 if (MInputMethod.GetSurroundingText != null
2133 && MInputMethod.GetSurroundingText (this, plist)
2135 && preceding_text.Length < plist.Text.Length)
2136 preceding_text = plist.Text;
2138 c = (-pos < preceding_text.Length
2139 ? preceding_text[preceding_text.Length + pos] : -1);
2141 else if (pos >= 0 && pos < preedit.Length)
2145 pos -= preedit.Length;
2146 if (pos >= following_text.Length)
2148 MPlist plist = new MPlist ();
2149 plist.Push (MSymbol.integer, pos + 1);
2150 if (MInputMethod.GetSurroundingText != null
2151 && MInputMethod.GetSurroundingText (this, plist)
2153 && following_text.Length < plist.Text.Length)
2154 following_text = plist.Text;
2156 c = (pos < following_text.Length ? following_text[pos] : -1);
2161 internal void delete (int pos)
2163 if (pos < cursor_pos)
2164 preedit_replace (pos, cursor_pos, null);
2166 preedit_replace (cursor_pos, pos, null);
2167 preedit_changed = true;
2168 cursor_pos_changed = true;
2171 internal void show ()
2173 candidate_show = true;
2174 candidate_changed = true;
2177 internal void hide ()
2179 candidate_show = false;
2180 candidate_changed = true;
2183 internal void move (int pos)
2187 else if (pos > preedit.Length)
2188 pos = preedit.Length;
2189 if (pos != cursor_pos)
2192 preedit_changed = true;
2196 internal void mark (MSymbol sym)
2198 MPlist slot = markers.Find (sym);
2201 markers.Push (sym, cursor_pos);
2203 slot.val = cursor_pos;
2206 internal void pushback (int n)
2219 if (key_head > keys.Count)
2220 key_head = keys.Count;
2224 internal void pushback (MInputMethod.KeySeq keyseq)
2228 if (key_head < keys.Count)
2229 keys.RemoveRange (key_head, keys.Count - key_head);
2230 for (int i = 0; i < keyseq.Count; i++)
2231 keys.Add (keyseq[i]);
2234 internal void pop ()
2236 if (key_head < keys.Count)
2237 keys.RemoveRange (key_head, 1);
2240 internal void undo (int n)
2243 keys.RemoveRange (keys.Count + n, - n);
2245 keys.RemoveRange (n, keys.Count - n);
2249 internal void commit ()
2251 produced.Cat (preedit);
2253 preedit_changed = true;
2256 internal void shift (MSymbol sym)
2258 MInputMethod.State state;
2260 if (sym == MSymbol.t)
2262 if (states.Count > 1)
2263 state = states.Pop ();
2265 state = states.Peek ();
2269 state = (MInputMethod.State) im.states.Get (sym);
2271 throw new Exception ("Unknown state: " + state.name);
2274 state = states.Pop ();
2275 if (state == (MInputMethod.State) im.states.val)
2282 state_key_head = key_head;
2283 state_pos = cursor_pos;
2284 state_preedit = preedit.Dup ();
2285 if (state != states.Peek ())
2287 states.Push (state);
2288 state_boundary = domain.SetBoundary ();
2289 status = state.title;
2292 status_changed = true;
2294 = (Xex) state.branches.Get (MSymbol.t);
2295 if (on_entry != null)
2296 on_entry.Eval (domain);
2301 internal bool call (MSymbol module, MSymbol method, MPlist arglist)
2303 MInputMethod.Plugin plugin;
2305 if (! im.plugins.TryGetValue (module, out plugin))
2307 if (plugin.assembly == null)
2312 assembly = Assembly.LoadFrom (module.Name + ".dll");
2316 Type t = assembly.GetType ("Plugin");
2317 for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
2318 p.Set (p.key, t.GetMethod (p.key.Name));
2321 MethodInfo method_info = (MethodInfo) plugin.methods.Get (method);
2322 if (method_info == null)
2324 object[] method_arg = new object[1];
2325 method_arg[0] = arglist;
2326 bool result = (bool) method_info.Invoke (null, method_arg);
2329 if (! arglist.IsEmpty)
2330 (new Xex (arglist, domain)).Eval (domain);
2334 internal void reset ()
2337 state_preedit.Del ();
2341 key_head = commit_key_head = 0;
2343 states.Push ((MInputMethod.State) im.states.Val);
2348 internal object GetCandidates (out int column)
2351 if (cursor_pos == 0)
2353 Candidates candidates
2354 = (Candidates) preedit.GetProp (cursor_pos - 1,
2355 MInputMethod.Mcandidates);
2356 if (candidates == null)
2358 column = candidates.Column;
2359 return candidates.Current;
2362 internal void HandleKey ()
2364 MInputMethod.State state = states.Peek ();