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> ();
66 internal static MInputMethod im_global;
69 private class Exception : System.Exception
73 public Exception (string msg) : base (msg)
78 public Exception (string fmt, params object[] args)
79 : base (String.Format (fmt, args))
84 public Exception (string msg, bool error) : base (msg)
91 private enum LoadStatus
101 public enum KeyModifier
104 Shift_L = 0x00400000,
105 Shift_R = 0x00800000,
107 Control_L = 0x01000000,
108 Control_R = 0x02000000,
109 Control = 0x03000000,
124 private static Dictionary<string, uint> keysyms
125 = new Dictionary<string, uint> ();
126 private static Dictionary<string, KeyModifier> keymodifiers
127 = new Dictionary<string, KeyModifier> ();
128 private static uint keysym_base = 0x200000;
129 private static uint char_mask = ~((uint) KeyModifier.All);
133 keysyms["bs"] = keysyms["backspace"] = 0x08;
134 keysyms["tab"] = 0x09;
135 keysyms["lf"] = keysyms["linefeed"] = 0x10;
136 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
137 keysyms["esc"] = keysyms["escape"] = 0x1B;
138 keysyms["spc"] = keysyms["space"] = 0x20;
139 keysyms["del"] = keysyms["delete"] = 0x7F;
140 keymodifiers["shift-l"] = KeyModifier.Shift_L;
141 keymodifiers["shift-r"] = KeyModifier.Shift_R;
142 keymodifiers["shift"] = KeyModifier.Shift;
143 keymodifiers["control-l"] = KeyModifier.Control_L;
144 keymodifiers["control-r"] = KeyModifier.Control_R;
145 keymodifiers["control"] = KeyModifier.Control;
146 keymodifiers["alt-l"] = KeyModifier.Alt_L;
147 keymodifiers["alt-r"] = KeyModifier.Alt_R;
148 keymodifiers["alt"] = KeyModifier.Alt;
149 keymodifiers["altgr"] = KeyModifier.AltGr;
150 keymodifiers["super"] = KeyModifier.Super;
151 keymodifiers["hyper"] = KeyModifier.Hyper;
154 private static uint decode_keysym (MSymbol keysym)
157 string name = keysym.Name;
159 if (name.Length == 1)
161 name = name.ToLower ();
162 if (! keysyms.TryGetValue (name, out key))
163 keysyms[name] = key = keysym_base++;
167 private static uint combine_modifiers (uint c, KeyModifier modifiers)
169 if (c < 0x7F && c != 0x20)
171 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
172 && Char.IsLower ((char) c))
174 modifiers &= ~KeyModifier.Shift;
175 c = Char.ToUpper ((char) c);
177 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
179 modifiers &= ~KeyModifier.Control;
183 return c | (uint) modifiers;
191 public Key (uint c, KeyModifier modifiers)
193 key = combine_modifiers (c, modifiers);
196 public Key (MSymbol keysym, KeyModifier modifiers)
198 key = combine_modifiers (decode_keysym (keysym), modifiers);
201 public Key (MSymbol keysym)
203 string str = keysym.Name;
204 int len = str.Length;
206 KeyModifier modifiers = KeyModifier.None;
208 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
211 modifiers |= KeyModifier.Shift;
212 else if (str[i] == 'C')
213 modifiers |= KeyModifier.Control;
214 else if (str[i] == 'A')
215 modifiers |= KeyModifier.Alt;
216 else if (str[i] == 'G')
217 modifiers |= KeyModifier.AltGr;
218 else if (str[i] == 's')
219 modifiers |= KeyModifier.Super;
220 else if (str[i] == 'H')
221 modifiers |= KeyModifier.Hyper;
224 key = combine_modifiers (str[i], modifiers);
226 key = combine_modifiers (decode_keysym (keysym), modifiers);
229 public Key (MPlist plist)
231 KeyModifier modifiers = KeyModifier.None;
234 for (p = plist; ! p.IsEmpty; p = p.next)
238 if (! p.next.IsEmpty)
239 throw new Exception ("Invalid Key: " + plist);
242 else if (! p.IsSymbol)
243 throw new Exception ("Invalid Key: " + plist);
246 string name = p.Symbol.Name.ToLower ();
249 if (! keymodifiers.TryGetValue (name, out m))
254 if (p.IsEmpty || ! p.next.IsEmpty)
255 throw new Exception ("Invalid Key: " + plist);
257 key = combine_modifiers ((uint) p.Integer, modifiers);
259 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
262 public bool HasModifier
264 get { return ((key & (uint) KeyModifier.All) != 0); }
267 public bool Match (Key k)
271 if ((k.key & char_mask) != (key & char_mask))
273 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
274 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
275 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
276 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
277 && (m2 & KeyModifier.Shift) != KeyModifier.None))
278 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
279 || ((m1 & KeyModifier.Control) == KeyModifier.Control
280 && (m2 & KeyModifier.Control) != KeyModifier.None))
281 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
282 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
283 && (m2 & KeyModifier.Alt) != KeyModifier.None))
284 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
287 public override string ToString ()
289 string str = Char.ToString ((char) key);
290 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
292 if (m != KeyModifier.None)
294 if ((m & KeyModifier.Shift) != KeyModifier.None)
296 if ((m & KeyModifier.Control) != KeyModifier.None)
298 if ((m & KeyModifier.Alt) != KeyModifier.None)
300 if ((m & KeyModifier.AltGr) != KeyModifier.None)
302 if ((m & KeyModifier.Super) != KeyModifier.None)
304 if ((m & KeyModifier.Hyper) != KeyModifier.None)
311 public class KeySeq : Xex.TermValue
313 List<Key> keyseq = new List<Key> ();
317 public KeySeq (MPlist plist)
319 foreach (MPlist p in plist)
322 keyseq.Add (new Key (p.Symbol));
323 else if (p.IsInteger)
324 keyseq.Add (new Key ((char) p.Integer));
326 keyseq.Add (new Key (p.Plist));
328 throw new Exception ("Invalid Key Sequence: " + plist);
332 public KeySeq (MText mt) : base ()
334 for (int i = 0; i < mt.Length; i++)
335 keyseq.Add (new Key ((uint) mt[i]));
338 private static uint parse_integer (string str)
340 if (Char.IsDigit (str[0]))
342 if (str[0] == '0' && str.Length > 2 && str[1] == 'x')
345 for (int idx = 2; idx < str.Length; idx++)
348 if (c >= '0' && c <= '9')
349 i = i * 16 + (c - '0');
350 else if (c >= 'A' && c <= 'F')
351 i = i * 16 + 10 + (c - 'A');
352 else if (c >= 'a' && c <= 'f')
353 i = i * 16 + 10 + (c - 'a');
359 return UInt32.Parse (str);
361 else if (str[0] == '?')
366 public KeySeq (XmlNode node)
368 XmlAttributeCollection acol = node.Attributes;
371 if (acol != null && (n = acol["keys"]) != null)
373 foreach (char c in n.Value)
374 keyseq.Add (new Key ((uint) c));
377 for (node = node.FirstChild; node != null; node = node.NextSibling)
379 if (node.Name == "key-event")
380 keyseq.Add (new Key ((MSymbol) node.InnerText));
382 keyseq.Add (new Key (parse_integer (node.InnerText)));
386 public KeySeq (Xex.Term[] args)
388 foreach (Xex.Term term in args)
391 keyseq.Add (new Key ((MSymbol) term.Nameval.name));
393 keyseq.Add (new Key (term.Strval));
397 public override string ToString ()
401 foreach (Key key in this)
405 foreach (Key k in this)
406 str += " " + k.ToString ();
410 foreach (Key key in this)
411 str += key.ToString ();
416 public class Variable
419 public MText description;
422 public object[] candidates;
424 public Variable (MPlist p)
428 description = parse_description (p);
429 if (description == null)
430 description = new MText ("No description");
433 type = (p.IsMText ? typeof (MText)
434 : p.IsInteger ? typeof (int)
435 : p.IsSymbol ? typeof (MSymbol)
439 candidates = new object[p.Count];
440 for (int i = 0; ! p.IsEmpty; i++, p = p.next)
441 candidates[i] = p.val;
444 private static Type parse_value (XmlNode node, out object value)
446 string typename = node.Attributes["type"].Value;
449 if (typename == "integer")
452 if (! Int32.TryParse (node.InnerText, out i))
457 else if (typename == "string")
459 MText mt = node.InnerText;
461 type = typeof (MText);
463 else if (typename == "symbol")
465 MSymbol sym = node.InnerText;
467 type = typeof (MSymbol);
472 type = typeof (object);
477 public Variable (XmlNode node)
479 name = node.Attributes["id"].Value;
480 for (node = node.FirstChild; node != null; node = node.NextSibling)
481 if (node.NodeType == XmlNodeType.Element)
483 if (node.Name == "description")
484 description = parse_description (node);
485 else if (node.Name == "value")
486 type = parse_value (node, out value);
487 else if (node.Name == "valiable-value-candidate")
489 XmlNodeList n_list = node.ChildNodes;
490 candidates = new object[n_list.Count];
491 for (int i = 0; i < n_list.Count; i++)
494 parse_value (n_list[i], out val);
501 public override string ToString ()
503 return ("(" + name + " \"" + (string) description
504 + "\" " + type + " " + value + " " + candidates + ")");
511 public MText description;
512 public List<KeySeq> keys;
514 public Command (MPlist p)
518 description = parse_description (p);
519 if (description == null)
520 description = "No description";
521 keys = new List<KeySeq> ();
522 for (p = p.next; ! p.IsEmpty; p = p.next)
525 keys.Add (new KeySeq (p.Text));
527 keys.Add (new KeySeq (p.Plist));
531 public Command (XmlNode node)
533 name = node.Attributes["id"].Value;
534 keys = new List<KeySeq> ();
535 for (node = node.FirstChild; node != null; node = node.NextSibling)
537 if (node.Name == "description")
538 description = parse_description (node);
539 else if (node.Name == "keyseq")
540 keys.Add (new KeySeq (node));
544 public override string ToString ()
546 string str = "(" + name + " \"" + (string) description;
547 foreach (KeySeq keyseq in keys)
553 internal class Plugin
556 public Assembly assembly;
557 public MPlist methods;
559 public override string ToString ()
561 string str = "(" + name;
562 for (MPlist p = methods; ! p.IsEmpty; p = p.next)
567 public Xex.Term Call (MSymbol method, MInputContext ic, Xex.Term[] args)
569 if (assembly == null)
572 assembly = Assembly.LoadFrom (name + ".dll");
576 Type t = assembly.GetType ("Plugin");
577 for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
578 p.Set (p.key, t.GetMethod (p.key.Name));
581 MethodInfo method_info = (MethodInfo) methods.Get (method);
582 if (method_info == null)
584 Xex.Term result = (Xex.Term) method_info.Invoke (null, args);
585 return result.Eval ();
589 internal class PluginMethod : Xex.Function
591 public Plugin plugin;
592 public MSymbol method;
594 public PluginMethod (Plugin plugin, MSymbol name)
595 : base ((Name) name.name, 0, -1)
597 this.plugin = plugin;
598 method = name.name.Substring (plugin.name.Length + 1);
601 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
604 args = (Xex.Term[]) args.Clone ();
605 for (int i = 0; i < args.Length; i++)
607 args[i] = args[i].Eval (domain);
611 return plugin.Call (method, (MIntutContext) domain.context, args);
615 internal class PluginCall : Xex.TermValue
620 public PluginCall (Plugin plugin, MSymbol entry, Xex.Term[] args)
622 this.plugin = plugin;
627 public override Xex.Term Eval (Xex.Domain domain)
629 Xex.Term[] args = new Xex.Term[this.args.Length];
630 for (int i = 0; i < args.Length; i++)
631 args[i] = this.args[i].Eval (domain);
632 return plugin.Call (entry, args);
636 internal abstract class Marker : Xex.TermValue
640 public Marker (MSymbol name)
645 public abstract int Position (MInputContext ic);
646 public abstract void Mark (MInputContext ic);
648 public class Named : Marker
652 public Named (MSymbol name) : base (name) { }
654 public override int Position (MInputContext ic) { return pos; }
656 public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
659 public class Predefined : Marker
661 public Predefined (MSymbol name) : base (name) { }
663 public override int Position (MInputContext ic)
665 switch (name.Name[1]) {
667 case '>': return ic.preedit.Length;
668 case '-': return ic.cursor_pos - 1;
669 case '+': return ic.cursor_pos + 1;
671 if (ic.cursor_pos > 0)
673 int pos = ic.cursor_os;
675 ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
681 if (ic.cursor_pos < ic.preedit.Length - 1)
683 int pos = ic.cursor_pos;
685 ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
689 return ic.preedit.Length;
691 return name.Name[1] - '0';
695 public override void Mark (MInputContext ic)
697 throw new Exception ("Can't set predefined marker: " + name);
701 static internal Dictionary<MSymbol,Predefined> predefined_markers;
705 predefined_markers = new Dictionary<MSymbol,Predefined> ();
706 MSymbol[] symlist = new MSYmbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
707 "@0", "@1", "@2", "@3", "@4",
708 "@5", "@6", "@7", "@8", "@9" };
709 foreach (MSymbol s in strline)
710 predefined_markers[s] = new Predefined (s);
713 public static Marker Get (MInputContext ic, MSymbol name)
717 if (predefined_markers.TryGetValue (name, out m))
719 if (name.Name[0] == '@')
720 throw new Exception ("Invalid marker name: " + name);
721 m = (Marker) ic->markers.Get (name);
724 m = new Named (name);
725 ic->markers.Put (name, m);
734 public Dictionary<Key, Map> submaps;
737 public void Add (KeySeq keys, int index, Xex actions)
742 submaps = new Dictionary<Key, Map> ();
744 submaps.TryGetValue (keys[index], out sub);
747 Key key = keys[index];
748 submaps[key] = sub = new Map ();
750 if (index + 1 < keys.Count)
751 sub.Add (keys, index + 1, actions);
753 this.actions = actions;
756 public Xex Lookup (KeySeq keys, int index)
760 if (index + 1 == keys.Count)
762 if (submaps.TryGetValue (keys[index], out sub))
763 return sub.Lookup (keys, index + 1);
767 private void describe (MText mt, KeySeq keyseq)
769 if (keyseq.Count > 0)
771 mt.Cat (" (").Cat (keyseq.ToString ());
773 mt.Cat (' ').Cat (actions.ToString ());
777 foreach (KeyValuePair<Key, Map> kv in submaps)
780 kv.Value.describe (mt, keyseq);
781 keyseq.RemoveAt (keyseq.Count - 1);
785 public override string ToString ()
787 MText mt = "(" + name.Name;
788 KeySeq keyseq = new KeySeq ();
790 describe (mt, keyseq);
800 public MPlist branches = new MPlist ();
802 public State (MSymbol name)
807 public override string ToString ()
809 MText mt = "(" + name.Name;
812 mt.Cat (" \"" + title + "\"");
813 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
814 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
815 return (string) mt + ")";
820 internal Xex.Domain domain;
822 private LoadStatus load_status = LoadStatus.None;
823 private MDatabase.Tag tag;
824 private MDatabase mdb;
826 private MText description;
827 internal MText title;
828 internal Command[] commands;
829 internal Xex.Name[] var_names;
830 internal Dictionary<MSymbol, Plugin> plugins;
831 internal Dictionary<MSymbol, Map> maps;
832 internal MPlist states;
834 static MInputMethod ()
836 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
837 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
838 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
839 im_domain.DefSubr (Fselect, "select", true, 1, 1);
840 im_domain.DefSubr (Fshow, "show", true, 0, 0);
841 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
842 im_domain.DefSubr (Fmove, "move", true, 1, 1);
843 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
844 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
845 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
846 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
847 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
848 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
849 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
850 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
851 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
852 im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
854 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
855 List<MDatabase> list = MDatabase.List (tag);
856 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
857 foreach (MDatabase mdb in list)
858 im_table[mdb.tag] = new MInputMethod (mdb.tag);
859 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil, "global");
860 im_global = im_table[tag];
864 private MInputMethod (MDatabase.Tag tag)
869 // Instance Properties
870 public MSymbol Language { get { return tag[1]; } }
871 public MSymbol Name { get { return tag[2]; } }
872 public MSymbol SubName { get { return tag[3]; } }
874 public bool Info (out MText description,
876 out Xex.Variable[] variables,
877 out Command[] commands)
879 if ((load_status & LoadStatus.Header) != LoadStatus.Header
888 description = this.description;
890 variables = new Xex.Variable[var_names.Length];
892 foreach (Xex.Name name in var_names)
893 variables[i++] = domain.GetVar (name, false);
894 commands = this.commands;
898 public static MInputMethod Find (MSymbol language, MSymbol name)
900 return Find (language, name, MSymbol.nil);
903 public static MInputMethod Find (MSymbol language, MSymbol name,
906 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
910 return (im_table.TryGetValue (tag, out im) ? im : null);
915 return ((load_status == LoadStatus.Full) || load_body ());
918 public static MInputMethod[] List ()
920 MInputMethod[] array = new MInputMethod[im_table.Count];
923 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
924 array[i++] = kv.Value;
928 private bool load_header ()
930 mdb = MDatabase.Find (tag);
934 MSymbol format = mdb.Format;
936 if (format == MSymbol.plist)
937 load ((MPlist) mdb.Load (Mmap), false);
940 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
941 load (doc.DocumentElement, false);
943 } catch (Exception e) {
944 Console.WriteLine ("{0}\n", e);
945 load_status = LoadStatus.Error;
948 load_status |= LoadStatus.Header;
952 private bool load_body ()
954 domain = new Xex.Domain (domain, null);
955 mdb = MDatabase.Find (tag);
959 object obj = mdb.Load ();
961 load ((MPlist) obj, true);
963 load ((XmlDocument) obj, true);
964 } catch (Exception e) {
965 Console.WriteLine (e);
966 load_status = LoadStatus.Error;
969 load_status = LoadStatus.Full;
973 private void load (MPlist plist, bool full)
975 maps = new Dictionary<MSymbol, Map> ();
976 states = new MPlist ();
978 for (; ! plist.IsEmpty; plist = plist.next)
981 MPlist pl = plist.Plist;
984 MSymbol sym = pl.Symbol;
987 if (sym == Mdescription)
989 description = parse_description (pl);
990 if (description == null)
991 description = new MText ("No description");
993 else if (sym == Mtitle)
998 else if (sym == Mvariable)
999 parse_variables (pl);
1000 else if (sym == Mcommand)
1001 parse_commands (pl);
1006 else if (sym == Minclude)
1008 else if (sym == Mmacro)
1010 else if (sym == Mmap)
1012 else if (sym == Mstate)
1017 if (description == null)
1018 description = (MText) "No description";
1020 title = new MText (tag[2].Name);
1021 if (commands == null)
1022 commands = new Command[0];
1027 State state = new State ((MSymbol) "init");
1028 plist = new MPlist ();
1029 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1030 state.branches.Add (kv.Key, null);
1031 states.Add (state.name, state);
1035 private void load (XmlNode node, bool full)
1037 bool skip_header = load_status == LoadStatus.Header;
1039 maps = new Dictionary<MSymbol, Map> ();
1040 states = new MPlist ();
1042 if (node.NodeType == XmlNodeType.Document)
1043 node = node.FirstChild;
1044 while (node.NodeType != XmlNodeType.Element)
1045 node = node.NextSibling;
1046 for (node = node.FirstChild; node != null; node = node.NextSibling)
1048 if (node.NodeType != XmlNodeType.Element)
1052 if (node.Name == "description")
1053 description = parse_description (node);
1054 else if (node.Name == "title")
1055 title = parse_title (node);
1056 else if (node.Name == "variable-list")
1057 parse_variables (node);
1058 else if (node.Name == "command-list")
1059 parse_commands (node);
1063 if (node.Name == "module-list")
1064 parse_plugins (node);
1065 else if (node.Name == "macro-list")
1066 parse_macros (node);
1067 else if (node.Name == "map-list")
1069 else if (node.Name == "state-list")
1070 parse_states (node);
1073 if (description == null)
1074 description = (MText) "No description";
1076 title = new MText (tag[2].Name);
1077 if (commands == null)
1078 commands = new Command[0];
1083 State state = new State ((MSymbol) "init");
1084 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1085 state.branches.Add (kv.Key, null);
1086 states.Add (state.name, state);
1090 private static void transform (MPlist plist)
1092 for (; ! plist.IsEmpty; plist = plist.next)
1096 MPlist p = new MPlist ();
1097 p.Add (MSymbol.symbol, Minsert);
1098 p.Add (MSymbol.mtext, plist.Text);
1099 plist.Set (MSymbol.plist, p);
1101 else if (plist.IsInteger)
1103 MPlist p = new MPlist ();
1104 p.Add (MSymbol.symbol, Minsert);
1105 p.Add (MSymbol.integer, plist.Integer);
1106 plist.Set (MSymbol.plist, p);
1108 else if (plist.IsPlist)
1110 MPlist pl = plist.Plist;
1114 if (pl.Symbol == Madd)
1115 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1116 else if (pl.Symbol == Msub)
1117 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1118 else if (pl.Symbol == Mmul)
1119 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1120 else if (pl.Symbol == Mdiv)
1121 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1122 else if (pl.Symbol == Minsert)
1124 // (insert (CANDIDATES ...))
1125 // => (candidates CANDIDATES ...)
1126 if (pl.next.IsPlist)
1128 pl.Set (MSymbol.symbol, Mcandidates);
1130 MPlist p = pl.Plist;
1131 pl.Set (p.key, p.val);
1132 for (p = p.next; ! p.IsEmpty; p = p.next);
1133 pl.Add (p.key, p.val);
1136 else if (pl.Symbol == Mif)
1140 transform (pl.next);
1142 else if (pl.Symbol == Mcond)
1144 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1147 MPlist p = pl.Plist;
1155 else if (pl.Symbol == Mdelete
1156 || pl.Symbol == Mmove
1157 || pl.Symbol == Mmark)
1162 MSymbol sym = pl.Symbol;
1163 MPlist p = new MPlist ();
1164 p.Add (MSymbol.symbol, Mmarker);
1165 p.Add (MSymbol.symbol, sym);
1166 pl.Set (MSymbol.plist, p);
1169 else if (pl.Symbol == Mpushback)
1173 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1176 else if (pl.IsMText)
1178 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1179 pl.Push (MSymbol.symbol, Mcandidates);
1182 else if (plist.IsSymbol)
1184 MSymbol sym = plist.Symbol;
1186 if (sym.Name.Length >= 3
1187 && sym.Name[0] == '@'
1188 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1190 int pos = int.Parse (sym.Name.Substring (1));
1191 MPlist p = new MPlist ();
1195 p.Add (MSymbol.symbol, Msurrounding_text_p);
1199 if (sym.Name[1] == '+')
1201 p.Add (MSymbol.symbol, Mchar_at);
1202 p.Add (MSymbol.integer, pos);
1204 plist.Set (MSymbol.plist, p);
1210 private static MText parse_description (MPlist plist)
1216 plist = plist.Plist;
1217 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1218 && plist.next.IsMText)
1219 return plist.next.Text;
1224 private static MText parse_description (XmlNode node)
1226 if (node.HasChildNodes)
1227 node = node.FirstChild;
1228 return node.InnerText;
1231 private static MText parse_title (XmlNode node)
1233 return node.InnerText;
1236 private void new_variable (Xex.Name name, string desc, int val,
1237 MPlist pl, Xex.Variable vari)
1245 range = new int[pl.Count * 2];
1246 for (int i = 0; i < range.Length; i++)
1250 MPlist p = pl.Plist;
1252 if (! p.IsInteger || ! p.next.IsInteger)
1253 throw new Exception ("Invalid range: " + p);
1254 range[i * 2] = p.Integer;
1255 range[i * 2 + 1] = p.next.Integer;
1257 else if (pl.IsInteger)
1258 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1260 throw new Exception ("Invalid range: " + pl);
1264 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1267 Xex.Term term = new Xex.Term (val);
1269 vari.DefaultValue = term;
1274 private void new_variable (Xex.Name name, string desc, MText val,
1275 MPlist pl, Xex.Variable vari)
1283 range = new string[pl.Count * 2];
1284 for (int i = 0; i < range.Length; i++)
1287 range[i] = (string) pl.Text;
1289 throw new Exception ("Invalid range: " + pl);
1293 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1296 Xex.Term term = new Xex.Term ((string) val);
1298 vari.DefaultValue = term;
1303 private void new_variable (Xex.Name name, string desc, MSymbol val,
1304 MPlist pl, Xex.Variable vari)
1307 Xex.Name sym = val.Name;
1313 range = new Xex.Name[pl.Count * 2];
1314 for (int i = 0; i < range.Length; i++)
1317 range[i] = pl.Symbol.Name;
1319 throw new Exception ("Invalid range: " + pl);
1323 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1326 Xex.Term term = new Xex.Term (sym);
1328 vari.DefaultValue = term;
1333 private void parse_variables (MPlist plist)
1335 var_names = new Xex.Name[plist.Count];
1337 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1339 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1340 throw new Exception ("Invalid variable: " + plist);
1342 MPlist p = plist.Plist;
1343 Xex.Name name = (Xex.Name) p.Symbol.Name;
1344 var_names[i] = name;
1346 string desc = (string) parse_description (p);
1347 Xex.Variable vari = im_global.domain.GetVar (name, false);
1350 domain.Defvar (vari);
1356 new_variable (name, desc, p.Integer, p.next, vari);
1358 new_variable (name, desc, p.Text, p.next, vari);
1359 else if (p.IsSymbol)
1360 new_variable (name, desc, p.Symbol, p.next, vari);
1362 throw new Exception ("Invalid variable type: " + p.val);
1367 private void parse_variables (XmlNode node)
1369 XmlNodeList node_list = node.ChildNodes;
1371 var_names = new Xex.Name[node_list.Count];
1372 for (int i = 0; i < node_list.Count; i++)
1374 Xex.Name name = node_list[i].Attributes[0].Value;
1375 Xex.Variable vari = im_global.domain.GetVar (name, false);
1378 domain.Defvar (vari);
1380 domain.Defvar (node_list[i]);
1382 var_names[i] = name;
1386 private void parse_commands (MPlist plist)
1388 commands = new Command[plist.Count];
1390 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1391 if (plist.IsPlist && plist.Plist.IsSymbol)
1392 commands[i++] = new Command (plist.Plist);
1395 private void parse_commands (XmlNode node)
1397 XmlNodeList node_list = node.ChildNodes;
1399 commands = new Command[node_list.Count];
1400 for (int i = 0; i < node_list.Count; i++)
1402 if (node_list[i].NodeType == XmlNodeType.Element)
1403 commands[i] = new Command (node_list[i]);
1407 private void parse_plugins (MPlist plist)
1409 plugins = new Dictionary<MSymbol, Plugin> ();
1411 for (; ! plist.IsEmpty; plist = plist.Next)
1413 MPlist p = plist.Plist;
1414 MSymbol sym = p.Symbol;
1415 Plugin plugin = new Plugin ();
1417 plugin.name = sym.Name;
1418 plugin.methods = new MPlist ();
1419 for (p = p.next; ! p.IsEmpty; p = p.next)
1420 plugin.methods.Add (p.Symbol, null);
1421 plugins.Add (sym, plugin);
1425 private void parse_plugins (XmlNode node)
1427 plugins = new Dictionary<MSymbol, Plugin> ();
1429 foreach (XmlNode n in node.ChildNodes)
1431 Plugin plugin = new Plugin ();
1432 plugin.name = n.Attributes["id"].Value;
1433 plugin.methods = new MPlist ();
1434 foreach (XmlNode nn in n.ChildNodes)
1435 plugin.methods.Add ((MSymbol) nn.Attributes["id"].Value,
1437 plugins.Add (plugin.name, plugin);
1441 private void parse_macros (XmlNode node)
1443 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1444 if (nn.NodeType == XmlNodeType.Element)
1445 domain.Defun ((MSymbol) node.GetAttribute ("id"));
1446 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1447 if (nn.NodeType == XmlNodeType.Element)
1448 domain.Defun ((MSymbol) node.GetAttribute ("id"), null,
1452 private void parse_maps (XmlNode node)
1456 private void parse_states (XmlNode node)
1460 private void parse_include (MPlist plist)
1462 if (! plist.IsPlist)
1464 MPlist p = plist.Plist;
1465 MSymbol language, name, subname;
1466 language = p.Symbol;
1469 name = subname = MSymbol.nil;
1475 subname = MSymbol.nil;
1480 MInputMethod im = MInputMethod.Find (language, name, subname);
1486 if (! plist.IsSymbol)
1488 MSymbol target_type = plist.Symbol;
1490 MSymbol target_name = MSymbol.nil;
1492 target_name = plist.Symbol;
1493 if (target_type == Mmacro)
1495 if (target_name == MSymbol.nil)
1496 im.domain.CopyFunc (domain);
1498 im.domain.CopyFunc (domain, target_name);
1500 else if (target_type == Mmap)
1502 if (target_name == MSymbol.nil)
1504 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1505 maps[kv.Key] = kv.Value;
1510 if (im.maps.TryGetValue (target_name, out map))
1511 maps[target_name] = map;
1514 else if (target_type == Mstate)
1516 if (target_name == MSymbol.nil)
1518 for (p = im.states; ! p.IsEmpty; p = p.next)
1519 states.Add (p.key, p.val);
1523 object state = im.states.Get (target_name);
1525 states.Add (target_name, state);
1530 private void parse_macros (MPlist plist)
1532 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1535 MPlist p = pl.Plist;
1539 domain.Defun (p.Symbol, null, null);
1541 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1544 MPlist p = pl.Plist;
1549 domain.Defun (p.Symbol, null, p.next);
1553 private void parse_maps (MPlist plist)
1555 for (; ! plist.IsEmpty; plist = plist.next)
1558 MPlist pl = plist.Plist;
1562 Map map = new Map ();
1563 map.name = pl.Symbol;
1564 maps[map.name] = map;
1565 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1569 MPlist p = pl.Plist;
1572 keys = new KeySeq (p.Text);
1574 keys = new KeySeq (p.Plist);
1581 MXex expr = new MXex (p, domain);
1582 map.Add (keys, 0, expr);
1587 private void parse_states (MPlist plist)
1589 for (; ! plist.IsEmpty; plist = plist.next)
1592 MPlist pl = plist.Plist;
1603 State state = new State (pl.Symbol);
1604 state.title = title;
1606 states = new MPlist ();
1607 states.Add (state.name, state);
1608 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1612 MPlist p = pl.Plist;
1615 MSymbol map_name = p.Symbol;
1618 state.branches.Add (map_name,
1619 new MXex (p, domain));
1624 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1627 ((MInputContext) domain.context).insert (args[0]);
1631 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1635 ((MInputContext) domain.context).insert_candidates (args[0]);
1639 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1642 MSymbol name = (string) args[0].Nameval;
1643 return Marker.Get (name);
1646 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1649 return ((MInputContext) domain.context).char_at (args[0].Intval);
1652 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1655 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1659 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1662 MInputContext ic = (MInputContext) domain.context;
1665 ic.select (args[0].Intval);
1667 ic.select ((MSYmbol) ((string) args[0].Nameval));
1671 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1674 ((MInputContext) domain.context).show ();
1678 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1681 ((MInputContext) domain.context).hide ();
1685 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1689 ((MInputContext) domain.context).move (args[0].Intval);
1692 Marker m = (Marker) args[0].Objval;
1693 MInputContext ic = (MInputContext) domain.context;
1694 ((MInputContext) domain.context).move (m.Position (ic));
1699 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1702 Marker m = (Marker) args[0].Objval;
1703 m.Mark ((MInputContext) domain.context);
1707 private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
1710 return new KeySeq (args);
1713 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1716 MInputContext ic = (MInputContext) domain.context;
1719 ic.pushback (args[0].Intval);
1720 else if (args[0].IsStr)
1721 ic.pushback (new KeySeq (args[0].Strval));
1723 ic.pushback ((KeySeq) args[0].Objval);
1727 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1730 ((MInputContext) domain.context).pop ();
1734 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1737 int n = args.Length == 0 ? -2 : args[0].Intval;
1738 ((MInputContext) domain.context).undo (n);
1742 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1745 ((MInputContext) domain.context).commit ();
1749 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1752 ((MInputContext) domain.context).commit ();
1753 args = new Xex.Term[2];
1754 args[0] = args[1] = catch_tag;
1755 return Xex.Fthrow (domain, vari, args);
1758 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1761 ((MInputContext) domain.context).shift (args[0].Symval);
1765 public override string ToString ()
1767 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1768 if (commands != null)
1770 str += " (commands";
1771 foreach (Command cmd in commands)
1775 if (variables != null)
1777 str += " (variables";
1778 foreach (Variable var in variables)
1782 if (plugins != null)
1785 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1786 str += " " + kv.Value;
1790 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1791 str += " " + kv.Value;
1793 foreach (MPlist p in states)
1799 public class MInputContext
1801 internal static MSymbol Mcandidates_group_size = "candidates-group-size";
1802 private static MSymbol Mat_less_than = "@<";
1803 private static MSymbol Mat_greater_than = "@>";
1804 private static MSymbol Mat_minus = "@-";
1805 private static MSymbol Mat_plus = "@+";
1806 private static MSymbol Mat_open_square_bracket = "@[";
1807 private static MSymbol Mat_close_square_bracket = "@]";
1809 public MInputMethod im;
1810 private MText produced;
1811 private bool active;
1812 private MText status;
1813 private bool status_changed;
1814 private MText preedit;
1815 private bool preedit_changed;
1816 private int cursor_pos;
1817 private bool cursor_pos_changed;
1818 private Candidates candidates;
1819 private MPlist candidate_group;
1820 private int candidate_index;
1821 private int candidate_from, candidate_to;
1822 private bool candidate_show;
1823 private bool candidate_changed;
1825 private Stack<MInputMethod.State> states;
1826 internal MInputMethod.KeySeq keys;
1827 private int key_head;
1828 private int state_key_head;
1829 private MPlist state_boundary;
1830 private int commit_key_head;
1831 private MText state_preedit;
1832 private int state_pos;
1833 internal MPlist markers = new MPlist ();
1834 private MPlist vars;
1835 private MPlist vars_saved;
1836 internal MText preceding_text = new MText ();
1837 internal MText following_text = new MText ();
1838 private bool key_unhandled;
1840 internal Xex.Domain domain;
1842 public MInputContext (MInputMethod im)
1845 domain = new Xex.Domain (im.domain, this);
1846 states = new Stack<MInputMethod.State> ();
1847 states.Push ((MInputMethod.State) im.states.val);
1848 keys = new MInputMethod.KeySeq ();
1851 private void adjust_markers (int from, int to, object inserted)
1853 int ins = (inserted == null ? 0
1854 : inserted is int ? 1
1855 : ((MText) inserted).Length);
1856 int diff = ins - (to - from);
1858 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1860 int pos = plist.Integer;
1864 plist.val = pos + diff;
1869 if (cursor_pos >= to)
1871 else if (cursor_pos > from)
1875 private void preedit_replace (int from, int to, int c)
1877 preedit.Del (from, to);
1878 preedit.Ins (from, c);
1879 adjust_markers (from, to, c);
1882 private void preedit_replace (int from, int to, MText mt)
1884 preedit[from, to] = mt;
1885 adjust_markers (from, to, mt);
1888 internal void insert (Xex.Term arg)
1891 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1893 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1894 preedit_changed = true;
1895 cursor_pos_changed = true;
1898 private class Candidates
1905 public Block (int index, MPlist plist)
1916 get { return (Data is MText
1917 ? ((MText) Data).Length
1918 : ((MPlist) Data).Count); }
1921 public object this[int i]
1924 if (Data is MText) return ((MText) Data)[i];
1925 return ((MPlist) Data)[i];
1930 private Block[] blocks;
1931 private int row = 0;
1932 private int index = 0;
1933 public object[] group;
1935 private bool IsFixed { get { return group != null; } }
1938 Block last = blocks[blocks.Length - 1];
1939 return last.Index + last.Count; }
1943 get { return (IsFixed ? index % group.Length
1944 : index - blocks[row].Index); }
1947 public object Group {
1948 get { return (IsFixed ? group : blocks[row].Data); }
1951 public int GroupLength
1956 int nitems = group.Length;
1957 int start = index - (index % nitems);
1959 return (start + nitems <= total ? nitems : total - start);
1961 return blocks[row].Count;
1965 public object Current {
1967 return (IsFixed ? group[index % group.Length]
1968 : blocks[row][index - blocks[row].Index]);
1972 public Candidates (MPlist list, int column)
1974 int nblocks = list.Count;
1976 blocks = new Block[nblocks];
1977 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
1978 start += (blocks[i] = new Block (index, list)).Count;
1980 group = new object[column];
1983 public Candidates (List<Xex.Term> list, int column)
1985 int nblocks = list.Count;
1987 blocks = new Block[nblocks];
1988 for (int i = 0, start = 0; i < nblocks; i++)
1989 start += (blocks[i] = new Block (index, list[i])).Count;
1991 group = new object[column];
1994 public static void Detach (MInputContext ic)
1996 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
1997 ic.candidates = null;
1998 ic.preedit_changed = true;
1999 ic.cursor_pos_changed = true;
2000 ic.candidate_changed = true;
2003 // Fill the array "group" by candidates stating from INDEX.
2004 // INDEX must be a multiple of "column". Set NTIMES to the
2005 // number of valid candidates in "group". Update "block" if
2006 // necessary. Return "group".
2008 private int fill_group (int start)
2010 int nitems = group.Length;
2012 Block b = blocks[r];
2014 if (start < b.Index)
2015 while (start < b.Index)
2018 while (start >= b.Index + b.Count)
2022 int count = b.Count;
2024 for (int i = 0; i < nitems; i++, start++)
2029 if (r == blocks.Length)
2035 group[i] = b[start];
2040 // Update "row" to what contains the first candidate of
2041 // the previous candidate-group, update "current_index", and
2042 // update "group" if necessary. Return the previous
2043 // candidate-group. Set NITEMS to the number of valid
2044 // candidates contained in that group.
2046 public int PrevGroup ()
2053 nitems = group.Length;
2054 if ((index -= col + nitems) < 0)
2055 index = (Total / nitems) * nitems;
2056 nitems = fill_group (index);
2060 row = row > 0 ? row-- : blocks.Length - 1;
2061 nitems = blocks[row].Count;
2062 index = blocks[row].Index;
2064 index += col < nitems ? col : nitems - 1;
2068 public int NextGroup ()
2075 nitems = group.Length;
2076 if ((index += nitems - col) >= Total)
2078 nitems = fill_group (index);
2082 row = row < blocks.Length - 1 ? row + 1 : 0;
2083 nitems = blocks[row].Count;
2084 index = blocks[row].Count;
2086 index += col < nitems ? col : nitems - 1;
2096 int nitems = PrevGroup ();
2097 index += col < nitems - 1 ? col : nitems - 1;
2106 int nitems = GroupLength;
2108 if (col == nitems - 1)
2110 nitems = NextGroup ();
2117 public void First ()
2124 index += GroupLength - (Column + 1);
2127 public void Select (int col)
2129 int maxcol = GroupLength - 1;
2132 index = index - Column + col;
2136 private void update_candidate ()
2138 object candidate = candidates.Current;
2140 if (candidate is MText)
2142 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2143 candidate_to = candidate_from + ((MText) candidate).Length;
2147 preedit_replace (candidate_from, candidate_to, (int) candidate);
2148 candidate_to = candidate_from + 1;
2150 preedit.PushProp (candidate_from, candidate_to,
2151 MInputMethod.Mcandidates, this);
2152 cursor_pos = candidate_from;
2153 preedit_changed = true;
2154 cursor_pos_changed = true;
2155 candidate_changed = true;
2158 internal void insert_candidates (Xex.Term arg)
2162 if (domain.IsBound (Mcandidates_group_size))
2164 object val = domain.GetValue (Mcandidates_group_size);
2168 candidates = new Candidates (arg.Listval, column);
2169 candidate_from = candidate_to = cursor_pos;
2170 update_candidate ();
2173 internal void select (int n)
2175 if (candidates != null)
2177 candidates.Select (n);
2178 update_candidate ();
2182 internal void select (MSymbol sym)
2184 if (candidates != null)
2186 if (sym == Mat_less_than)
2187 candidates.First ();
2188 else if (sym == Mat_greater_than)
2190 else if (sym == Mat_minus)
2192 else if (sym == Mat_plus)
2194 else if (sym == Mat_open_square_bracket)
2195 candidates.PrevGroup ();
2196 else if (sym == Mat_close_square_bracket)
2197 candidates.NextGroup ();
2201 internal int marker (MSymbol sym)
2203 int pos = cursor_pos;
2205 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2207 switch (sym.Name[0])
2209 case '<': pos = 0; break;
2210 case '>': pos = preedit.Length; break;
2211 case '-': pos = cursor_pos - 1; break;
2212 case '+': pos = cursor_pos + 1; break;
2217 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2224 if (cursor_pos < preedit.Length - 1)
2227 preedit.FindProp (MInputMethod.Mcandidates, pos,
2231 pos = preedit.Length;
2234 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2239 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2241 pos = int.Parse (sym.Name.Substring (2));
2245 object val = markers.Get (sym);
2253 internal int char_at (int pos)
2260 if (preceding_text.Length < -pos)
2262 MPlist plist = new MPlist ();
2263 plist.Push (MSymbol.integer, pos);
2264 if (MInputMethod.GetSurroundingText != null
2265 && MInputMethod.GetSurroundingText (this, plist)
2267 && preceding_text.Length < plist.Text.Length)
2268 preceding_text = plist.Text;
2270 c = (-pos < preceding_text.Length
2271 ? preceding_text[preceding_text.Length + pos] : -1);
2273 else if (pos >= 0 && pos < preedit.Length)
2277 pos -= preedit.Length;
2278 if (pos >= following_text.Length)
2280 MPlist plist = new MPlist ();
2281 plist.Push (MSymbol.integer, pos + 1);
2282 if (MInputMethod.GetSurroundingText != null
2283 && MInputMethod.GetSurroundingText (this, plist)
2285 && following_text.Length < plist.Text.Length)
2286 following_text = plist.Text;
2288 c = (pos < following_text.Length ? following_text[pos] : -1);
2293 internal void delete (int pos)
2295 if (pos < cursor_pos)
2296 preedit_replace (pos, cursor_pos, null);
2298 preedit_replace (cursor_pos, pos, null);
2299 preedit_changed = true;
2300 cursor_pos_changed = true;
2303 internal void show ()
2305 candidate_show = true;
2306 candidate_changed = true;
2309 internal void hide ()
2311 candidate_show = false;
2312 candidate_changed = true;
2315 internal void move (int pos)
2319 else if (pos > preedit.Length)
2320 pos = preedit.Length;
2321 if (pos != cursor_pos)
2324 preedit_changed = true;
2328 internal void mark (MSymbol sym)
2330 MPlist slot = markers.Find (sym);
2333 markers.Push (sym, cursor_pos);
2335 slot.val = cursor_pos;
2338 internal void pushback (int n)
2351 if (key_head > keys.Count)
2352 key_head = keys.Count;
2356 internal void pushback (MInputMethod.KeySeq keyseq)
2360 if (key_head < keys.Count)
2361 keys.RemoveRange (key_head, keys.Count - key_head);
2362 for (int i = 0; i < keyseq.Count; i++)
2363 keys.Add (keyseq[i]);
2366 internal void pop ()
2368 if (key_head < keys.Count)
2369 keys.RemoveRange (key_head, 1);
2372 internal void undo (int n)
2375 keys.RemoveRange (keys.Count + n, - n);
2377 keys.RemoveRange (n, keys.Count - n);
2381 internal void commit ()
2383 produced.Cat (preedit);
2385 preedit_changed = true;
2388 internal void shift (MSymbol sym)
2390 MInputMethod.State state;
2392 if (sym == MSymbol.t)
2394 if (states.Count > 1)
2395 state = states.Pop ();
2397 state = states.Peek ();
2401 state = (MInputMethod.State) im.states.Get (sym);
2403 throw new Exception ("Unknown state: " + state.name);
2406 state = states.Pop ();
2407 if (state == (MInputMethod.State) im.states.val)
2414 state_key_head = key_head;
2415 state_pos = cursor_pos;
2416 state_preedit = preedit.Dup ();
2417 if (state != states.Peek ())
2419 states.Push (state);
2420 state_boundary = domain.SetBoundary ();
2421 status = state.title;
2424 status_changed = true;
2426 = (Xex) state.branches.Get (MSymbol.t);
2427 if (on_entry != null)
2428 on_entry.Eval (domain);
2433 internal bool call (MSymbol module, MSymbol method, MPlist arglist)
2435 MInputMethod.Plugin plugin;
2437 if (! im.plugins.TryGetValue (module, out plugin))
2439 if (plugin.assembly == null)
2444 assembly = Assembly.LoadFrom (module.Name + ".dll");
2448 Type t = assembly.GetType ("Plugin");
2449 for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
2450 p.Set (p.key, t.GetMethod (p.key.Name));
2453 MethodInfo method_info = (MethodInfo) plugin.methods.Get (method);
2454 if (method_info == null)
2456 object[] method_arg = new object[1];
2457 method_arg[0] = arglist;
2458 bool result = (bool) method_info.Invoke (null, method_arg);
2461 if (! arglist.IsEmpty)
2462 (new Xex (arglist, domain)).Eval (domain);
2466 internal void reset ()
2469 state_preedit.Del ();
2473 key_head = commit_key_head = 0;
2475 states.Push ((MInputMethod.State) im.states.Val);
2480 internal object GetCandidates (out int column)
2483 if (cursor_pos == 0)
2485 Candidates candidates
2486 = (Candidates) preedit.GetProp (cursor_pos - 1,
2487 MInputMethod.Mcandidates);
2488 if (candidates == null)
2490 column = candidates.Column;
2491 return candidates.Current;
2494 internal void HandleKey ()
2496 MInputMethod.State state = states.Peek ();