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 Mtitle = "title";
38 private static MSymbol Minclude = "include";
39 private static MSymbol Mmacro = "macro";
40 private static MSymbol Mmap = "map";
41 private static MSymbol Mmap_list = "map-list";
42 private static MSymbol Mstate = "state";
43 internal static MSymbol Mcandidates = "candidates";
44 private static MSymbol Minsert = "insert";
45 private static MSymbol Mdelete = "delete";
46 private static MSymbol Mmove = "move";
47 private static MSymbol Mmark = "mark";
48 private static MSymbol Mmarker = "marker";
49 private static MSymbol Mset = "set";
50 private static MSymbol Madd = "add";
51 private static MSymbol Msub = "sub";
52 private static MSymbol Mmul = "mul";
53 private static MSymbol Mdiv = "div";
54 private static MSymbol Mif = "if";
55 private static MSymbol Mcond = "cond";
56 private static MSymbol Mchar_at = "char-at";
57 private static MSymbol Msurrounding_text_p = "surrounding-text-p";
58 private static MSymbol Mpushback = "pushback";
59 private static MSymbol Mkeyseq = "keyseq";
61 private static Xex.Symbol Nprogn = "progn";
62 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
63 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
65 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
66 = new Dictionary<MDatabase.Tag, MInputMethod> ();
68 internal static MInputMethod im_global = null;
71 private enum LoadStatus
81 public enum KeyModifier
87 Control_L = 0x01000000,
88 Control_R = 0x02000000,
104 private static Dictionary<string, uint> keysyms
105 = new Dictionary<string, uint> ();
106 private static Dictionary<string, KeyModifier> keymodifiers
107 = new Dictionary<string, KeyModifier> ();
108 private static uint keysym_base = 0x200000;
109 private static uint char_mask = ~((uint) KeyModifier.All);
113 keysyms["bs"] = keysyms["backspace"] = 0x08;
114 keysyms["tab"] = 0x09;
115 keysyms["lf"] = keysyms["linefeed"] = 0x10;
116 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
117 keysyms["esc"] = keysyms["escape"] = 0x1B;
118 keysyms["spc"] = keysyms["space"] = 0x20;
119 keysyms["del"] = keysyms["delete"] = 0x7F;
120 keymodifiers["shift-l"] = KeyModifier.Shift_L;
121 keymodifiers["shift-r"] = KeyModifier.Shift_R;
122 keymodifiers["shift"] = KeyModifier.Shift;
123 keymodifiers["control-l"] = KeyModifier.Control_L;
124 keymodifiers["control-r"] = KeyModifier.Control_R;
125 keymodifiers["control"] = KeyModifier.Control;
126 keymodifiers["alt-l"] = KeyModifier.Alt_L;
127 keymodifiers["alt-r"] = KeyModifier.Alt_R;
128 keymodifiers["alt"] = KeyModifier.Alt;
129 keymodifiers["altgr"] = KeyModifier.AltGr;
130 keymodifiers["super"] = KeyModifier.Super;
131 keymodifiers["hyper"] = KeyModifier.Hyper;
134 private static uint decode_keysym (MSymbol keysym)
137 string name = keysym.Name;
139 if (name.Length == 1)
141 name = name.ToLower ();
142 if (! keysyms.TryGetValue (name, out key))
143 keysyms[name] = key = keysym_base++;
147 private static uint combine_modifiers (uint c, KeyModifier modifiers)
149 if (c < 0x7F && c != 0x20)
151 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
152 && Char.IsLower ((char) c))
154 modifiers &= ~KeyModifier.Shift;
155 c = Char.ToUpper ((char) c);
157 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
159 modifiers &= ~KeyModifier.Control;
163 return c | (uint) modifiers;
171 public Key (uint c, KeyModifier modifiers)
173 key = combine_modifiers (c, modifiers);
176 public Key (MSymbol keysym, KeyModifier modifiers)
178 key = combine_modifiers (decode_keysym (keysym), modifiers);
181 public Key (MSymbol keysym)
183 string str = keysym.Name;
184 int len = str.Length;
186 KeyModifier modifiers = KeyModifier.None;
188 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
191 modifiers |= KeyModifier.Shift;
192 else if (str[i] == 'C')
193 modifiers |= KeyModifier.Control;
194 else if (str[i] == 'A')
195 modifiers |= KeyModifier.Alt;
196 else if (str[i] == 'G')
197 modifiers |= KeyModifier.AltGr;
198 else if (str[i] == 's')
199 modifiers |= KeyModifier.Super;
200 else if (str[i] == 'H')
201 modifiers |= KeyModifier.Hyper;
204 key = combine_modifiers (str[i], modifiers);
206 key = combine_modifiers (decode_keysym (keysym), modifiers);
209 public Key (MPlist plist)
211 KeyModifier modifiers = KeyModifier.None;
214 for (p = plist; ! p.IsEmpty; p = p.next)
218 if (! p.next.IsEmpty)
219 throw new Exception ("Invalid Key: " + plist);
222 else if (! p.IsSymbol)
223 throw new Exception ("Invalid Key: " + plist);
226 string name = p.Symbol.Name.ToLower ();
229 if (! keymodifiers.TryGetValue (name, out m))
234 if (p.IsEmpty || ! p.next.IsEmpty)
235 throw new Exception ("Invalid Key: " + plist);
237 key = combine_modifiers ((uint) p.Integer, modifiers);
239 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
242 public bool HasModifier
244 get { return ((key & (uint) KeyModifier.All) != 0); }
247 public bool Match (Key k)
251 if ((k.key & char_mask) != (key & char_mask))
253 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
254 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
255 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
256 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
257 && (m2 & KeyModifier.Shift) != KeyModifier.None))
258 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
259 || ((m1 & KeyModifier.Control) == KeyModifier.Control
260 && (m2 & KeyModifier.Control) != KeyModifier.None))
261 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
262 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
263 && (m2 & KeyModifier.Alt) != KeyModifier.None))
264 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
267 public override string ToString ()
269 string str = Char.ToString ((char) key);
270 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
272 if (m != KeyModifier.None)
274 if ((m & KeyModifier.Shift) != KeyModifier.None)
276 if ((m & KeyModifier.Control) != KeyModifier.None)
278 if ((m & KeyModifier.Alt) != KeyModifier.None)
280 if ((m & KeyModifier.AltGr) != KeyModifier.None)
282 if ((m & KeyModifier.Super) != KeyModifier.None)
284 if ((m & KeyModifier.Hyper) != KeyModifier.None)
291 public class KeySeq : Xex.TermValue
293 public List<Key> keyseq = new List<Key> ();
295 public override Xex.TermValue Clone ()
297 KeySeq ks = new KeySeq ();
298 ks.keyseq.InsertRange (0, keyseq);
304 public KeySeq (MPlist plist)
306 foreach (MPlist p in plist)
309 keyseq.Add (new Key (p.Symbol));
310 else if (p.IsInteger)
311 keyseq.Add (new Key ((char) p.Integer));
313 keyseq.Add (new Key (p.Plist));
315 throw new Exception ("Invalid Key Sequence: " + plist);
319 public KeySeq (MText mt) : base ()
321 for (int i = 0; i < mt.Length; i++)
322 keyseq.Add (new Key ((uint) mt[i]));
325 private static uint parse_integer (string str)
327 if (Char.IsDigit (str[0]))
329 if (str[0] == '0' && str.Length > 2 && str[1] == 'x')
332 for (int idx = 2; idx < str.Length; idx++)
335 if (c >= '0' && c <= '9')
336 i = i * 16 + (c - '0');
337 else if (c >= 'A' && c <= 'F')
338 i = i * 16 + 10 + (c - 'A');
339 else if (c >= 'a' && c <= 'f')
340 i = i * 16 + 10 + (c - 'a');
346 return UInt32.Parse (str);
348 else if (str[0] == '?')
353 public KeySeq (XmlNode node)
355 XmlAttributeCollection acol = node.Attributes;
358 if (acol != null && (n = acol["keys"]) != null)
360 foreach (char c in n.Value)
361 keyseq.Add (new Key ((uint) c));
364 for (node = node.FirstChild; node != null; node = node.NextSibling)
366 if (node.Name == "key-event")
367 keyseq.Add (new Key ((MSymbol) node.InnerText));
369 keyseq.Add (new Key (parse_integer (node.InnerText)));
373 public KeySeq (Xex.Term[] args)
375 foreach (Xex.Term term in args)
378 keyseq.Add (new Key ((MSymbol) ((string) term.Symval)));
380 keyseq.Add (new Key (term.Strval));
384 public override string ToString ()
388 foreach (Key key in keyseq)
392 foreach (Key k in keyseq)
393 str += " " + k.ToString ();
397 foreach (Key key in keyseq)
398 str += key.ToString ();
403 public class Variable
406 public MText description;
409 public object[] candidates;
411 public Variable (MPlist p)
415 description = parse_description (p);
416 if (description == null)
417 description = new MText ("No description");
420 type = (p.IsMText ? typeof (MText)
421 : p.IsInteger ? typeof (int)
422 : p.IsSymbol ? typeof (MSymbol)
426 candidates = new object[p.Count];
427 for (int i = 0; ! p.IsEmpty; i++, p = p.next)
428 candidates[i] = p.val;
431 private static Type parse_value (XmlNode node, out object value)
433 string typename = node.Attributes["type"].Value;
436 if (typename == "integer")
439 if (! Int32.TryParse (node.InnerText, out i))
444 else if (typename == "string")
446 MText mt = node.InnerText;
448 type = typeof (MText);
450 else if (typename == "symbol")
452 MSymbol sym = node.InnerText;
454 type = typeof (MSymbol);
459 type = typeof (object);
464 public Variable (XmlNode node)
466 name = node.Attributes["id"].Value;
467 for (node = node.FirstChild; node != null; node = node.NextSibling)
468 if (node.NodeType == XmlNodeType.Element)
470 if (node.Name == "description")
471 description = parse_description (node);
472 else if (node.Name == "value")
473 type = parse_value (node, out value);
474 else if (node.Name == "valiable-value-candidate")
476 XmlNodeList n_list = node.ChildNodes;
477 candidates = new object[n_list.Count];
478 for (int i = 0; i < n_list.Count; i++)
481 parse_value (n_list[i], out val);
488 public override string ToString ()
490 return ("(" + name + " \"" + (string) description
491 + "\" " + type + " " + value + " " + candidates + ")");
498 public MText description;
499 public List<KeySeq> keys;
501 public Command (MPlist p)
505 description = parse_description (p);
506 if (description == null)
507 description = "No description";
508 keys = new List<KeySeq> ();
509 for (p = p.next; ! p.IsEmpty; p = p.next)
512 keys.Add (new KeySeq (p.Text));
514 keys.Add (new KeySeq (p.Plist));
518 public Command (XmlNode node)
520 name = node.Attributes["id"].Value;
521 keys = new List<KeySeq> ();
522 for (node = node.FirstChild; node != null; node = node.NextSibling)
524 if (node.Name == "description")
525 description = parse_description (node);
526 else if (node.Name == "keyseq")
527 keys.Add (new KeySeq (node));
531 public override string ToString ()
533 string str = "(" + name + " \"" + (string) description;
534 foreach (KeySeq keyseq in keys)
540 internal class Plugin
543 private Assembly assembly;
544 private Type plugin_type;
546 public Plugin (string name)
551 public MethodInfo GetMethod (Xex.Symbol name)
553 if (assembly == null)
555 assembly = Assembly.LoadFrom (name + ".dll");
556 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
559 MethodInfo info = plugin_type.GetMethod ((string) name);
561 throw new Exception ("Invalid plugin method: " + name);
565 public override string ToString ()
567 return String.Format ("(module {0}", name);
571 internal class PluginMethod : Xex.Function
573 private Plugin plugin;
574 private MethodInfo method_info;
575 object[] parameters = new object[2];
577 public PluginMethod (Plugin plugin, string name)
578 : base ((Xex.Symbol) name, 0, -1)
580 this.plugin = plugin;
583 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
586 args = (Xex.Term[]) args.Clone ();
587 for (int i = 0; i < args.Length; i++)
589 args[i] = args[i].Eval (domain);
593 if (method_info == null)
594 method_info = plugin.GetMethod (name);
595 parameters[0] = domain.context;
596 parameters[1] = args;
597 return (Xex.Term) method_info.Invoke (null, parameters);
601 internal abstract class Marker : Xex.TermValue
605 public Marker (MSymbol name)
610 public abstract int Position (MInputContext ic);
611 public abstract void Mark (MInputContext ic);
613 public class Named : Marker
617 public Named (MSymbol name) : this (name, 0) { }
619 private Named (MSymbol name, int p) : base (name) { pos = p; }
621 public override int Position (MInputContext ic) { return pos; }
623 public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
625 public override Xex.TermValue Clone ()
627 return new Named (name, pos);
631 public class Predefined : Marker
633 public Predefined (MSymbol name) : base (name) { }
635 public override int Position (MInputContext ic)
637 switch (name.Name[1]) {
639 case '>': return ic.preedit.Length;
640 case '-': return ic.cursor_pos - 1;
641 case '+': return ic.cursor_pos + 1;
643 if (ic.cursor_pos > 0)
645 int pos = ic.cursor_pos;
647 ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
653 if (ic.cursor_pos < ic.preedit.Length - 1)
655 int pos = ic.cursor_pos;
657 ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
661 return ic.preedit.Length;
663 return name.Name[1] - '0';
667 public override void Mark (MInputContext ic)
669 throw new Exception ("Can't set predefined marker: " + name);
672 public override Xex.TermValue Clone ()
674 return new Predefined (name);
678 static internal Dictionary<MSymbol,Predefined> predefined_markers;
682 predefined_markers = new Dictionary<MSymbol,Predefined> ();
683 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
684 "@0", "@1", "@2", "@3", "@4",
685 "@5", "@6", "@7", "@8", "@9" };
686 foreach (MSymbol s in symlist)
687 predefined_markers[s] = new Predefined (s);
690 public static Marker Get (MInputContext ic, MSymbol name)
695 if (predefined_markers.TryGetValue (name, out pred))
697 if (name.Name[0] == '@')
698 throw new Exception ("Invalid marker name: " + name);
699 m = (Marker) ic.markers.Get (name);
702 m = new Named (name);
703 ic.markers.Put (name, m);
712 public Dictionary<Key, Map> submaps;
713 public Xex.Term actions;
715 public void Add (KeySeq keys, int index, Xex.Term actions)
720 submaps = new Dictionary<Key, Map> ();
722 submaps.TryGetValue (keys.keyseq[index], out sub);
725 Key key = keys.keyseq[index];
726 submaps[key] = sub = new Map ();
728 if (index + 1 < keys.keyseq.Count)
729 sub.Add (keys, index + 1, actions);
731 this.actions = actions;
734 public Xex.Term Lookup (KeySeq keys, int index)
738 if (index + 1 == keys.keyseq.Count)
740 if (submaps.TryGetValue (keys.keyseq[index], out sub))
741 return sub.Lookup (keys, index + 1);
745 private void describe (MText mt, KeySeq keyseq)
747 if (keyseq.keyseq.Count > 0)
749 mt.Cat (" (").Cat (keyseq.ToString ());
750 mt.Cat (' ').Cat (actions.ToString ());
754 foreach (KeyValuePair<Key, Map> kv in submaps)
756 keyseq.keyseq.Add (kv.Key);
757 kv.Value.describe (mt, keyseq);
758 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
762 public override string ToString ()
764 MText mt = "(" + name.Name;
765 KeySeq keyseq = new KeySeq ();
767 describe (mt, keyseq);
777 public MPlist branches = new MPlist ();
779 public State (MSymbol name)
784 public override string ToString ()
786 MText mt = "(" + name.Name;
789 mt.Cat (" \"" + title + "\"");
790 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
791 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
792 return (string) mt + ")";
797 internal Xex.Domain domain;
799 private LoadStatus load_status = LoadStatus.None;
800 private MDatabase.Tag tag;
801 private MDatabase mdb;
803 private MText description;
804 internal MText title;
805 internal Command[] commands;
806 internal Xex.Symbol[] var_names;
807 internal Dictionary<MSymbol, Plugin> plugins;
808 internal Dictionary<MSymbol, Map> maps;
809 internal MPlist states;
811 static MInputMethod ()
813 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
814 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
815 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
816 im_domain.DefSubr (Fselect, "select", true, 1, 1);
817 im_domain.DefSubr (Fshow, "show", true, 0, 0);
818 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
819 im_domain.DefSubr (Fmove, "move", true, 1, 1);
820 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
821 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
822 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
823 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
824 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
825 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
826 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
827 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
828 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
829 im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
831 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
832 List<MDatabase> list = MDatabase.List (tag);
833 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
834 foreach (MDatabase mdb in list)
835 im_table[mdb.tag] = new MInputMethod (mdb.tag);
839 private MInputMethod (MDatabase.Tag tag)
844 // Instance Properties
845 public MSymbol Language { get { return tag[1]; } }
846 public MSymbol Name { get { return tag[2]; } }
847 public MSymbol SubName { get { return tag[3]; } }
849 public bool Info (out MText description,
851 out Xex.Variable[] variables,
852 out Command[] commands)
854 if ((load_status & LoadStatus.Header) != LoadStatus.Header
863 description = this.description;
865 if (var_names == null)
869 variables = new Xex.Variable[var_names.Length];
871 foreach (Xex.Symbol name in var_names)
872 variables[i++] = domain.GetVar (name, false);
874 commands = this.commands;
878 public static MInputMethod Find (MSymbol language, MSymbol name)
880 return Find (language, name, MSymbol.nil);
883 public static MInputMethod Find (MSymbol language, MSymbol name,
886 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
890 return (im_table.TryGetValue (tag, out im) ? im : null);
895 return ((load_status == LoadStatus.Full) || load_body ());
898 public static MInputMethod[] List ()
900 MInputMethod[] array = new MInputMethod[im_table.Count];
903 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
904 array[i++] = kv.Value;
908 private bool load_header ()
910 mdb = MDatabase.Find (tag);
914 MSymbol format = mdb.Format;
916 if (format == MSymbol.plist)
917 load ((MPlist) mdb.Load (Mmap), false);
920 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
921 load (doc.DocumentElement, false);
923 } catch (Exception e) {
924 Console.WriteLine ("{0}\n", e);
925 load_status = LoadStatus.Error;
928 load_status |= LoadStatus.Header;
932 private bool load_body ()
934 domain = new Xex.Domain (im_domain, null);
935 mdb = MDatabase.Find (tag);
939 object obj = mdb.Load ();
941 load ((MPlist) obj, true);
943 load ((XmlDocument) obj, true);
944 } catch (Exception e) {
945 Console.WriteLine (e);
946 load_status = LoadStatus.Error;
949 load_status = LoadStatus.Full;
953 private void load (MPlist plist, bool full)
955 maps = new Dictionary<MSymbol, Map> ();
956 states = new MPlist ();
958 for (; ! plist.IsEmpty; plist = plist.next)
961 MPlist pl = plist.Plist;
964 MSymbol sym = pl.Symbol;
967 if (sym == Mdescription)
968 description = parse_description (pl);
969 else if (sym == Mtitle)
974 else if (sym == Mvariable)
975 parse_variables (pl);
976 else if (sym == Mcommand)
982 else if (sym == Minclude)
984 else if (sym == Mmacro)
986 else if (sym == Mmap)
988 else if (sym == Mstate)
993 if (description == null)
994 description = (MText) "No description";
996 title = new MText (tag[2].Name);
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, null);
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 (commands == null)
1054 commands = new Command[0];
1059 State state = new State ((MSymbol) "init");
1060 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1061 state.branches.Add (kv.Key, null);
1062 states.Add (state.name, state);
1066 private static void transform (MPlist plist)
1068 for (; ! plist.IsEmpty; plist = plist.next)
1072 MPlist p = new MPlist ();
1073 p.Add (MSymbol.symbol, Minsert);
1074 p.Add (MSymbol.mtext, plist.Text);
1075 plist.Set (MSymbol.plist, p);
1077 else if (plist.IsInteger)
1079 MPlist p = new MPlist ();
1080 p.Add (MSymbol.symbol, Minsert);
1081 p.Add (MSymbol.integer, plist.Integer);
1082 plist.Set (MSymbol.plist, p);
1084 else if (plist.IsPlist)
1086 MPlist pl = plist.Plist;
1090 if (pl.Symbol == Madd)
1091 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1092 else if (pl.Symbol == Msub)
1093 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1094 else if (pl.Symbol == Mmul)
1095 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1096 else if (pl.Symbol == Mdiv)
1097 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1098 else if (pl.Symbol == Minsert)
1100 // (insert (CANDIDATES ...))
1101 // => (candidates CANDIDATES ...)
1102 if (pl.next.IsPlist)
1104 pl.Set (MSymbol.symbol, Mcandidates);
1106 MPlist p = pl.Plist;
1107 pl.Set (p.key, p.val);
1108 for (p = p.next; ! p.IsEmpty; p = p.next);
1109 pl.Add (p.key, p.val);
1112 else if (pl.Symbol == Mif)
1116 transform (pl.next);
1118 else if (pl.Symbol == Mcond)
1120 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1123 MPlist p = pl.Plist;
1131 else if (pl.Symbol == Mdelete
1132 || pl.Symbol == Mmove
1133 || pl.Symbol == Mmark)
1138 MSymbol sym = pl.Symbol;
1139 MPlist p = new MPlist ();
1140 p.Add (MSymbol.symbol, Mmarker);
1141 p.Add (MSymbol.symbol, sym);
1142 pl.Set (MSymbol.plist, p);
1145 else if (pl.Symbol == Mpushback)
1149 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1152 else if (pl.IsMText)
1154 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1155 pl.Push (MSymbol.symbol, Mcandidates);
1158 else if (plist.IsSymbol)
1160 MSymbol sym = plist.Symbol;
1162 if (sym.Name.Length >= 3
1163 && sym.Name[0] == '@'
1164 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1166 int pos = int.Parse (sym.Name.Substring (1));
1167 MPlist p = new MPlist ();
1171 p.Add (MSymbol.symbol, Msurrounding_text_p);
1175 if (sym.Name[1] == '+')
1177 p.Add (MSymbol.symbol, Mchar_at);
1178 p.Add (MSymbol.integer, pos);
1180 plist.Set (MSymbol.plist, p);
1186 private static MText parse_description (MPlist plist)
1192 plist = plist.Plist;
1193 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1194 && plist.next.IsMText)
1195 return plist.next.Text;
1200 private static MText parse_description (XmlNode node)
1202 if (node.HasChildNodes)
1203 node = node.FirstChild;
1204 return node.InnerText;
1207 private static MText parse_title (XmlNode node)
1209 return node.InnerText;
1212 private void new_variable (Xex.Symbol name, string desc, int val,
1213 MPlist pl, Xex.Variable vari)
1221 range = new int[pl.Count * 2];
1222 for (int i = 0; i < range.Length; i++)
1226 MPlist p = pl.Plist;
1228 if (! p.IsInteger || ! p.next.IsInteger)
1229 throw new Exception ("Invalid range: " + p);
1230 range[i * 2] = p.Integer;
1231 range[i * 2 + 1] = p.next.Integer;
1233 else if (pl.IsInteger)
1234 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1236 throw new Exception ("Invalid range: " + pl);
1240 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1243 Xex.Term term = new Xex.Term (val);
1245 vari.DefaultValue = term;
1250 private void new_variable (Xex.Symbol name, string desc, MText val,
1251 MPlist pl, Xex.Variable vari)
1259 range = new string[pl.Count * 2];
1260 for (int i = 0; i < range.Length; i++)
1263 range[i] = (string) pl.Text;
1265 throw new Exception ("Invalid range: " + pl);
1269 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1272 Xex.Term term = new Xex.Term ((string) val);
1274 vari.DefaultValue = term;
1279 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1280 MPlist pl, Xex.Variable vari)
1283 Xex.Symbol sym = val.Name;
1289 range = new Xex.Symbol[pl.Count * 2];
1290 for (int i = 0; i < range.Length; i++)
1293 range[i] = pl.Symbol.Name;
1295 throw new Exception ("Invalid range: " + pl);
1299 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1302 Xex.Term term = new Xex.Term (sym);
1304 vari.DefaultValue = term;
1309 private Xex.Variable get_global_var (Xex.Symbol name)
1311 if (im_global == null || this != im_global)
1313 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1315 im_global = im_table[tag];
1316 if (! im_global.Open ())
1317 throw new Exception ("Failed to load global");
1319 return im_global.domain.GetVar (name, false);
1322 private void parse_variables (MPlist plist)
1324 var_names = new Xex.Symbol[plist.Count];
1326 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1328 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1329 throw new Exception ("Invalid variable: " + plist);
1331 MPlist p = plist.Plist;
1332 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1333 var_names[i] = name;
1335 string desc = (string) parse_description (p);
1336 Xex.Variable vari = get_global_var (name);
1338 domain.Defvar (vari);
1344 new_variable (name, desc, p.Integer, p.next, vari);
1346 new_variable (name, desc, p.Text, p.next, vari);
1347 else if (p.IsSymbol)
1348 new_variable (name, desc, p.Symbol, p.next, vari);
1350 throw new Exception ("Invalid variable type: " + p.val);
1355 private void parse_variables (XmlNode node)
1357 XmlNodeList node_list = node.ChildNodes;
1359 var_names = new Xex.Symbol[node_list.Count];
1360 for (int i = 0; i < node_list.Count; i++)
1362 Xex.Symbol name = node_list[i].Attributes[0].Value;
1363 Xex.Variable vari = get_global_var (name);
1365 domain.Defvar (vari);
1367 domain.Defvar (node_list[i]);
1369 var_names[i] = name;
1373 private void parse_commands (MPlist plist)
1375 commands = new Command[plist.Count];
1377 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1378 if (plist.IsPlist && plist.Plist.IsSymbol)
1379 commands[i++] = new Command (plist.Plist);
1382 private void parse_commands (XmlNode node)
1384 XmlNodeList node_list = node.ChildNodes;
1386 commands = new Command[node_list.Count];
1387 for (int i = 0; i < node_list.Count; i++)
1389 if (node_list[i].NodeType == XmlNodeType.Element)
1390 commands[i] = new Command (node_list[i]);
1394 private void parse_plugins (MPlist plist)
1396 plugins = new Dictionary<MSymbol, Plugin> ();
1398 for (; ! plist.IsEmpty; plist = plist.Next)
1400 MPlist p = plist.Plist;
1401 MSymbol sym = p.Symbol;
1402 Plugin plugin = new Plugin (sym.Name);
1404 for (p = p.next; ! p.IsEmpty; p = p.next)
1406 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1407 domain.Defun (func);
1412 private void parse_plugins (XmlNode node)
1414 plugins = new Dictionary<MSymbol, Plugin> ();
1416 foreach (XmlNode n in node.ChildNodes)
1418 Plugin plugin = new Plugin (n.Attributes[0].Value);
1419 foreach (XmlNode nn in n.ChildNodes)
1421 Xex.Function func = new PluginMethod (plugin,
1422 nn.Attributes[0].Value);
1423 domain.Defun (func);
1428 private void parse_macros (XmlNode node)
1430 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1431 if (nn.NodeType == XmlNodeType.Element)
1432 domain.Defun (nn, true);
1433 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1434 if (nn.NodeType == XmlNodeType.Element)
1435 domain.Defun (nn, false);
1438 private void parse_maps (XmlNode node)
1442 private void parse_states (XmlNode node)
1446 private void parse_include (MPlist plist)
1448 if (! plist.IsPlist)
1450 MPlist p = plist.Plist;
1451 MSymbol language, name, subname;
1452 language = p.Symbol;
1455 name = subname = MSymbol.nil;
1461 subname = MSymbol.nil;
1466 MInputMethod im = MInputMethod.Find (language, name, subname);
1472 if (! plist.IsSymbol)
1474 MSymbol target_type = plist.Symbol;
1476 MSymbol target_name = MSymbol.nil;
1478 target_name = plist.Symbol;
1479 if (target_type == Mmacro)
1481 if (target_name == MSymbol.nil)
1482 im.domain.CopyFunc (domain);
1484 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1486 else if (target_type == Mmap)
1488 if (target_name == MSymbol.nil)
1490 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1491 maps[kv.Key] = kv.Value;
1496 if (im.maps.TryGetValue (target_name, out map))
1497 maps[target_name] = map;
1500 else if (target_type == Mstate)
1502 if (target_name == MSymbol.nil)
1504 for (p = im.states; ! p.IsEmpty; p = p.next)
1505 states.Add (p.key, p.val);
1509 object state = im.states.Get (target_name);
1511 states.Add (target_name, state);
1516 private Xex.Term parse_cond (MPlist plist)
1518 Xex.Term[] args = new Xex.Term[plist.Count];
1520 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1522 if (! plist.IsPlist)
1523 throw new Exception ("Invalid cond args: " + plist);
1524 MPlist p = plist.Plist;
1525 List<Xex.Term> arg = new List<Xex.Term> (parse_action (p));
1526 args[i] = new Xex.Term (arg);
1528 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1531 private Xex.Term[] parse_action (MPlist plist)
1533 Xex.Term[] terms = new Xex.Term[plist.Count];
1535 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1538 terms[i] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1539 else if (plist.IsMText)
1540 terms[i] = new Xex.Term ((string) plist.Text);
1541 else if (plist.IsInteger)
1542 terms[i] = new Xex.Term (plist.Integer);
1543 else if (plist.IsPlist)
1545 MPlist p = plist.Plist;
1547 throw new Exception ("Invalid action: " + p);
1548 MSymbol name = p.Symbol;
1551 terms[i] = parse_cond (p);
1552 else if (name == Mset || name == Madd || name == Msub
1553 || name == Mmul || name == Mdiv)
1556 throw new Exception ("Invalid action: " + p);
1557 Xex.Symbol varname = p.Symbol.Name;
1558 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1559 varname, parse_action (p.next));
1562 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1566 throw new Exception ("Invalid action: " + plist);
1572 private void parse_macros (MPlist plist)
1574 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1577 MPlist p = pl.Plist;
1581 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
1583 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1586 MPlist p = pl.Plist;
1591 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
1592 parse_action (p.next), false);
1596 private void parse_maps (MPlist plist)
1598 for (; ! plist.IsEmpty; plist = plist.next)
1601 MPlist pl = plist.Plist;
1605 Map map = new Map ();
1606 map.name = pl.Symbol;
1607 maps[map.name] = map;
1608 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1612 MPlist p = pl.Plist;
1615 keys = new KeySeq (p.Text);
1617 keys = new KeySeq (p.Plist);
1624 new Xex.Term (domain, Nprogn, parse_action (p)));
1629 private void parse_states (MPlist plist)
1631 for (; ! plist.IsEmpty; plist = plist.next)
1634 MPlist pl = plist.Plist;
1645 State state = new State (pl.Symbol);
1646 state.title = title;
1648 states = new MPlist ();
1649 states.Add (state.name, state);
1650 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1654 MPlist p = pl.Plist;
1657 MSymbol map_name = p.Symbol;
1659 Xex.Term term = new Xex.Term (domain, Nprogn, parse_action (p));
1660 state.branches.Add (map_name, term);
1665 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1668 ((MInputContext) domain.context).insert (args[0]);
1672 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1676 ((MInputContext) domain.context).insert_candidates (args[0]);
1680 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1683 MSymbol name = (string) args[0].Symval;
1684 return new Xex.Term (Marker.Get ((MInputContext) domain.context, name));
1687 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1690 int c = ((MInputContext) domain.context).char_at (args[0].Intval);
1691 return new Xex.Term (c);
1694 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1697 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1701 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1704 MInputContext ic = (MInputContext) domain.context;
1707 ic.select (args[0].Intval);
1709 ic.select ((MSymbol) ((string) args[0].Symval));
1713 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1716 ((MInputContext) domain.context).show ();
1720 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1723 ((MInputContext) domain.context).hide ();
1727 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1731 ((MInputContext) domain.context).move (args[0].Intval);
1734 Marker m = (Marker) args[0].Objval;
1735 MInputContext ic = (MInputContext) domain.context;
1736 ((MInputContext) domain.context).move (m.Position (ic));
1741 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1744 Marker m = (Marker) args[0].Objval;
1745 m.Mark ((MInputContext) domain.context);
1749 private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
1752 return new Xex.Term (new KeySeq (args));
1755 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1758 MInputContext ic = (MInputContext) domain.context;
1761 ic.pushback (args[0].Intval);
1762 else if (args[0].IsStr)
1763 ic.pushback (new KeySeq (args[0].Strval));
1765 ic.pushback ((KeySeq) args[0].Objval);
1769 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1772 ((MInputContext) domain.context).pop ();
1776 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1779 int n = args.Length == 0 ? -2 : args[0].Intval;
1780 ((MInputContext) domain.context).undo (n);
1784 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1787 ((MInputContext) domain.context).commit ();
1791 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1794 ((MInputContext) domain.context).commit ();
1795 args = new Xex.Term[2];
1796 args[0] = args[1] = Tcatch_tag;
1797 return Xex.Fthrow (domain, vari, args);
1800 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1803 ((MInputContext) domain.context).shift (args[0].Symval);
1807 public override string ToString ()
1809 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1810 if (commands != null)
1812 str += " (commands";
1813 foreach (Command cmd in commands)
1817 if (var_names != null)
1819 str += " (variables";
1820 foreach (Xex.Symbol var in var_names)
1824 if (plugins != null)
1827 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1828 str += " " + kv.Value;
1832 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1833 str += " " + kv.Value;
1835 foreach (MPlist p in states)
1841 public class MInputContext
1843 internal static Xex.Symbol Ncandidates_group_size = "candidates-group-size";
1844 private static MSymbol Mat_less_than = "@<";
1845 private static MSymbol Mat_greater_than = "@>";
1846 private static MSymbol Mat_minus = "@-";
1847 private static MSymbol Mat_plus = "@+";
1848 private static MSymbol Mat_open_square_bracket = "@[";
1849 private static MSymbol Mat_close_square_bracket = "@]";
1851 public MInputMethod im;
1852 private MText produced;
1853 private bool active;
1854 private MText status;
1855 internal MText preedit;
1856 internal int cursor_pos;
1857 private Candidates candidates;
1858 private MPlist candidate_group;
1859 private int candidate_index;
1860 private int candidate_from, candidate_to;
1861 private bool candidate_show;
1863 private Stack<MInputMethod.State> states;
1864 internal MInputMethod.KeySeq keys;
1865 private int key_head;
1866 private int state_key_head;
1867 private object state_var_values;
1868 private int commit_key_head;
1869 private MText state_preedit;
1870 private int state_pos;
1871 internal MPlist markers = new MPlist ();
1872 internal MText preceding_text = new MText ();
1873 internal MText following_text = new MText ();
1874 private bool key_unhandled;
1876 internal Xex.Domain domain;
1879 public enum ChangedStatus
1885 CandidateList = 0x08,
1886 CandidateIndex = 0x10,
1887 CandidateShow = 0x20,
1890 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
1891 | ChangedStatus.CandidateIndex
1892 | ChangedStatus.CandidateShow);
1893 private ChangedStatus changed;
1895 public ChangedStatus Changed { get { return changed; } }
1897 public MInputContext (MInputMethod im)
1900 domain = new Xex.Domain (im.domain, this);
1901 states = new Stack<MInputMethod.State> ();
1902 states.Push ((MInputMethod.State) im.states.val);
1903 keys = new MInputMethod.KeySeq ();
1906 private void adjust_markers (int from, int to, object inserted)
1908 int ins = (inserted == null ? 0
1909 : inserted is int ? 1
1910 : ((MText) inserted).Length);
1911 int diff = ins - (to - from);
1913 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1915 int pos = plist.Integer;
1919 plist.val = pos + diff;
1924 if (cursor_pos >= to)
1926 else if (cursor_pos > from)
1930 private void preedit_replace (int from, int to, int c)
1932 preedit.Del (from, to);
1933 preedit.Ins (from, c);
1934 adjust_markers (from, to, c);
1937 private void preedit_replace (int from, int to, MText mt)
1939 preedit[from, to] = mt;
1940 adjust_markers (from, to, mt);
1943 internal void insert (Xex.Term arg)
1946 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1948 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1949 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
1952 private class Candidates
1959 public Block (int index, Xex.Term term)
1963 Data = (MText) term.Strval;
1966 MPlist plist = new MPlist ();
1968 foreach (Xex.Term t in term.Listval)
1969 p = p.Add (MSymbol.mtext, (MText) t.Strval);
1974 public Block (int index, MPlist plist)
1979 else if (plist.IsPlist)
1982 throw new Exception ("Invalid candidate: " + plist);
1987 get { return (Data is MText
1988 ? ((MText) Data).Length
1989 : ((MPlist) Data).Count); }
1992 public object this[int i]
1995 if (Data is MText) return ((MText) Data)[i];
1996 return ((MPlist) Data)[i];
2001 private Block[] blocks;
2002 private int row = 0;
2003 private int index = 0;
2004 public object[] group;
2006 private bool IsFixed { get { return group != null; } }
2009 Block last = blocks[blocks.Length - 1];
2010 return last.Index + last.Count; }
2014 get { return (IsFixed ? index % group.Length
2015 : index - blocks[row].Index); }
2018 public object Group {
2019 get { return (IsFixed ? group : blocks[row].Data); }
2022 public int GroupLength
2027 int nitems = group.Length;
2028 int start = index - (index % nitems);
2030 return (start + nitems <= total ? nitems : total - start);
2032 return blocks[row].Count;
2036 public object Current {
2038 return (IsFixed ? group[index % group.Length]
2039 : blocks[row][index - blocks[row].Index]);
2043 public Candidates (MPlist list, int column)
2045 int nblocks = list.Count;
2047 blocks = new Block[nblocks];
2048 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
2049 start += (blocks[i] = new Block (index, list)).Count;
2051 group = new object[column];
2054 public Candidates (List<Xex.Term> list, int column)
2056 int nblocks = list.Count;
2058 blocks = new Block[nblocks];
2059 for (int i = 0, start = 0; i < nblocks; i++)
2060 start += (blocks[i] = new Block (index, list[i])).Count;
2062 group = new object[column];
2065 public static void Detach (MInputContext ic)
2067 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
2068 ic.candidates = null;
2069 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2073 // Fill the array "group" by candidates stating from INDEX.
2074 // INDEX must be a multiple of "column". Set NTIMES to the
2075 // number of valid candidates in "group". Update "block" if
2076 // necessary. Return "group".
2078 private int fill_group (int start)
2080 int nitems = group.Length;
2082 Block b = blocks[r];
2084 if (start < b.Index)
2085 while (start < b.Index)
2088 while (start >= b.Index + b.Count)
2092 int count = b.Count;
2094 for (int i = 0; i < nitems; i++, start++)
2099 if (r == blocks.Length)
2105 group[i] = b[start];
2110 // Update "row" to what contains the first candidate of
2111 // the previous candidate-group, update "current_index", and
2112 // update "group" if necessary. Return the previous
2113 // candidate-group. Set NITEMS to the number of valid
2114 // candidates contained in that group.
2116 public int PrevGroup ()
2123 nitems = group.Length;
2124 if ((index -= col + nitems) < 0)
2125 index = (Total / nitems) * nitems;
2126 nitems = fill_group (index);
2130 row = row > 0 ? row-- : blocks.Length - 1;
2131 nitems = blocks[row].Count;
2132 index = blocks[row].Index;
2134 index += col < nitems ? col : nitems - 1;
2138 public int NextGroup ()
2145 nitems = group.Length;
2146 if ((index += nitems - col) >= Total)
2148 nitems = fill_group (index);
2152 row = row < blocks.Length - 1 ? row + 1 : 0;
2153 nitems = blocks[row].Count;
2154 index = blocks[row].Count;
2156 index += col < nitems ? col : nitems - 1;
2166 int nitems = PrevGroup ();
2167 index += col < nitems - 1 ? col : nitems - 1;
2176 int nitems = GroupLength;
2178 if (col == nitems - 1)
2180 nitems = NextGroup ();
2187 public void First ()
2194 index += GroupLength - (Column + 1);
2197 public void Select (int col)
2199 int maxcol = GroupLength - 1;
2202 index = index - Column + col;
2206 private void update_candidate ()
2208 object candidate = candidates.Current;
2210 if (candidate is MText)
2212 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2213 candidate_to = candidate_from + ((MText) candidate).Length;
2217 preedit_replace (candidate_from, candidate_to, (int) candidate);
2218 candidate_to = candidate_from + 1;
2220 preedit.PushProp (candidate_from, candidate_to,
2221 MInputMethod.Mcandidates, this);
2222 cursor_pos = candidate_from;
2223 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2227 internal void insert_candidates (Xex.Term arg)
2230 Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
2233 column = v.Value.Intval;
2234 candidates = new Candidates (arg.Listval, column);
2235 candidate_from = candidate_to = cursor_pos;
2236 update_candidate ();
2239 internal void select (int n)
2241 if (candidates != null)
2243 candidates.Select (n);
2244 update_candidate ();
2248 internal void select (MSymbol sym)
2250 if (candidates != null)
2252 if (sym == Mat_less_than)
2253 candidates.First ();
2254 else if (sym == Mat_greater_than)
2256 else if (sym == Mat_minus)
2258 else if (sym == Mat_plus)
2260 else if (sym == Mat_open_square_bracket)
2261 candidates.PrevGroup ();
2262 else if (sym == Mat_close_square_bracket)
2263 candidates.NextGroup ();
2267 internal int marker (MSymbol sym)
2269 int pos = cursor_pos;
2271 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2273 switch (sym.Name[0])
2275 case '<': pos = 0; break;
2276 case '>': pos = preedit.Length; break;
2277 case '-': pos = cursor_pos - 1; break;
2278 case '+': pos = cursor_pos + 1; break;
2283 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2290 if (cursor_pos < preedit.Length - 1)
2293 preedit.FindProp (MInputMethod.Mcandidates, pos,
2297 pos = preedit.Length;
2300 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2305 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2307 pos = int.Parse (sym.Name.Substring (2));
2311 object val = markers.Get (sym);
2319 internal int char_at (int pos)
2326 if (preceding_text.Length < -pos)
2328 MPlist plist = new MPlist ();
2329 plist.Push (MSymbol.integer, pos);
2330 if (MInputMethod.GetSurroundingText != null
2331 && MInputMethod.GetSurroundingText (this, plist)
2333 && preceding_text.Length < plist.Text.Length)
2334 preceding_text = plist.Text;
2336 c = (-pos < preceding_text.Length
2337 ? preceding_text[preceding_text.Length + pos] : -1);
2339 else if (pos >= 0 && pos < preedit.Length)
2343 pos -= preedit.Length;
2344 if (pos >= following_text.Length)
2346 MPlist plist = new MPlist ();
2347 plist.Push (MSymbol.integer, pos + 1);
2348 if (MInputMethod.GetSurroundingText != null
2349 && MInputMethod.GetSurroundingText (this, plist)
2351 && following_text.Length < plist.Text.Length)
2352 following_text = plist.Text;
2354 c = (pos < following_text.Length ? following_text[pos] : -1);
2359 internal void delete (int pos)
2361 if (pos < cursor_pos)
2362 preedit_replace (pos, cursor_pos, null);
2364 preedit_replace (cursor_pos, pos, null);
2365 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2368 internal void show ()
2370 candidate_show = true;
2371 changed |= ChangedStatus.CandidateShow;
2374 internal void hide ()
2376 candidate_show = false;
2377 changed |= ChangedStatus.CandidateShow;
2380 internal void move (int pos)
2384 else if (pos > preedit.Length)
2385 pos = preedit.Length;
2386 if (pos != cursor_pos)
2389 changed |= ChangedStatus.Preedit;
2393 internal void mark (MSymbol sym)
2395 MPlist slot = markers.Find (sym);
2398 markers.Push (sym, cursor_pos);
2400 slot.val = cursor_pos;
2403 internal void pushback (int n)
2416 if (key_head > keys.keyseq.Count)
2417 key_head = keys.keyseq.Count;
2421 internal void pushback (MInputMethod.KeySeq keyseq)
2425 if (key_head < keys.keyseq.Count)
2426 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2427 for (int i = 0; i < keyseq.keyseq.Count; i++)
2428 keys.keyseq.Add (keyseq.keyseq[i]);
2431 internal void pop ()
2433 if (key_head < keys.keyseq.Count)
2434 keys.keyseq.RemoveRange (key_head, 1);
2437 internal void undo (int n)
2440 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2442 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2446 internal void commit ()
2448 produced.Cat (preedit);
2450 changed |= ChangedStatus.Preedit;
2453 internal void shift (MSymbol sym)
2455 MInputMethod.State state;
2457 if (sym == MSymbol.t)
2459 if (states.Count > 1)
2460 state = states.Pop ();
2462 state = states.Peek ();
2466 state = (MInputMethod.State) im.states.Get (sym);
2468 throw new Exception ("Unknown state: " + state.name);
2471 state = states.Pop ();
2472 if (state == (MInputMethod.State) im.states.val)
2479 state_key_head = key_head;
2480 state_pos = cursor_pos;
2481 state_preedit = preedit.Dup ();
2482 if (state != states.Peek ())
2484 states.Push (state);
2485 state_var_values = domain.SaveValues ();
2486 status = state.title;
2489 changed |= ChangedStatus.StateTitle;
2491 = (Xex) state.branches.Get (MSymbol.t);
2492 if (on_entry != null)
2493 on_entry.Eval (domain);
2498 internal void reset ()
2501 state_preedit.Del ();
2505 key_head = commit_key_head = 0;
2507 states.Push ((MInputMethod.State) im.states.Val);
2512 internal object GetCandidates (out int column)
2515 if (cursor_pos == 0)
2517 Candidates candidates
2518 = (Candidates) preedit.GetProp (cursor_pos - 1,
2519 MInputMethod.Mcandidates);
2520 if (candidates == null)
2522 column = candidates.Column;
2523 return candidates.Current;
2526 internal void HandleKey ()
2530 public bool Toggle ()