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;
166 public Key (uint c) { key = c; }
167 public Key (int c) { key = (uint) c; }
169 public Key (uint c, KeyModifier modifiers)
171 key = combine_modifiers (c, modifiers);
174 public Key (MSymbol keysym, KeyModifier modifiers)
176 key = combine_modifiers (decode_keysym (keysym), modifiers);
179 public Key (MSymbol keysym)
181 string str = keysym.Name;
182 int len = str.Length;
184 KeyModifier modifiers = KeyModifier.None;
186 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
189 modifiers |= KeyModifier.Shift;
190 else if (str[i] == 'C')
191 modifiers |= KeyModifier.Control;
192 else if (str[i] == 'A')
193 modifiers |= KeyModifier.Alt;
194 else if (str[i] == 'G')
195 modifiers |= KeyModifier.AltGr;
196 else if (str[i] == 's')
197 modifiers |= KeyModifier.Super;
198 else if (str[i] == 'H')
199 modifiers |= KeyModifier.Hyper;
202 key = combine_modifiers (str[i], modifiers);
204 key = combine_modifiers (decode_keysym (keysym), modifiers);
207 public Key (MPlist plist)
209 KeyModifier modifiers = KeyModifier.None;
212 for (p = plist; ! p.IsEmpty; p = p.next)
216 if (! p.next.IsEmpty)
217 throw new Exception ("Invalid Key: " + plist);
220 else if (! p.IsSymbol)
221 throw new Exception ("Invalid Key: " + plist);
224 string name = p.Symbol.Name.ToLower ();
227 if (! keymodifiers.TryGetValue (name, out m))
232 if (p.IsEmpty || ! p.next.IsEmpty)
233 throw new Exception ("Invalid Key: " + plist);
235 key = combine_modifiers ((uint) p.Integer, modifiers);
237 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
240 public bool HasModifier
242 get { return ((key & (uint) KeyModifier.All) != 0); }
245 public bool Match (Key k)
249 if ((k.key & char_mask) != (key & char_mask))
251 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
252 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
253 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
254 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
255 && (m2 & KeyModifier.Shift) != KeyModifier.None))
256 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
257 || ((m1 & KeyModifier.Control) == KeyModifier.Control
258 && (m2 & KeyModifier.Control) != KeyModifier.None))
259 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
260 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
261 && (m2 & KeyModifier.Alt) != KeyModifier.None))
262 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
265 public override string ToString ()
267 string str = Char.ToString ((char) key);
268 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
270 if (m != KeyModifier.None)
272 if ((m & KeyModifier.Shift) != KeyModifier.None)
274 if ((m & KeyModifier.Control) != KeyModifier.None)
276 if ((m & KeyModifier.Alt) != KeyModifier.None)
278 if ((m & KeyModifier.AltGr) != KeyModifier.None)
280 if ((m & KeyModifier.Super) != KeyModifier.None)
282 if ((m & KeyModifier.Hyper) != KeyModifier.None)
289 public class KeySeq : Xex.TermValue
291 public List<Key> keyseq = new List<Key> ();
293 public override Xex.TermValue Clone ()
295 KeySeq ks = new KeySeq ();
296 ks.keyseq.InsertRange (0, keyseq);
302 public KeySeq (MPlist plist)
304 foreach (MPlist p in plist)
307 keyseq.Add (new Key (p.Symbol));
308 else if (p.IsInteger)
309 keyseq.Add (new Key ((char) p.Integer));
311 keyseq.Add (new Key (p.Plist));
313 throw new Exception ("Invalid Key Sequence: " + plist);
317 public KeySeq (MText mt) : base ()
319 for (int i = 0; i < mt.Length; i++)
320 keyseq.Add (new Key ((uint) mt[i]));
323 private static uint parse_integer (string str)
325 if (Char.IsDigit (str[0]))
327 if (str[0] == '0' && str.Length > 2 && str[1] == 'x')
330 for (int idx = 2; idx < str.Length; idx++)
333 if (c >= '0' && c <= '9')
334 i = i * 16 + (c - '0');
335 else if (c >= 'A' && c <= 'F')
336 i = i * 16 + 10 + (c - 'A');
337 else if (c >= 'a' && c <= 'f')
338 i = i * 16 + 10 + (c - 'a');
344 return UInt32.Parse (str);
346 else if (str[0] == '?')
351 public KeySeq (Xex.Term arg)
355 string str = arg.Strval;
357 for (int i = 0; i < str.Length; i++)
358 keyseq.Add (new Key (str[i]));
362 List<Xex.Term> list = arg.Listval;
363 int len = list.Count;
365 for (int i = 0; i < len; i++)
368 keyseq.Add (new Key (list[i].Intval));
369 else if (list[i].IsStr)
370 keyseq.Add (new Key (list[i].Strval));
371 else if (list[i].IsSymbol)
372 keyseq.Add (new Key ((string) list[i].Symval));
374 throw new Exception ("Invalid key: " + list[i]);
379 public static Xex.TermValue New (Xex.Domain domain, XmlNode node)
381 return new KeySeq (new Xex.Term (domain, node.FirstChild));
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 Console.WriteLine ("cmd:" + p);
510 for (p = p.next; ! p.IsEmpty; p = p.next)
513 keys.Add (new KeySeq (p.Text));
515 keys.Add (new KeySeq (p.Plist));
519 public Command (XmlNode node)
521 name = node.Attributes[0].Value;
522 keys = new List<KeySeq> ();
523 for (node = node.FirstChild; node != null; node = node.NextSibling)
525 if (node.Name == "description")
526 description = parse_description (node);
527 else if (node.Name == "keyseq")
529 KeySeq ks = MInputMethod.KeySeq.New (domain, node);
535 public override string ToString ()
537 string str = "(" + name + " \"" + (string) description;
538 foreach (KeySeq keyseq in keys)
544 internal class Plugin
547 private Assembly assembly;
548 private Type plugin_type;
550 public Plugin (string name)
555 public MethodInfo GetMethod (Xex.Symbol name)
557 if (assembly == null)
559 assembly = Assembly.LoadFrom (name + ".dll");
560 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
563 MethodInfo info = plugin_type.GetMethod ((string) name);
565 throw new Exception ("Invalid plugin method: " + name);
569 public override string ToString ()
571 return String.Format ("(module {0}", name);
575 internal class PluginMethod : Xex.Function
577 private Plugin plugin;
578 private MethodInfo method_info;
579 object[] parameters = new object[2];
581 public PluginMethod (Plugin plugin, string name)
582 : base ((Xex.Symbol) name, 0, -1)
584 this.plugin = plugin;
587 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
590 args = (Xex.Term[]) args.Clone ();
591 for (int i = 0; i < args.Length; i++)
593 args[i] = args[i].Eval (domain);
597 if (method_info == null)
598 method_info = plugin.GetMethod (name);
599 parameters[0] = domain.context;
600 parameters[1] = args;
601 return (Xex.Term) method_info.Invoke (null, parameters);
605 internal abstract class Marker : Xex.TermValue
609 public Marker (MSymbol name)
614 public abstract int Position (MInputContext ic);
615 public abstract void Mark (MInputContext ic);
617 public class Named : Marker
621 public Named (MSymbol name) : this (name, 0) { }
623 private Named (MSymbol name, int p) : base (name) { pos = p; }
625 public override int Position (MInputContext ic) { return pos; }
627 public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
629 public override Xex.TermValue Clone ()
631 return new Named (name, pos);
635 public class Predefined : Marker
637 public Predefined (MSymbol name) : base (name) { }
639 public override int Position (MInputContext ic)
641 switch (name.Name[1]) {
643 case '>': return ic.preedit.Length;
644 case '-': return ic.cursor_pos - 1;
645 case '+': return ic.cursor_pos + 1;
647 if (ic.cursor_pos > 0)
649 int pos = ic.cursor_pos;
651 ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
657 if (ic.cursor_pos < ic.preedit.Length - 1)
659 int pos = ic.cursor_pos;
661 ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
665 return ic.preedit.Length;
667 return name.Name[1] - '0';
671 public override void Mark (MInputContext ic)
673 throw new Exception ("Can't set predefined marker: " + name);
676 public override Xex.TermValue Clone ()
678 return new Predefined (name);
682 static internal Dictionary<MSymbol,Predefined> predefined_markers;
686 predefined_markers = new Dictionary<MSymbol,Predefined> ();
687 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
688 "@0", "@1", "@2", "@3", "@4",
689 "@5", "@6", "@7", "@8", "@9" };
690 foreach (MSymbol s in symlist)
691 predefined_markers[s] = new Predefined (s);
694 public static Marker Get (MInputContext ic, MSymbol name)
699 if (predefined_markers.TryGetValue (name, out pred))
701 if (name.Name[0] == '@')
702 throw new Exception ("Invalid marker name: " + name);
703 m = (Marker) ic.markers.Get (name);
706 m = new Named (name);
707 ic.markers.Put (name, m);
716 public Dictionary<Key, Map> submaps;
717 public Xex.Term actions;
719 public void Add (KeySeq keys, int index, Xex.Term actions)
724 submaps = new Dictionary<Key, Map> ();
726 submaps.TryGetValue (keys.keyseq[index], out sub);
729 Key key = keys.keyseq[index];
730 submaps[key] = sub = new Map ();
732 if (index + 1 < keys.keyseq.Count)
733 sub.Add (keys, index + 1, actions);
735 this.actions = actions;
738 public Xex.Term Lookup (KeySeq keys, int index)
742 if (index + 1 == keys.keyseq.Count)
744 if (submaps.TryGetValue (keys.keyseq[index], out sub))
745 return sub.Lookup (keys, index + 1);
749 private void describe (MText mt, KeySeq keyseq)
751 if (keyseq.keyseq.Count > 0)
753 mt.Cat (" (").Cat (keyseq.ToString ());
754 mt.Cat (' ').Cat (actions.ToString ());
758 foreach (KeyValuePair<Key, Map> kv in submaps)
760 keyseq.keyseq.Add (kv.Key);
761 kv.Value.describe (mt, keyseq);
762 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
766 public override string ToString ()
768 MText mt = "(" + name.Name;
769 KeySeq keyseq = new KeySeq ();
771 describe (mt, keyseq);
781 public MPlist branches = new MPlist ();
783 public State (MSymbol name)
788 public override string ToString ()
790 MText mt = "(" + name.Name;
793 mt.Cat (" \"" + title + "\"");
794 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
795 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
796 return (string) mt + ")";
801 internal Xex.Domain domain = new Xex.Domain (im_domain, null);
803 private LoadStatus load_status = LoadStatus.None;
804 private MDatabase.Tag tag;
805 private MDatabase mdb;
807 private MText description;
808 internal MText title;
809 internal Command[] commands;
810 internal Xex.Symbol[] var_names;
811 internal Dictionary<MSymbol, Plugin> plugins;
812 internal Dictionary<MSymbol, Map> maps;
813 internal MPlist states;
815 static MInputMethod ()
817 im_domain.DefType ("keyseq", KeySeq.New);
819 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
820 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
821 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
822 im_domain.DefSubr (Fselect, "select", true, 1, 1);
823 im_domain.DefSubr (Fshow, "show", true, 0, 0);
824 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
825 im_domain.DefSubr (Fmove, "move", true, 1, 1);
826 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
827 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
828 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
829 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
830 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
831 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
832 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
833 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
834 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
836 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
837 List<MDatabase> list = MDatabase.List (tag);
838 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
839 foreach (MDatabase mdb in list)
840 im_table[mdb.tag] = new MInputMethod (mdb.tag);
844 private MInputMethod (MDatabase.Tag tag)
849 // Instance Properties
850 public MSymbol Language { get { return tag[1]; } }
851 public MSymbol Name { get { return tag[2]; } }
852 public MSymbol SubName { get { return tag[3]; } }
854 public bool Info (out MText description,
856 out Xex.Variable[] variables,
857 out Command[] commands)
859 if ((load_status & LoadStatus.Header) != LoadStatus.Header
868 description = this.description;
870 if (var_names == null)
874 variables = new Xex.Variable[var_names.Length];
876 foreach (Xex.Symbol name in var_names)
877 variables[i++] = domain.GetVar (name, false);
879 commands = this.commands;
883 public static MInputMethod Find (MSymbol language, MSymbol name)
885 return Find (language, name, MSymbol.nil);
888 public static MInputMethod Find (MSymbol language, MSymbol name,
891 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
895 return (im_table.TryGetValue (tag, out im) ? im : null);
900 return ((load_status == LoadStatus.Full) || load_body ());
903 public static MInputMethod[] List ()
905 MInputMethod[] array = new MInputMethod[im_table.Count];
908 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
909 array[i++] = kv.Value;
913 private bool load_header ()
915 mdb = MDatabase.Find (tag);
918 mdb.name_table = Xex.Symbol.Table;
920 MSymbol format = mdb.Format;
922 if (format == MSymbol.plist)
923 load ((MPlist) mdb.Load (Mmap), false);
926 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
927 load (doc.DocumentElement, false);
929 } catch (Exception e) {
930 Console.WriteLine ("{0}\n", e);
931 load_status = LoadStatus.Error;
934 load_status |= LoadStatus.Header;
938 private bool load_body ()
940 mdb = MDatabase.Find (tag);
943 mdb.name_table = Xex.Symbol.Table;
945 object obj = mdb.Load ();
947 load ((MPlist) obj, true);
949 load ((XmlDocument) obj, true);
950 } catch (Exception e) {
951 Console.WriteLine (e);
952 load_status = LoadStatus.Error;
955 load_status = LoadStatus.Full;
959 private void load (MPlist plist, bool full)
961 maps = new Dictionary<MSymbol, Map> ();
962 states = new MPlist ();
964 for (; ! plist.IsEmpty; plist = plist.next)
967 MPlist pl = plist.Plist;
970 MSymbol sym = pl.Symbol;
973 if (sym == Mdescription)
974 description = parse_description (pl);
975 else if (sym == Mtitle)
980 else if (sym == Mvariable)
981 parse_variables (pl);
982 else if (sym == Mcommand)
988 else if (sym == Minclude)
990 else if (sym == Mmacro)
992 else if (sym == Mmap)
994 else if (sym == Mstate)
999 if (description == null)
1000 description = (MText) "No description";
1002 title = new MText (tag[2].Name);
1003 if (commands == null)
1004 commands = new Command[0];
1009 State state = new State ((MSymbol) "init");
1010 plist = new MPlist ();
1011 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1012 state.branches.Add (kv.Key, null);
1013 states.Add (state.name, state);
1017 private void load (XmlNode node, bool full)
1019 bool skip_header = load_status == LoadStatus.Header;
1021 maps = new Dictionary<MSymbol, Map> ();
1022 states = new MPlist ();
1024 if (node.NodeType == XmlNodeType.Document)
1025 node = node.FirstChild;
1026 while (node.NodeType != XmlNodeType.Element)
1027 node = node.NextSibling;
1028 for (node = node.FirstChild; node != null; node = node.NextSibling)
1030 if (node.NodeType != XmlNodeType.Element)
1034 if (node.Name == "description")
1035 description = parse_description (node);
1036 else if (node.Name == "title")
1037 title = parse_title (node);
1038 else if (node.Name == "variable-list")
1039 parse_variables (node);
1040 else if (node.Name == "command-list")
1041 parse_commands (node);
1045 if (node.Name == "module-list")
1046 parse_plugins (node);
1047 else if (node.Name == "macro-list")
1048 parse_macros (node);
1049 else if (node.Name == "map-list")
1051 else if (node.Name == "state-list")
1052 parse_states (node);
1055 if (description == null)
1056 description = (MText) "No description";
1058 title = new MText (tag[2].Name);
1059 if (commands == null)
1060 commands = new Command[0];
1065 State state = new State ((MSymbol) "init");
1066 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1067 state.branches.Add (kv.Key, null);
1068 states.Add (state.name, state);
1072 private static void transform (MPlist plist)
1074 for (; ! plist.IsEmpty; plist = plist.next)
1078 MPlist p = new MPlist ();
1079 p.Add (MSymbol.symbol, Minsert);
1080 p.Add (MSymbol.mtext, plist.Text);
1081 plist.Set (MSymbol.plist, p);
1083 else if (plist.IsInteger)
1085 MPlist p = new MPlist ();
1086 p.Add (MSymbol.symbol, Minsert);
1087 p.Add (MSymbol.integer, plist.Integer);
1088 plist.Set (MSymbol.plist, p);
1090 else if (plist.IsPlist)
1092 MPlist pl = plist.Plist;
1096 if (pl.Symbol == Madd)
1097 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1098 else if (pl.Symbol == Msub)
1099 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1100 else if (pl.Symbol == Mmul)
1101 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1102 else if (pl.Symbol == Mdiv)
1103 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1104 else if (pl.Symbol == Minsert)
1106 // (insert (CANDIDATES ...))
1107 // => (candidates CANDIDATES ...)
1108 if (pl.next.IsPlist)
1110 pl.Set (MSymbol.symbol, Mcandidates);
1112 MPlist p = pl.Plist;
1113 pl.Set (p.key, p.val);
1114 for (p = p.next; ! p.IsEmpty; p = p.next);
1115 pl.Add (p.key, p.val);
1118 else if (pl.Symbol == Mif)
1122 transform (pl.next);
1124 else if (pl.Symbol == Mcond)
1126 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1129 MPlist p = pl.Plist;
1137 else if (pl.Symbol == Mdelete
1138 || pl.Symbol == Mmove
1139 || pl.Symbol == Mmark)
1144 MSymbol sym = pl.Symbol;
1145 MPlist p = new MPlist ();
1146 p.Add (MSymbol.symbol, Mmarker);
1147 p.Add (MSymbol.symbol, sym);
1148 pl.Set (MSymbol.plist, p);
1151 else if (pl.Symbol == Mpushback)
1155 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1158 else if (pl.IsMText)
1160 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1161 pl.Push (MSymbol.symbol, Mcandidates);
1164 else if (plist.IsSymbol)
1166 MSymbol sym = plist.Symbol;
1168 if (sym.Name.Length >= 3
1169 && sym.Name[0] == '@'
1170 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1172 int pos = int.Parse (sym.Name.Substring (1));
1173 MPlist p = new MPlist ();
1177 p.Add (MSymbol.symbol, Msurrounding_text_p);
1181 if (sym.Name[1] == '+')
1183 p.Add (MSymbol.symbol, Mchar_at);
1184 p.Add (MSymbol.integer, pos);
1186 plist.Set (MSymbol.plist, p);
1192 private static MText parse_description (MPlist plist)
1198 plist = plist.Plist;
1199 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1200 && plist.next.IsMText)
1201 return plist.next.Text;
1206 private static MText parse_description (XmlNode node)
1208 if (node.HasChildNodes)
1209 node = node.FirstChild;
1210 return node.InnerText;
1213 private static MText parse_title (XmlNode node)
1215 return node.InnerText;
1218 private void new_variable (Xex.Symbol name, string desc, int val,
1219 MPlist pl, Xex.Variable vari)
1227 range = new int[pl.Count * 2];
1228 for (int i = 0; i < range.Length; i++)
1232 MPlist p = pl.Plist;
1234 if (! p.IsInteger || ! p.next.IsInteger)
1235 throw new Exception ("Invalid range: " + p);
1236 range[i * 2] = p.Integer;
1237 range[i * 2 + 1] = p.next.Integer;
1239 else if (pl.IsInteger)
1240 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1242 throw new Exception ("Invalid range: " + pl);
1246 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1249 Xex.Term term = new Xex.Term (val);
1251 vari.DefaultValue = term;
1256 private void new_variable (Xex.Symbol name, string desc, MText val,
1257 MPlist pl, Xex.Variable vari)
1265 range = new string[pl.Count * 2];
1266 for (int i = 0; i < range.Length; i++)
1269 range[i] = (string) pl.Text;
1271 throw new Exception ("Invalid range: " + pl);
1275 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1278 Xex.Term term = new Xex.Term ((string) val);
1280 vari.DefaultValue = term;
1285 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1286 MPlist pl, Xex.Variable vari)
1289 Xex.Symbol sym = val.Name;
1295 range = new Xex.Symbol[pl.Count * 2];
1296 for (int i = 0; i < range.Length; i++)
1299 range[i] = pl.Symbol.Name;
1301 throw new Exception ("Invalid range: " + pl);
1305 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1308 Xex.Term term = new Xex.Term (sym);
1310 vari.DefaultValue = term;
1315 private Xex.Variable get_global_var (Xex.Symbol name)
1317 if (im_global == null || this != im_global)
1319 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1321 im_global = im_table[tag];
1322 if (! im_global.Open ())
1323 throw new Exception ("Failed to load global");
1325 return im_global.domain.GetVar (name, false);
1328 private void parse_variables (MPlist plist)
1330 var_names = new Xex.Symbol[plist.Count];
1332 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1334 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1335 throw new Exception ("Invalid variable: " + plist);
1337 MPlist p = plist.Plist;
1338 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1339 var_names[i] = name;
1341 string desc = (string) parse_description (p);
1342 Xex.Variable vari = get_global_var (name);
1344 domain.Defvar (vari);
1350 new_variable (name, desc, p.Integer, p.next, vari);
1352 new_variable (name, desc, p.Text, p.next, vari);
1353 else if (p.IsSymbol)
1354 new_variable (name, desc, p.Symbol, p.next, vari);
1356 throw new Exception ("Invalid variable type: " + p.val);
1361 private void parse_variables (XmlNode node)
1363 XmlNodeList node_list = node.ChildNodes;
1365 var_names = new Xex.Symbol[node_list.Count];
1366 for (int i = 0; i < node_list.Count; i++)
1368 Xex.Symbol name = node_list[i].Attributes[0].Value;
1369 Xex.Variable vari = get_global_var (name);
1371 domain.Defvar (vari);
1372 domain.Defvar (node_list[i]);
1373 var_names[i] = name;
1377 private void parse_commands (MPlist plist)
1379 commands = new Command[plist.Count];
1381 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1382 if (plist.IsPlist && plist.Plist.IsSymbol)
1383 commands[i++] = new Command (plist.Plist);
1386 private void parse_commands (XmlNode node)
1388 XmlNodeList node_list = node.ChildNodes;
1390 commands = new Command[node_list.Count];
1391 for (int i = 0; i < node_list.Count; i++)
1393 if (node_list[i].NodeType == XmlNodeType.Element)
1394 commands[i] = new Command (node_list[i]);
1398 private void parse_plugins (MPlist plist)
1400 plugins = new Dictionary<MSymbol, Plugin> ();
1402 for (; ! plist.IsEmpty; plist = plist.Next)
1404 MPlist p = plist.Plist;
1405 MSymbol sym = p.Symbol;
1406 Plugin plugin = new Plugin (sym.Name);
1408 for (p = p.next; ! p.IsEmpty; p = p.next)
1410 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1411 domain.Defun (func);
1416 private void parse_plugins (XmlNode node)
1418 plugins = new Dictionary<MSymbol, Plugin> ();
1420 foreach (XmlNode n in node.ChildNodes)
1422 Plugin plugin = new Plugin (n.Attributes[0].Value);
1423 foreach (XmlNode nn in n.ChildNodes)
1425 Xex.Function func = new PluginMethod (plugin,
1426 nn.Attributes[0].Value);
1427 domain.Defun (func);
1432 private void parse_macros (XmlNode node)
1434 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1435 if (nn.NodeType == XmlNodeType.Element)
1436 domain.Defun (nn, true);
1437 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1438 if (nn.NodeType == XmlNodeType.Element)
1439 domain.Defun (nn, false);
1442 private void parse_maps (XmlNode node)
1446 private void parse_states (XmlNode node)
1450 private void parse_include (MPlist plist)
1452 if (! plist.IsPlist)
1454 MPlist p = plist.Plist;
1455 MSymbol language, name, subname;
1456 language = p.Symbol;
1459 name = subname = MSymbol.nil;
1465 subname = MSymbol.nil;
1470 MInputMethod im = MInputMethod.Find (language, name, subname);
1476 if (! plist.IsSymbol)
1478 MSymbol target_type = plist.Symbol;
1480 MSymbol target_name = MSymbol.nil;
1482 target_name = plist.Symbol;
1483 if (target_type == Mmacro)
1485 if (target_name == MSymbol.nil)
1486 im.domain.CopyFunc (domain);
1488 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1490 else if (target_type == Mmap)
1492 if (target_name == MSymbol.nil)
1494 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1495 maps[kv.Key] = kv.Value;
1500 if (im.maps.TryGetValue (target_name, out map))
1501 maps[target_name] = map;
1504 else if (target_type == Mstate)
1506 if (target_name == MSymbol.nil)
1508 for (p = im.states; ! p.IsEmpty; p = p.next)
1509 states.Add (p.key, p.val);
1513 object state = im.states.Get (target_name);
1515 states.Add (target_name, state);
1520 private Xex.Term parse_cond (MPlist plist)
1522 Xex.Term[] args = new Xex.Term[plist.Count];
1524 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1526 if (! plist.IsPlist)
1527 throw new Exception ("Invalid cond args: " + plist);
1528 MPlist p = plist.Plist;
1529 List<Xex.Term> arg = new List<Xex.Term> (parse_action (p));
1530 args[i] = new Xex.Term (arg);
1532 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1535 private Xex.Term[] parse_action (MPlist plist)
1537 Xex.Term[] terms = new Xex.Term[plist.Count];
1539 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1542 terms[i] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1543 else if (plist.IsMText)
1544 terms[i] = new Xex.Term ((string) plist.Text);
1545 else if (plist.IsInteger)
1546 terms[i] = new Xex.Term (plist.Integer);
1547 else if (plist.IsPlist)
1549 MPlist p = plist.Plist;
1551 throw new Exception ("Invalid action: " + p);
1552 MSymbol name = p.Symbol;
1555 terms[i] = parse_cond (p);
1556 else if (name == Mset || name == Madd || name == Msub
1557 || name == Mmul || name == Mdiv)
1560 throw new Exception ("Invalid action: " + p);
1561 Xex.Symbol varname = p.Symbol.Name;
1562 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1563 varname, parse_action (p.next));
1566 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1570 throw new Exception ("Invalid action: " + plist);
1576 private void parse_macros (MPlist plist)
1578 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1581 MPlist p = pl.Plist;
1585 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
1587 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1590 MPlist p = pl.Plist;
1595 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
1596 parse_action (p.next), false);
1600 private void parse_maps (MPlist plist)
1602 for (; ! plist.IsEmpty; plist = plist.next)
1605 MPlist pl = plist.Plist;
1609 Map map = new Map ();
1610 map.name = pl.Symbol;
1611 maps[map.name] = map;
1612 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1616 MPlist p = pl.Plist;
1619 keys = new KeySeq (p.Text);
1621 keys = new KeySeq (p.Plist);
1628 new Xex.Term (domain, Nprogn, parse_action (p)));
1633 private void parse_states (MPlist plist)
1635 for (; ! plist.IsEmpty; plist = plist.next)
1638 MPlist pl = plist.Plist;
1649 State state = new State (pl.Symbol);
1650 state.title = title;
1652 states = new MPlist ();
1653 states.Add (state.name, state);
1654 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1658 MPlist p = pl.Plist;
1661 MSymbol map_name = p.Symbol;
1663 Xex.Term term = new Xex.Term (domain, Nprogn, parse_action (p));
1664 state.branches.Add (map_name, term);
1669 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1672 ((MInputContext) domain.context).insert (args[0]);
1676 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1680 ((MInputContext) domain.context).insert_candidates (args[0]);
1684 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1687 MSymbol name = (string) args[0].Symval;
1688 return new Xex.Term (Marker.Get ((MInputContext) domain.context, name));
1691 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1694 int c = ((MInputContext) domain.context).char_at (args[0].Intval);
1695 return new Xex.Term (c);
1698 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1701 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1705 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1708 MInputContext ic = (MInputContext) domain.context;
1711 ic.select (args[0].Intval);
1713 ic.select ((MSymbol) ((string) args[0].Symval));
1717 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1720 ((MInputContext) domain.context).show ();
1724 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1727 ((MInputContext) domain.context).hide ();
1731 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1735 ((MInputContext) domain.context).move (args[0].Intval);
1738 Marker m = (Marker) args[0].Objval;
1739 MInputContext ic = (MInputContext) domain.context;
1740 ((MInputContext) domain.context).move (m.Position (ic));
1745 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1748 Marker m = (Marker) args[0].Objval;
1749 m.Mark ((MInputContext) domain.context);
1753 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1756 MInputContext ic = (MInputContext) domain.context;
1759 ic.pushback (args[0].Intval);
1760 else if (args[0].IsStr)
1761 ic.pushback (new KeySeq (args[0].Strval));
1763 ic.pushback ((KeySeq) args[0].Objval);
1767 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1770 ((MInputContext) domain.context).pop ();
1774 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1777 int n = args.Length == 0 ? -2 : args[0].Intval;
1778 ((MInputContext) domain.context).undo (n);
1782 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1785 ((MInputContext) domain.context).commit ();
1789 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1792 ((MInputContext) domain.context).commit ();
1793 args = new Xex.Term[2];
1794 args[0] = args[1] = Tcatch_tag;
1795 return Xex.Fthrow (domain, vari, args);
1798 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1801 ((MInputContext) domain.context).shift (args[0].Symval);
1805 public override string ToString ()
1807 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1808 if (commands != null)
1810 str += " (commands";
1811 foreach (Command cmd in commands)
1815 if (var_names != null)
1817 str += " (variables";
1818 foreach (Xex.Symbol var in var_names)
1822 if (plugins != null)
1825 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1826 str += " " + kv.Value;
1830 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1831 str += " " + kv.Value;
1833 foreach (MPlist p in states)
1839 public class MInputContext
1841 internal static Xex.Symbol Ncandidates_group_size = "candidates-group-size";
1842 private static MSymbol Mat_less_than = "@<";
1843 private static MSymbol Mat_greater_than = "@>";
1844 private static MSymbol Mat_minus = "@-";
1845 private static MSymbol Mat_plus = "@+";
1846 private static MSymbol Mat_open_square_bracket = "@[";
1847 private static MSymbol Mat_close_square_bracket = "@]";
1849 public MInputMethod im;
1850 private MText produced;
1851 private bool active;
1852 private MText status;
1853 internal MText preedit;
1854 internal int cursor_pos;
1855 private Candidates candidates;
1856 private MPlist candidate_group;
1857 private int candidate_index;
1858 private int candidate_from, candidate_to;
1859 private bool candidate_show;
1861 private Stack<MInputMethod.State> states;
1862 internal MInputMethod.KeySeq keys;
1863 private int key_head;
1864 private int state_key_head;
1865 private object state_var_values;
1866 private int commit_key_head;
1867 private MText state_preedit;
1868 private int state_pos;
1869 internal MPlist markers = new MPlist ();
1870 internal MText preceding_text = new MText ();
1871 internal MText following_text = new MText ();
1872 private bool key_unhandled;
1874 internal Xex.Domain domain;
1877 public enum ChangedStatus
1883 CandidateList = 0x08,
1884 CandidateIndex = 0x10,
1885 CandidateShow = 0x20,
1888 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
1889 | ChangedStatus.CandidateIndex
1890 | ChangedStatus.CandidateShow);
1891 private ChangedStatus changed;
1893 public ChangedStatus Changed { get { return changed; } }
1895 public MInputContext (MInputMethod im)
1898 domain = new Xex.Domain (im.domain, this);
1899 states = new Stack<MInputMethod.State> ();
1900 states.Push ((MInputMethod.State) im.states.val);
1901 keys = new MInputMethod.KeySeq ();
1904 private void adjust_markers (int from, int to, object inserted)
1906 int ins = (inserted == null ? 0
1907 : inserted is int ? 1
1908 : ((MText) inserted).Length);
1909 int diff = ins - (to - from);
1911 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1913 int pos = plist.Integer;
1917 plist.val = pos + diff;
1922 if (cursor_pos >= to)
1924 else if (cursor_pos > from)
1928 private void preedit_replace (int from, int to, int c)
1930 preedit.Del (from, to);
1931 preedit.Ins (from, c);
1932 adjust_markers (from, to, c);
1935 private void preedit_replace (int from, int to, MText mt)
1937 preedit[from, to] = mt;
1938 adjust_markers (from, to, mt);
1941 internal void insert (Xex.Term arg)
1944 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1946 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1947 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
1950 private class Candidates
1957 public Block (int index, Xex.Term term)
1961 Data = (MText) term.Strval;
1964 MPlist plist = new MPlist ();
1966 foreach (Xex.Term t in term.Listval)
1967 p = p.Add (MSymbol.mtext, (MText) t.Strval);
1972 public Block (int index, MPlist plist)
1977 else if (plist.IsPlist)
1980 throw new Exception ("Invalid candidate: " + plist);
1985 get { return (Data is MText
1986 ? ((MText) Data).Length
1987 : ((MPlist) Data).Count); }
1990 public object this[int i]
1993 if (Data is MText) return ((MText) Data)[i];
1994 return ((MPlist) Data)[i];
1999 private Block[] blocks;
2000 private int row = 0;
2001 private int index = 0;
2002 public object[] group;
2004 private bool IsFixed { get { return group != null; } }
2007 Block last = blocks[blocks.Length - 1];
2008 return last.Index + last.Count; }
2012 get { return (IsFixed ? index % group.Length
2013 : index - blocks[row].Index); }
2016 public object Group {
2017 get { return (IsFixed ? group : blocks[row].Data); }
2020 public int GroupLength
2025 int nitems = group.Length;
2026 int start = index - (index % nitems);
2028 return (start + nitems <= total ? nitems : total - start);
2030 return blocks[row].Count;
2034 public object Current {
2036 return (IsFixed ? group[index % group.Length]
2037 : blocks[row][index - blocks[row].Index]);
2041 public Candidates (MPlist list, int column)
2043 int nblocks = list.Count;
2045 blocks = new Block[nblocks];
2046 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
2047 start += (blocks[i] = new Block (index, list)).Count;
2049 group = new object[column];
2052 public Candidates (List<Xex.Term> list, int column)
2054 int nblocks = list.Count;
2056 blocks = new Block[nblocks];
2057 for (int i = 0, start = 0; i < nblocks; i++)
2058 start += (blocks[i] = new Block (index, list[i])).Count;
2060 group = new object[column];
2063 public static void Detach (MInputContext ic)
2065 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
2066 ic.candidates = null;
2067 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2071 // Fill the array "group" by candidates stating from INDEX.
2072 // INDEX must be a multiple of "column". Set NTIMES to the
2073 // number of valid candidates in "group". Update "block" if
2074 // necessary. Return "group".
2076 private int fill_group (int start)
2078 int nitems = group.Length;
2080 Block b = blocks[r];
2082 if (start < b.Index)
2083 while (start < b.Index)
2086 while (start >= b.Index + b.Count)
2090 int count = b.Count;
2092 for (int i = 0; i < nitems; i++, start++)
2097 if (r == blocks.Length)
2103 group[i] = b[start];
2108 // Update "row" to what contains the first candidate of
2109 // the previous candidate-group, update "current_index", and
2110 // update "group" if necessary. Return the previous
2111 // candidate-group. Set NITEMS to the number of valid
2112 // candidates contained in that group.
2114 public int PrevGroup ()
2121 nitems = group.Length;
2122 if ((index -= col + nitems) < 0)
2123 index = (Total / nitems) * nitems;
2124 nitems = fill_group (index);
2128 row = row > 0 ? row-- : blocks.Length - 1;
2129 nitems = blocks[row].Count;
2130 index = blocks[row].Index;
2132 index += col < nitems ? col : nitems - 1;
2136 public int NextGroup ()
2143 nitems = group.Length;
2144 if ((index += nitems - col) >= Total)
2146 nitems = fill_group (index);
2150 row = row < blocks.Length - 1 ? row + 1 : 0;
2151 nitems = blocks[row].Count;
2152 index = blocks[row].Count;
2154 index += col < nitems ? col : nitems - 1;
2164 int nitems = PrevGroup ();
2165 index += col < nitems - 1 ? col : nitems - 1;
2174 int nitems = GroupLength;
2176 if (col == nitems - 1)
2178 nitems = NextGroup ();
2185 public void First ()
2192 index += GroupLength - (Column + 1);
2195 public void Select (int col)
2197 int maxcol = GroupLength - 1;
2200 index = index - Column + col;
2204 private void update_candidate ()
2206 object candidate = candidates.Current;
2208 if (candidate is MText)
2210 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2211 candidate_to = candidate_from + ((MText) candidate).Length;
2215 preedit_replace (candidate_from, candidate_to, (int) candidate);
2216 candidate_to = candidate_from + 1;
2218 preedit.PushProp (candidate_from, candidate_to,
2219 MInputMethod.Mcandidates, this);
2220 cursor_pos = candidate_from;
2221 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2225 internal void insert_candidates (Xex.Term arg)
2228 Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
2231 column = v.Value.Intval;
2232 candidates = new Candidates (arg.Listval, column);
2233 candidate_from = candidate_to = cursor_pos;
2234 update_candidate ();
2237 internal void select (int n)
2239 if (candidates != null)
2241 candidates.Select (n);
2242 update_candidate ();
2246 internal void select (MSymbol sym)
2248 if (candidates != null)
2250 if (sym == Mat_less_than)
2251 candidates.First ();
2252 else if (sym == Mat_greater_than)
2254 else if (sym == Mat_minus)
2256 else if (sym == Mat_plus)
2258 else if (sym == Mat_open_square_bracket)
2259 candidates.PrevGroup ();
2260 else if (sym == Mat_close_square_bracket)
2261 candidates.NextGroup ();
2265 internal int marker (MSymbol sym)
2267 int pos = cursor_pos;
2269 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2271 switch (sym.Name[0])
2273 case '<': pos = 0; break;
2274 case '>': pos = preedit.Length; break;
2275 case '-': pos = cursor_pos - 1; break;
2276 case '+': pos = cursor_pos + 1; break;
2281 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2288 if (cursor_pos < preedit.Length - 1)
2291 preedit.FindProp (MInputMethod.Mcandidates, pos,
2295 pos = preedit.Length;
2298 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2303 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2305 pos = int.Parse (sym.Name.Substring (2));
2309 object val = markers.Get (sym);
2317 internal int char_at (int pos)
2324 if (preceding_text.Length < -pos)
2326 MPlist plist = new MPlist ();
2327 plist.Push (MSymbol.integer, pos);
2328 if (MInputMethod.GetSurroundingText != null
2329 && MInputMethod.GetSurroundingText (this, plist)
2331 && preceding_text.Length < plist.Text.Length)
2332 preceding_text = plist.Text;
2334 c = (-pos < preceding_text.Length
2335 ? preceding_text[preceding_text.Length + pos] : -1);
2337 else if (pos >= 0 && pos < preedit.Length)
2341 pos -= preedit.Length;
2342 if (pos >= following_text.Length)
2344 MPlist plist = new MPlist ();
2345 plist.Push (MSymbol.integer, pos + 1);
2346 if (MInputMethod.GetSurroundingText != null
2347 && MInputMethod.GetSurroundingText (this, plist)
2349 && following_text.Length < plist.Text.Length)
2350 following_text = plist.Text;
2352 c = (pos < following_text.Length ? following_text[pos] : -1);
2357 internal void delete (int pos)
2359 if (pos < cursor_pos)
2360 preedit_replace (pos, cursor_pos, null);
2362 preedit_replace (cursor_pos, pos, null);
2363 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2366 internal void show ()
2368 candidate_show = true;
2369 changed |= ChangedStatus.CandidateShow;
2372 internal void hide ()
2374 candidate_show = false;
2375 changed |= ChangedStatus.CandidateShow;
2378 internal void move (int pos)
2382 else if (pos > preedit.Length)
2383 pos = preedit.Length;
2384 if (pos != cursor_pos)
2387 changed |= ChangedStatus.Preedit;
2391 internal void mark (MSymbol sym)
2393 MPlist slot = markers.Find (sym);
2396 markers.Push (sym, cursor_pos);
2398 slot.val = cursor_pos;
2401 internal void pushback (int n)
2414 if (key_head > keys.keyseq.Count)
2415 key_head = keys.keyseq.Count;
2419 internal void pushback (MInputMethod.KeySeq keyseq)
2423 if (key_head < keys.keyseq.Count)
2424 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2425 for (int i = 0; i < keyseq.keyseq.Count; i++)
2426 keys.keyseq.Add (keyseq.keyseq[i]);
2429 internal void pop ()
2431 if (key_head < keys.keyseq.Count)
2432 keys.keyseq.RemoveRange (key_head, 1);
2435 internal void undo (int n)
2438 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2440 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2444 internal void commit ()
2446 produced.Cat (preedit);
2448 changed |= ChangedStatus.Preedit;
2451 internal void shift (MSymbol sym)
2453 MInputMethod.State state;
2455 if (sym == MSymbol.t)
2457 if (states.Count > 1)
2458 state = states.Pop ();
2460 state = states.Peek ();
2464 state = (MInputMethod.State) im.states.Get (sym);
2466 throw new Exception ("Unknown state: " + state.name);
2469 state = states.Pop ();
2470 if (state == (MInputMethod.State) im.states.val)
2477 state_key_head = key_head;
2478 state_pos = cursor_pos;
2479 state_preedit = preedit.Dup ();
2480 if (state != states.Peek ())
2482 states.Push (state);
2483 state_var_values = domain.SaveValues ();
2484 status = state.title;
2487 changed |= ChangedStatus.StateTitle;
2489 = (Xex) state.branches.Get (MSymbol.t);
2490 if (on_entry != null)
2491 on_entry.Eval (domain);
2496 internal void reset ()
2499 state_preedit.Del ();
2503 key_head = commit_key_head = 0;
2505 states.Push ((MInputMethod.State) im.states.Val);
2510 internal object GetCandidates (out int column)
2513 if (cursor_pos == 0)
2515 Candidates candidates
2516 = (Candidates) preedit.GetProp (cursor_pos - 1,
2517 MInputMethod.Mcandidates);
2518 if (candidates == null)
2520 column = candidates.Column;
2521 return candidates.Current;
2524 internal void HandleKey ()
2528 public bool Toggle ()