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 (Xex.Term[] args)
386 foreach (Xex.Term term in args)
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)
565 public Xex.Term Call (MSymbol method, MInputContext ic, Xex.Term[] args)
567 if (assembly == null)
570 assembly = Assembly.LoadFrom (name + ".dll");
574 Type t = assembly.GetType ("Plugin");
575 for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
576 p.Set (p.key, t.GetMethod (p.key.Name));
579 MethodInfo method_info = (MethodInfo) methods.Get (method);
580 if (method_info == null)
582 Xex.Term result = (Xex.Term) method_info.Invoke (null, args);
583 return result.Eval ();
587 internal class PluginMethod : Function
589 public Plugin plugin;
590 public MSymbol method;
592 public PluginMethod (Plugin plugin, MSymbol name)
593 : base ((Name) name.name, 0, -1)
595 this.plugin = plugin;
596 method = name.name.Substring (plugin.name.Length + 1);
599 public override Term Call (Xex.Domain domain, Xex.Variable vari,
602 Xex.Term[] args = (Xex.Term[]) args.Clone ();
603 for (int i = 0; i < args.Length; i++)
605 args[i] = args[i].Eval (domain);
609 return plugin.Call (method, (MIntutContext) domain.context, args);
613 internal class PluginCall : Xex.TermValue
618 public PluginCall (Plugin plugin, MSymbol entry, Xex.Term[] args)
620 this.plugin = plugin;
625 public override Xex.Term Eval (Xex.Domain domain)
627 Xex.Term[] args = new Xex.Term[this.args.Length];
628 for (int i = 0; i < args.Length; i++)
629 args[i] = this.args[i].Eval (domain);
630 return plugin.Call (entry, args);
634 internal abstract class Marker : Xex.TermValue
639 public Marker (MSymbol name, MInputContext ic)
645 public abstract int Position { get; set; }
648 internal class NamedMarker : Marker
652 public NamedMaker (MSymbol name, MInputContext ic) : base (name, ic)
654 ic.markers.Put (name, this);
657 public override int Position {
658 get { return position; }
659 set { position = value; }
663 internal class SystemMarker : Marker
665 public SystemMarker (MSymbol name, MInputContext ic) : base (name, ic)
669 public override int Position {
671 switch (name.Name[1])
674 case '>': return ic.preedit.Length;
675 case '-': return ic.cursor_pos - 1;
676 case '+': return ic.cursor_pos + 1;
678 if (ic.cursor_pos > 0)
680 int pos = ic.cursor_os;
682 ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
688 if (ic.cursor_pos < ic.preedit.Length - 1)
690 int pos = ic.cursor_pos;
692 ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
696 return ic.preedit.Length;
698 return name.Name[1] - '0';
702 throw new Exception ("Can't set predefined marker: " + name);
710 public Dictionary<Key, Map> submaps;
713 public void Add (KeySeq keys, int index, Xex actions)
718 submaps = new Dictionary<Key, Map> ();
720 submaps.TryGetValue (keys[index], out sub);
723 Key key = keys[index];
724 submaps[key] = sub = new Map ();
726 if (index + 1 < keys.Count)
727 sub.Add (keys, index + 1, actions);
729 this.actions = actions;
732 public Xex Lookup (KeySeq keys, int index)
736 if (index + 1 == keys.Count)
738 if (submaps.TryGetValue (keys[index], out sub))
739 return sub.Lookup (keys, index + 1);
743 private void describe (MText mt, KeySeq keyseq)
745 if (keyseq.Count > 0)
747 mt.Cat (" (").Cat (keyseq.ToString ());
749 mt.Cat (' ').Cat (actions.ToString ());
753 foreach (KeyValuePair<Key, Map> kv in submaps)
756 kv.Value.describe (mt, keyseq);
757 keyseq.RemoveAt (keyseq.Count - 1);
761 public override string ToString ()
763 MText mt = "(" + name.Name;
764 KeySeq keyseq = new KeySeq ();
766 describe (mt, keyseq);
776 public MPlist branches = new MPlist ();
778 public State (MSymbol name)
783 public override string ToString ()
785 MText mt = "(" + name.Name;
788 mt.Cat (" \"" + title + "\"");
789 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
790 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
791 return (string) mt + ")";
796 internal Xex.Domain domain;
798 private LoadStatus load_status = LoadStatus.None;
799 private MDatabase.Tag tag;
800 private MDatabase mdb;
802 private MText description;
803 internal MText title;
804 internal Command[] commands;
805 internal Xex.Name[] var_names;
806 internal Dictionary<MSymbol, Plugin> plugins;
807 internal Dictionary<MSymbol, Map> maps;
808 internal MPlist states;
810 static MInputMethod ()
812 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
813 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
814 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
815 im_domain.DefSubr (Fselect, "select", true, 1, 1);
816 im_domain.DefSubr (Fshow, "show", true, 0, 0);
817 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
818 im_domain.DefSubr (Fmove, "move", true, 1, 1);
819 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
820 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
821 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
822 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
823 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
824 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
825 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
826 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
827 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
828 im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
830 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
831 List<MDatabase> list = MDatabase.List (tag);
832 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
833 foreach (MDatabase mdb in list)
834 im_table[mdb.tag] = new MInputMethod (mdb.tag);
838 private MInputMethod (MDatabase.Tag tag)
843 // Instance Properties
844 public MSymbol Language { get { return tag[1]; } }
845 public MSymbol Name { get { return tag[2]; } }
846 public MSymbol SubName { get { return tag[3]; } }
848 public bool Info (out MText description,
850 out Xex.Variable[] variables,
851 out Command[] commands)
853 if ((load_status & LoadStatus.Header) != LoadStatus.Header
862 description = this.description;
864 variables = new Xex.Variable[var_names.Length];
866 foreach (Xex.Name name in var_names)
867 variables[i++] = domain.GetVar (name);
868 commands = this.commands;
872 public static MInputMethod Find (MSymbol language, MSymbol name)
874 return Find (language, name, MSymbol.nil);
877 public static MInputMethod Find (MSymbol language, MSymbol name,
880 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
884 return (im_table.TryGetValue (tag, out im) ? im : null);
889 return ((load_status == LoadStatus.Full) || load_body ());
892 public static MInputMethod[] List ()
894 MInputMethod[] array = new MInputMethod[im_table.Count];
897 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
898 array[i++] = kv.Value;
902 private bool load_header ()
904 mdb = MDatabase.Find (tag);
908 MSymbol format = mdb.Format;
910 if (format == MSymbol.plist)
911 load ((MPlist) mdb.Load (Mmap), false);
914 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
915 load (doc.DocumentElement, false);
917 } catch (Exception e) {
918 Console.WriteLine ("{0}\n", e);
919 load_status = LoadStatus.Error;
922 load_status |= LoadStatus.Header;
926 private bool load_body ()
928 domain = new Xex.Domain (domain, null);
929 mdb = MDatabase.Find (tag);
933 object obj = mdb.Load ();
935 load ((MPlist) obj, true);
937 load ((XmlDocument) obj, true);
938 } catch (Exception e) {
939 Console.WriteLine (e);
940 load_status = LoadStatus.Error;
943 load_status = LoadStatus.Full;
947 private void load (MPlist plist, bool full)
949 maps = new Dictionary<MSymbol, Map> ();
950 states = new MPlist ();
952 for (; ! plist.IsEmpty; plist = plist.next)
955 MPlist pl = plist.Plist;
958 MSymbol sym = pl.Symbol;
961 if (sym == Mdescription)
963 description = parse_description (pl);
964 if (description == null)
965 description = new MText ("No description");
967 else if (sym == Mtitle)
972 else if (sym == Mvariable)
973 parse_variables (pl);
974 else if (sym == Mcommand)
980 else if (sym == Minclude)
982 else if (sym == Mmacro)
984 else if (sym == Mmap)
986 else if (sym == Mstate)
991 if (description == null)
992 description = (MText) "No description";
994 title = new MText (tag[2].Name);
995 if (variables == null)
996 variables = new Variable[0];
997 if (commands == null)
998 commands = new Command[0];
1003 State state = new State ((MSymbol) "init");
1004 plist = new MPlist ();
1005 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1006 state.branches.Add (kv.Key, new Xex (plist, domain));
1007 states.Add (state.name, state);
1011 private void load (XmlNode node, bool full)
1013 bool skip_header = load_status == LoadStatus.Header;
1015 maps = new Dictionary<MSymbol, Map> ();
1016 states = new MPlist ();
1018 if (node.NodeType == XmlNodeType.Document)
1019 node = node.FirstChild;
1020 while (node.NodeType != XmlNodeType.Element)
1021 node = node.NextSibling;
1022 for (node = node.FirstChild; node != null; node = node.NextSibling)
1024 if (node.NodeType != XmlNodeType.Element)
1028 if (node.Name == "description")
1029 description = parse_description (node);
1030 else if (node.Name == "title")
1031 title = parse_title (node);
1032 else if (node.Name == "variable-list")
1033 parse_variables (node);
1034 else if (node.Name == "command-list")
1035 parse_commands (node);
1039 if (node.Name == "module-list")
1040 parse_plugins (node);
1041 else if (node.Name == "macro-list")
1042 parse_macros (node);
1043 else if (node.Name == "map-list")
1045 else if (node.Name == "state-list")
1046 parse_states (node);
1049 if (description == null)
1050 description = (MText) "No description";
1052 title = new MText (tag[2].Name);
1053 if (variables == null)
1054 variables = new Variable[0];
1055 if (commands == null)
1056 commands = new Command[0];
1061 State state = new State ((MSymbol) "init");
1062 MPlist plist = new MPlist ();
1063 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1064 state.branches.Add (kv.Key, new Xex (plist, domain));
1065 states.Add (state.name, state);
1069 private static void transform (MPlist plist)
1071 for (; ! plist.IsEmpty; plist = plist.next)
1075 MPlist p = new MPlist ();
1076 p.Add (MSymbol.symbol, Minsert);
1077 p.Add (MSymbol.mtext, plist.Text);
1078 plist.Set (MSymbol.plist, p);
1080 else if (plist.IsInteger)
1082 MPlist p = new MPlist ();
1083 p.Add (MSymbol.symbol, Minsert);
1084 p.Add (MSymbol.integer, plist.Integer);
1085 plist.Set (MSymbol.plist, p);
1087 else if (plist.IsPlist)
1089 MPlist pl = plist.Plist;
1093 if (pl.Symbol == Madd)
1094 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1095 else if (pl.Symbol == Msub)
1096 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1097 else if (pl.Symbol == Mmul)
1098 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1099 else if (pl.Symbol == Mdiv)
1100 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1101 else if (pl.Symbol == Minsert)
1103 // (insert (CANDIDATES ...))
1104 // => (candidates CANDIDATES ...)
1105 if (pl.next.IsPlist)
1107 pl.Set (MSymbol.symbol, Mcandidates);
1109 MPlist p = pl.Plist;
1110 pl.Set (p.key, p.val);
1111 for (p = p.next; ! p.IsEmpty; p = p.next);
1112 pl.Add (p.key, p.val);
1115 else if (pl.Symbol == Mif)
1119 transform (pl.next);
1121 else if (pl.Symbol == Mcond)
1123 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1126 MPlist p = pl.Plist;
1134 else if (pl.Symbol == Mdelete
1135 || pl.Symbol == Mmove
1136 || pl.Symbol == Mmark)
1141 MSymbol sym = pl.Symbol;
1142 MPlist p = new MPlist ();
1143 p.Add (MSymbol.symbol, Mmarker);
1144 p.Add (MSymbol.symbol, sym);
1145 pl.Set (MSymbol.plist, p);
1148 else if (pl.Symbol == Mpushback)
1152 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1155 else if (pl.IsMText)
1157 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1158 pl.Push (MSymbol.symbol, Mcandidates);
1161 else if (plist.IsSymbol)
1163 MSymbol sym = plist.Symbol;
1165 if (sym.Name.Length >= 3
1166 && sym.Name[0] == '@'
1167 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1169 int pos = int.Parse (sym.Name.Substring (1));
1170 MPlist p = new MPlist ();
1174 p.Add (MSymbol.symbol, Msurrounding_text_p);
1178 if (sym.Name[1] == '+')
1180 p.Add (MSymbol.symbol, Mchar_at);
1181 p.Add (MSymbol.integer, pos);
1183 plist.Set (MSymbol.plist, p);
1189 private static MText parse_description (MPlist plist)
1195 plist = plist.Plist;
1196 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1197 && plist.next.IsMText)
1198 return plist.next.Text;
1203 private static MText parse_description (XmlNode node)
1205 if (node.HasChildNodes)
1206 node = node.FirstChild;
1207 return node.InnerText;
1210 private static MText parse_title (XmlNode node)
1212 return node.InnerText;
1215 private void new_variable (Xex.Name name, string desc, int val,
1216 MPlist pl, Xex.Variable vari)
1218 Xex.Term term = new Xex.Term (val);
1225 range = new int[pl.Count * 2];
1226 for (int i = 0; i < range.Length; i++)
1230 MPlist p = pl.Plist;
1232 if (! p.IsInteger || ! p.next.IsInteger)
1233 throw new Exception ("Invalid range: " + p);
1234 range[i * 2] = p.Integer;
1235 range[i * 2 + 1] = p.next.Integer;
1237 else if (pl.IsInteger)
1238 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1240 throw new Exception ("Invalid range: " + p);
1244 domain.Defvar (new Xex.Variable.Int (name, desc, term, range));
1248 vari.DefaultValue = term;
1253 private void new_variable (Xex.Name name, string desc, MText val,
1254 MPlist pl, Xex.Variable vari)
1256 Xex.Term term = new Xex.Term ((string) val);
1263 range = new string[pl.Count * 2];
1264 for (int i = 0; i < range.Length; i++)
1269 throw new Exception ("Invalid range: " + p);
1273 domain.Defvar (new Xex.Variable.Str (name, desc, term, range));
1277 vari.DefaultValue = term;
1282 private void new_variable (Xex.Name name, string desc, MSymbol val,
1283 MPlist pl, Xex.Variable vari)
1285 Xex.Term term = new Xex.Term ((Xex.Name) val.name);
1292 range = new Xex.Name[pl.Count * 2];
1293 for (int i = 0; i < range.Length; i++)
1296 range[i] = pl.Symbol.name;
1298 throw new Exception ("Invalid range: " + p);
1302 domain.Defvar (new Xex.Variable.Sym (name, desc, term, range));
1306 vari.DefaultValue = term;
1311 private void parse_variables (MPlist plist)
1313 variables = new Variable[plist.Count];
1315 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1316 if (plist.IsPlist && plist.Plist.IsSymbol)
1318 MPlist p = plist.Plist;
1319 Xex.Name name = (Name) p.Symbol.name;
1321 string desc = parse_description (p);
1322 Xex.Variable vari = null;
1325 if (im_global.domain.GetVar (name, out vari))
1326 domain.Defvar (vari);
1332 new_variable (name, desc, p.Integer, p.next, vari);
1334 new_variable (name, desc, p.Text, p.next, vari);
1335 else if (p.IsSymbol)
1336 new_variable (name, desc, p.Symbol, p.next, vari);
1338 throw new Exception ("Unknown type: " + p.val);
1343 private void parse_variables (XmlNode node)
1345 XmlNodeList node_list = node.ChildNodes;
1347 variables = new Variable[node_list.Count];
1348 for (int i = 0; i < node_list.Count; i++)
1349 if (node_list[i].NodeType == XmlNodeType.Element)
1350 variables[i] = new Variable (node_list[i]);
1353 private void parse_commands (MPlist plist)
1355 commands = new Command[plist.Count];
1357 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1358 if (plist.IsPlist && plist.Plist.IsSymbol)
1359 commands[i++] = new Command (plist.Plist);
1362 private void parse_commands (XmlNode node)
1364 XmlNodeList node_list = node.ChildNodes;
1366 commands = new Command[node_list.Count];
1367 for (int i = 0; i < node_list.Count; i++)
1369 if (node_list[i].NodeType == XmlNodeType.Element)
1370 commands[i] = new Command (node_list[i]);
1374 private void parse_plugins (MPlist plist)
1376 plugins = new Dictionary<MSymbol, Plugin> ();
1378 for (; ! plist.IsEmpty; plist = plist.Next)
1380 MPlist p = plist.Plist;
1381 MSymbol sym = p.Symbol;
1382 Plugin plugin = new Plugin ();
1384 plugin.name = sym.Name;
1385 plugin.methods = new MPlist ();
1386 for (p = p.next; ! p.IsEmpty; p = p.next)
1387 plugin.methods.Add (p.Symbol, null);
1388 plugins.Add (sym, plugin);
1392 private void parse_plugins (XmlNode node)
1394 plugins = new Dictionary<MSymbol, Plugin> ();
1396 foreach (XmlNode n in node.ChildNodes)
1398 Plugin plugin = new Plugin ();
1399 plugin.name = n.Attributes["id"].Value;
1400 plugin.methods = new MPlist ();
1401 foreach (XmlNode nn in n.ChildNodes)
1402 plugin.methods.Add ((MSymbol) nn.Attributes["id"].Value,
1404 plugins.Add (plugin.name, plugin);
1408 private void parse_macros (XmlNode node)
1410 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1411 if (nn.NodeType == XmlNodeType.Element
1412 && nn.Name == "xi:include")
1414 XmlNode n = nn.FirstChild.FirstChild;
1415 MSymbol language = n.InnerText;
1417 MSymbol name = n.InnerText;
1419 MSymbol subname = (n != null ? n.InnerText : MSymbol.nil);
1420 n = n.ParentNode.NextSibling;
1421 MSymbol section = n.InnerText;
1423 MSymbol id = (n != null ? n.InnerText : MSymbol.nil);
1425 MInputMethod im = MInputMethod.Find (language, name, subname);
1426 if (im == null || ! im.Open ())
1428 if (id == MSymbol.nil)
1429 im.domain.CopyFunc (domain);
1431 im.domain.CopyFunc (domain, id);
1433 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1434 if (nn.NodeType == XmlNodeType.Element
1435 && nn.Name != "xi:include")
1436 domain.Defun ((MSymbol) node.GetAttribute ("id"));
1437 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1438 if (nn.NodeType == XmlNodeType.Element
1439 && nn.Name != "xi:include")
1440 domain.Defun ((MSymbol) node.GetAttribute ("id"), null,
1444 private void parse_maps (XmlNode node)
1448 private void parse_states (XmlNode node)
1452 private void parse_include (MPlist plist)
1454 if (! plist.IsPlist)
1456 MPlist p = plist.Plist;
1457 MSymbol language, name, subname;
1458 language = p.Symbol;
1461 name = subname = MSymbol.nil;
1467 subname = MSymbol.nil;
1472 MInputMethod im = MInputMethod.Find (language, name, subname);
1478 if (! plist.IsSymbol)
1480 MSymbol target_type = plist.Symbol;
1482 MSymbol target_name = MSymbol.nil;
1484 target_name = plist.Symbol;
1485 if (target_type == Mmacro)
1487 if (target_name == MSymbol.nil)
1488 im.domain.CopyFunc (domain);
1490 im.domain.CopyFunc (domain, target_name);
1492 else if (target_type == Mmap)
1494 if (target_name == MSymbol.nil)
1496 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1497 maps[kv.Key] = kv.Value;
1502 if (im.maps.TryGetValue (target_name, out map))
1503 maps[target_name] = map;
1506 else if (target_type == Mstate)
1508 if (target_name == MSymbol.nil)
1510 for (p = im.states; ! p.IsEmpty; p = p.next)
1511 states.Add (p.key, p.val);
1515 object state = im.states.Get (target_name);
1517 states.Add (target_name, state);
1522 private void parse_macros (MPlist plist)
1524 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1527 MPlist p = pl.Plist;
1531 domain.Defun (p.Symbol, null, null);
1533 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1536 MPlist p = pl.Plist;
1541 domain.Defun (p.Symbol, null, p.next);
1545 private void parse_maps (MPlist plist)
1547 for (; ! plist.IsEmpty; plist = plist.next)
1550 MPlist pl = plist.Plist;
1554 Map map = new Map ();
1555 map.name = pl.Symbol;
1556 maps[map.name] = map;
1557 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1561 MPlist p = pl.Plist;
1564 keys = new KeySeq (p.Text);
1566 keys = new KeySeq (p.Plist);
1573 MXex expr = new MXex (p, domain);
1574 map.Add (keys, 0, expr);
1579 private void parse_states (MPlist plist)
1581 for (; ! plist.IsEmpty; plist = plist.next)
1584 MPlist pl = plist.Plist;
1595 State state = new State (pl.Symbol);
1596 state.title = title;
1598 states = new MPlist ();
1599 states.Add (state.name, state);
1600 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1604 MPlist p = pl.Plist;
1607 MSymbol map_name = p.Symbol;
1610 state.branches.Add (map_name,
1611 new MXex (p, domain));
1616 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1619 ((MInputContext) domain.context).insert (args[0]);
1623 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1627 ((MInputContext) domain.context).insert_candidates (args[0]);
1631 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1634 MSymbol sym = (MSymbol) args[0].Args[0].Val;
1636 return ((MInputContext) domain.context).marker (sym);
1639 private static Xex.Term char_at (Xex.Domain domain, Xex.Variable vari,
1642 return ((MInputContext) domain.context).char_at ((int) args[0].Val);
1645 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1648 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1652 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1655 MInputContext ic = (MInputContext) domain.context;
1658 ic.select (args[0].Intval);
1660 ic.select ((MSYmbol) ((string) args[0].Nameval));
1664 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1667 ((MInputContext) domain.context).show ();
1671 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1674 ((MInputContext) domain.context).hide ();
1678 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1681 ((MInputContext) domain.context).move (args[0].Intval);
1685 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1688 ((MInputContext) domain.context).mark ((Marker) args[0].Objval);
1692 private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
1695 return new KeySeq (args);
1698 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1701 MInputContext ic = (MInputContext) domain.context;
1704 ic.pushback (args[0].Intval);
1705 else if (args[0].IsStr)
1706 ic.pushback (new KeySeq (args[0].Strval));
1708 ic.pushback ((KeySeq) args[0].Objval);
1712 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1715 ((MInputContext) domain.context).pop ();
1719 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1722 int n = args.Length == 0 ? -2 : args[0].Intval;
1723 ((MInputContext) domain.context).undo (n);
1727 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1730 ((MInputContext) domain.context).commit ();
1734 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1737 ((MInputContext) domain.context).commit ();
1738 args = new Xex.Term[2];
1739 args[0] = args[1] = catch_tag;
1740 return Xex.Fthrow (domain, vari, args);
1743 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1746 ((MInputContext) domain.context).shift (args[0].Symval);
1750 public override string ToString ()
1752 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1753 if (commands != null)
1755 str += " (commands";
1756 foreach (Command cmd in commands)
1760 if (variables != null)
1762 str += " (variables";
1763 foreach (Variable var in variables)
1767 if (plugins != null)
1770 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1771 str += " " + kv.Value;
1775 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1776 str += " " + kv.Value;
1778 foreach (MPlist p in states)
1784 public class MInputContext
1786 internal static MSymbol Mcandidates_group_size = "candidates-group-size";
1787 private static MSymbol Mat_less_than = "@<";
1788 private static MSymbol Mat_greater_than = "@>";
1789 private static MSymbol Mat_minus = "@-";
1790 private static MSymbol Mat_plus = "@+";
1791 private static MSymbol Mat_open_square_bracket = "@[";
1792 private static MSymbol Mat_close_square_bracket = "@]";
1794 public MInputMethod im;
1795 private MText produced;
1796 private bool active;
1797 private MText status;
1798 private bool status_changed;
1799 private MText preedit;
1800 private bool preedit_changed;
1801 private int cursor_pos;
1802 private bool cursor_pos_changed;
1803 private Candidates candidates;
1804 private MPlist candidate_group;
1805 private int candidate_index;
1806 private int candidate_from, candidate_to;
1807 private bool candidate_show;
1808 private bool candidate_changed;
1810 private Stack<MInputMethod.State> states;
1811 internal MInputMethod.KeySeq keys;
1812 private int key_head;
1813 private int state_key_head;
1814 private MPlist state_boundary;
1815 private int commit_key_head;
1816 private MText state_preedit;
1817 private int state_pos;
1818 internal MPlist markers = new MPlist ();
1819 private MPlist vars;
1820 private MPlist vars_saved;
1821 internal MText preceding_text = new MText ();
1822 internal MText following_text = new MText ();
1823 private bool key_unhandled;
1825 internal Xex.Domain domain;
1827 public MInputContext (MInputMethod im)
1830 domain = new Xex.Domain (im.domain, this);
1831 states = new Stack<MInputMethod.State> ();
1832 states.Push ((MInputMethod.State) im.states.val);
1833 keys = new MInputMethod.KeySeq ();
1836 private void adjust_markers (int from, int to, object inserted)
1838 int ins = (inserted == null ? 0
1839 : inserted is int ? 1
1840 : ((MText) inserted).Length);
1841 int diff = ins - (to - from);
1843 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1845 int pos = plist.Integer;
1849 plist.val = pos + diff;
1854 if (cursor_pos >= to)
1856 else if (cursor_pos > from)
1860 private void preedit_replace (int from, int to, int c)
1862 preedit.Del (from, to);
1863 preedit.Ins (from, c);
1864 adjust_markers (from, to, c);
1867 private void preedit_replace (int from, int to, MText mt)
1869 preedit[from, to] = mt;
1870 adjust_markers (from, to, mt);
1873 internal void insert (Xex.Term arg)
1875 if (arg.IsInt is int)
1876 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1878 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1879 preedit_changed = true;
1880 cursor_pos_changed = true;
1883 private class Candidates
1890 public Block (int index, MPlist plist)
1901 get { return (Data is MText
1902 ? ((MText) Data).Length
1903 : ((MPlist) Data).Count); }
1906 public object this[int i]
1909 if (Data is MText) return ((MText) Data)[i];
1910 return ((MPlist) Data)[i];
1915 private Block[] blocks;
1916 private int row = 0;
1917 private int index = 0;
1918 public object[] group;
1920 private bool IsFixed { get { return group != null; } }
1923 Block last = blocks[blocks.Length - 1];
1924 return last.Index + last.Count; }
1928 get { return (IsFixed ? index % group.Length
1929 : index - blocks[row].Index); }
1932 public object Group {
1933 get { return (IsFixed ? group : blocks[row].Data); }
1936 public int GroupLength
1941 int nitems = group.Length;
1942 int start = index - (index % nitems);
1944 return (start + nitems <= total ? nitems : total - start);
1946 return blocks[row].Count;
1950 public object Current {
1952 return (IsFixed ? group[index % group.Length]
1953 : blocks[row][index - blocks[row].Index]);
1957 public Candidates (MPlist list, int column)
1959 int nblocks = list.Count;
1961 blocks = new Block[nblocks];
1962 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
1963 start += (blocks[i] = new Block (index, list)).Count;
1965 group = new object[column];
1968 public Candidates (List<Xex.Term> list, int column)
1970 int nblocks = list.Count;
1972 blocks = new Block[nblocks];
1973 for (int i = 0, start = 0; i < nblocks; i++)
1974 start += (blocks[i] = new Block (index, list[i])).Count;
1976 group = new object[column];
1979 public static void Detach (MInputContext ic)
1981 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
1982 ic.candidates = null;
1983 ic.preedit_changed = true;
1984 ic.cursor_pos_changed = true;
1985 ic.candidate_changed = true;
1988 // Fill the array "group" by candidates stating from INDEX.
1989 // INDEX must be a multiple of "column". Set NTIMES to the
1990 // number of valid candidates in "group". Update "block" if
1991 // necessary. Return "group".
1993 private int fill_group (int start)
1995 int nitems = group.Length;
1997 Block b = blocks[r];
1999 if (start < b.Index)
2000 while (start < b.Index)
2003 while (start >= b.Index + b.Count)
2007 int count = b.Count;
2009 for (int i = 0; i < nitems; i++, start++)
2014 if (r == blocks.Length)
2020 group[i] = b[start];
2025 // Update "row" to what contains the first candidate of
2026 // the previous candidate-group, update "current_index", and
2027 // update "group" if necessary. Return the previous
2028 // candidate-group. Set NITEMS to the number of valid
2029 // candidates contained in that group.
2031 public int PrevGroup ()
2038 nitems = group.Length;
2039 if ((index -= col + nitems) < 0)
2040 index = (Total / nitems) * nitems;
2041 nitems = fill_group (index);
2045 row = row > 0 ? row-- : blocks.Length - 1;
2046 nitems = blocks[row].Count;
2047 index = blocks[row].Index;
2049 index += col < nitems ? col : nitems - 1;
2053 public int NextGroup ()
2060 nitems = group.Length;
2061 if ((index += nitems - col) >= Total)
2063 nitems = fill_group (index);
2067 row = row < blocks.Length - 1 ? row + 1 : 0;
2068 nitems = blocks[row].Count;
2069 index = blocks[row].Count;
2071 index += col < nitems ? col : nitems - 1;
2081 int nitems = PrevGroup ();
2082 index += col < nitems - 1 ? col : nitems - 1;
2091 int nitems = GroupLength;
2093 if (col == nitems - 1)
2095 nitems = NextGroup ();
2102 public void First ()
2109 index += GroupLength - (Column + 1);
2112 public void Select (int col)
2114 int maxcol = GroupLength - 1;
2117 index = index - Column + col;
2121 private void update_candidate ()
2123 object candidate = candidates.Current;
2125 if (candidate is MText)
2127 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2128 candidate_to = candidate_from + ((MText) candidate).Length;
2132 preedit_replace (candidate_from, candidate_to, (int) candidate);
2133 candidate_to = candidate_from + 1;
2135 preedit.PushProp (candidate_from, candidate_to,
2136 MInputMethod.Mcandidates, this);
2137 cursor_pos = candidate_from;
2138 preedit_changed = true;
2139 cursor_pos_changed = true;
2140 candidate_changed = true;
2143 internal void insert_candidates (Xex.Term arg)
2147 if (domain.IsBound (Mcandidates_group_size))
2149 object val = domain.GetValue (Mcandidates_group_size);
2153 candidates = new Candidates (arg.Listval, column);
2154 candidate_from = candidate_to = cursor_pos;
2155 update_candidate ();
2158 internal void select (int n)
2160 if (candidates != null)
2162 candidates.Select (n);
2163 update_candidate ();
2167 internal void select (MSymbol sym)
2169 if (candidates != null)
2171 if (sym == Mat_less_than)
2172 candidates.First ();
2173 else if (sym == Mat_greater_than)
2175 else if (sym == Mat_minus)
2177 else if (sym == Mat_plus)
2179 else if (sym == Mat_open_square_bracket)
2180 candidates.PrevGroup ();
2181 else if (sym == Mat_close_square_bracket)
2182 candidates.NextGroup ();
2186 internal int marker (MSymbol sym)
2188 int pos = cursor_pos;
2190 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2192 switch (sym.Name[0])
2194 case '<': pos = 0; break;
2195 case '>': pos = preedit.Length; break;
2196 case '-': pos = cursor_pos - 1; break;
2197 case '+': pos = cursor_pos + 1; break;
2202 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2209 if (cursor_pos < preedit.Length - 1)
2212 preedit.FindProp (MInputMethod.Mcandidates, pos,
2216 pos = preedit.Length;
2219 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2224 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2226 pos = int.Parse (sym.Name.Substring (2));
2230 object val = markers.Get (sym);
2238 internal int char_at (int pos)
2245 if (preceding_text.Length < -pos)
2247 MPlist plist = new MPlist ();
2248 plist.Push (MSymbol.integer, pos);
2249 if (MInputMethod.GetSurroundingText != null
2250 && MInputMethod.GetSurroundingText (this, plist)
2252 && preceding_text.Length < plist.Text.Length)
2253 preceding_text = plist.Text;
2255 c = (-pos < preceding_text.Length
2256 ? preceding_text[preceding_text.Length + pos] : -1);
2258 else if (pos >= 0 && pos < preedit.Length)
2262 pos -= preedit.Length;
2263 if (pos >= following_text.Length)
2265 MPlist plist = new MPlist ();
2266 plist.Push (MSymbol.integer, pos + 1);
2267 if (MInputMethod.GetSurroundingText != null
2268 && MInputMethod.GetSurroundingText (this, plist)
2270 && following_text.Length < plist.Text.Length)
2271 following_text = plist.Text;
2273 c = (pos < following_text.Length ? following_text[pos] : -1);
2278 internal void delete (int pos)
2280 if (pos < cursor_pos)
2281 preedit_replace (pos, cursor_pos, null);
2283 preedit_replace (cursor_pos, pos, null);
2284 preedit_changed = true;
2285 cursor_pos_changed = true;
2288 internal void show ()
2290 candidate_show = true;
2291 candidate_changed = true;
2294 internal void hide ()
2296 candidate_show = false;
2297 candidate_changed = true;
2300 internal void move (int pos)
2304 else if (pos > preedit.Length)
2305 pos = preedit.Length;
2306 if (pos != cursor_pos)
2309 preedit_changed = true;
2313 internal void mark (MSymbol sym)
2315 MPlist slot = markers.Find (sym);
2318 markers.Push (sym, cursor_pos);
2320 slot.val = cursor_pos;
2323 internal void pushback (int n)
2336 if (key_head > keys.Count)
2337 key_head = keys.Count;
2341 internal void pushback (MInputMethod.KeySeq keyseq)
2345 if (key_head < keys.Count)
2346 keys.RemoveRange (key_head, keys.Count - key_head);
2347 for (int i = 0; i < keyseq.Count; i++)
2348 keys.Add (keyseq[i]);
2351 internal void pop ()
2353 if (key_head < keys.Count)
2354 keys.RemoveRange (key_head, 1);
2357 internal void undo (int n)
2360 keys.RemoveRange (keys.Count + n, - n);
2362 keys.RemoveRange (n, keys.Count - n);
2366 internal void commit ()
2368 produced.Cat (preedit);
2370 preedit_changed = true;
2373 internal void shift (MSymbol sym)
2375 MInputMethod.State state;
2377 if (sym == MSymbol.t)
2379 if (states.Count > 1)
2380 state = states.Pop ();
2382 state = states.Peek ();
2386 state = (MInputMethod.State) im.states.Get (sym);
2388 throw new Exception ("Unknown state: " + state.name);
2391 state = states.Pop ();
2392 if (state == (MInputMethod.State) im.states.val)
2399 state_key_head = key_head;
2400 state_pos = cursor_pos;
2401 state_preedit = preedit.Dup ();
2402 if (state != states.Peek ())
2404 states.Push (state);
2405 state_boundary = domain.SetBoundary ();
2406 status = state.title;
2409 status_changed = true;
2411 = (Xex) state.branches.Get (MSymbol.t);
2412 if (on_entry != null)
2413 on_entry.Eval (domain);
2418 internal bool call (MSymbol module, MSymbol method, MPlist arglist)
2420 MInputMethod.Plugin plugin;
2422 if (! im.plugins.TryGetValue (module, out plugin))
2424 if (plugin.assembly == null)
2429 assembly = Assembly.LoadFrom (module.Name + ".dll");
2433 Type t = assembly.GetType ("Plugin");
2434 for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
2435 p.Set (p.key, t.GetMethod (p.key.Name));
2438 MethodInfo method_info = (MethodInfo) plugin.methods.Get (method);
2439 if (method_info == null)
2441 object[] method_arg = new object[1];
2442 method_arg[0] = arglist;
2443 bool result = (bool) method_info.Invoke (null, method_arg);
2446 if (! arglist.IsEmpty)
2447 (new Xex (arglist, domain)).Eval (domain);
2451 internal void reset ()
2454 state_preedit.Del ();
2458 key_head = commit_key_head = 0;
2460 states.Push ((MInputMethod.State) im.states.Val);
2465 internal object GetCandidates (out int column)
2468 if (cursor_pos == 0)
2470 Candidates candidates
2471 = (Candidates) preedit.GetProp (cursor_pos - 1,
2472 MInputMethod.Mcandidates);
2473 if (candidates == null)
2475 column = candidates.Column;
2476 return candidates.Current;
2479 internal void HandleKey ()
2481 MInputMethod.State state = states.Peek ();