2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
14 using Xex = System.Xml.Expression.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (MInputContext ic, MPlist args);
22 public static Callback PreeditStart, PreeditDone, PreeditDraw;
23 public static Callback StatusStart, StatusDone, StatusDraw;
24 public static Callback CandidateStart, CandidateDone, CandidateDraw;
25 public static Callback SetSpot;
26 public static Callback Toggle;
27 public static Callback Reset;
28 public static Callback GetSurroundingText;
29 public static Callback DeleteSurroundingText;
31 internal static Xex.Domain im_domain = new Xex.Domain (null);
32 private static MSymbol Minput_method = "input-method";
33 private static MSymbol Mdescription = "description";
34 private static MSymbol Mvariable = "variable";
35 private static MSymbol Mcommand = "command";
36 private static MSymbol Mmodule = "module";
37 private static MSymbol Mmodule_list = "module-list";
38 private static MSymbol Mtitle = "title";
39 private static MSymbol Minclude = "include";
40 private static MSymbol Mmacro = "macro";
41 private static MSymbol Mmacro_list = "macro-list";
42 private static MSymbol Mmap = "map";
43 private static MSymbol Mmap_list = "map-list";
44 private static MSymbol Mstate = "state";
45 private static MSymbol Mstate_list = "state-list";
46 internal static MSymbol Mcandidates = "candidates";
47 private static MSymbol Minsert = "insert";
48 private static MSymbol Mdelete = "delete";
49 private static MSymbol Mmove = "move";
50 private static MSymbol Mmark = "mark";
51 private static MSymbol Mmarker = "marker";
52 private static MSymbol Mset = "set";
53 private static MSymbol Madd = "add";
54 private static MSymbol Msub = "sub";
55 private static MSymbol Mmul = "mul";
56 private static MSymbol Mdiv = "div";
57 private static MSymbol Mif = "if";
58 private static MSymbol Mcond = "cond";
59 private static MSymbol Mchar_at = "char-at";
60 private static MSymbol Msurrounding_text_p = "surrounding-text-p";
61 private static MSymbol Mpushback = "pushback";
62 private static MSymbol Mkeyseq = "keyseq";
64 private static Xex.Symbol Nprogn = "progn";
65 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
66 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
68 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
69 = new Dictionary<MDatabase.Tag, MInputMethod> ();
71 internal static MInputMethod im_global = null;
74 private class Exception : System.Exception
78 public Exception (string msg) : base (msg)
83 public Exception (string fmt, params object[] args)
84 : base (String.Format (fmt, args))
89 public Exception (string msg, bool error) : base (msg)
96 private enum LoadStatus
106 public enum KeyModifier
109 Shift_L = 0x00400000,
110 Shift_R = 0x00800000,
112 Control_L = 0x01000000,
113 Control_R = 0x02000000,
114 Control = 0x03000000,
129 private static Dictionary<string, uint> keysyms
130 = new Dictionary<string, uint> ();
131 private static Dictionary<string, KeyModifier> keymodifiers
132 = new Dictionary<string, KeyModifier> ();
133 private static uint keysym_base = 0x200000;
134 private static uint char_mask = ~((uint) KeyModifier.All);
138 keysyms["bs"] = keysyms["backspace"] = 0x08;
139 keysyms["tab"] = 0x09;
140 keysyms["lf"] = keysyms["linefeed"] = 0x10;
141 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
142 keysyms["esc"] = keysyms["escape"] = 0x1B;
143 keysyms["spc"] = keysyms["space"] = 0x20;
144 keysyms["del"] = keysyms["delete"] = 0x7F;
145 keymodifiers["shift-l"] = KeyModifier.Shift_L;
146 keymodifiers["shift-r"] = KeyModifier.Shift_R;
147 keymodifiers["shift"] = KeyModifier.Shift;
148 keymodifiers["control-l"] = KeyModifier.Control_L;
149 keymodifiers["control-r"] = KeyModifier.Control_R;
150 keymodifiers["control"] = KeyModifier.Control;
151 keymodifiers["alt-l"] = KeyModifier.Alt_L;
152 keymodifiers["alt-r"] = KeyModifier.Alt_R;
153 keymodifiers["alt"] = KeyModifier.Alt;
154 keymodifiers["altgr"] = KeyModifier.AltGr;
155 keymodifiers["super"] = KeyModifier.Super;
156 keymodifiers["hyper"] = KeyModifier.Hyper;
159 private static uint decode_keysym (MSymbol keysym)
162 string name = keysym.Name;
164 if (name.Length == 1)
166 name = name.ToLower ();
167 if (! keysyms.TryGetValue (name, out key))
168 keysyms[name] = key = keysym_base++;
172 private static uint combine_modifiers (uint c, KeyModifier modifiers)
174 if (c < 0x7F && c != 0x20)
176 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
177 && Char.IsLower ((char) c))
179 modifiers &= ~KeyModifier.Shift;
180 c = Char.ToUpper ((char) c);
182 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
184 modifiers &= ~KeyModifier.Control;
188 return c | (uint) modifiers;
196 public Key (uint c, KeyModifier modifiers)
198 key = combine_modifiers (c, modifiers);
201 public Key (MSymbol keysym, KeyModifier modifiers)
203 key = combine_modifiers (decode_keysym (keysym), modifiers);
206 public Key (MSymbol keysym)
208 string str = keysym.Name;
209 int len = str.Length;
211 KeyModifier modifiers = KeyModifier.None;
213 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
216 modifiers |= KeyModifier.Shift;
217 else if (str[i] == 'C')
218 modifiers |= KeyModifier.Control;
219 else if (str[i] == 'A')
220 modifiers |= KeyModifier.Alt;
221 else if (str[i] == 'G')
222 modifiers |= KeyModifier.AltGr;
223 else if (str[i] == 's')
224 modifiers |= KeyModifier.Super;
225 else if (str[i] == 'H')
226 modifiers |= KeyModifier.Hyper;
229 key = combine_modifiers (str[i], modifiers);
231 key = combine_modifiers (decode_keysym (keysym), modifiers);
234 public Key (MPlist plist)
236 KeyModifier modifiers = KeyModifier.None;
239 for (p = plist; ! p.IsEmpty; p = p.next)
243 if (! p.next.IsEmpty)
244 throw new Exception ("Invalid Key: " + plist);
247 else if (! p.IsSymbol)
248 throw new Exception ("Invalid Key: " + plist);
251 string name = p.Symbol.Name.ToLower ();
254 if (! keymodifiers.TryGetValue (name, out m))
259 if (p.IsEmpty || ! p.next.IsEmpty)
260 throw new Exception ("Invalid Key: " + plist);
262 key = combine_modifiers ((uint) p.Integer, modifiers);
264 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
267 public bool HasModifier
269 get { return ((key & (uint) KeyModifier.All) != 0); }
272 public bool Match (Key k)
276 if ((k.key & char_mask) != (key & char_mask))
278 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
279 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
280 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
281 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
282 && (m2 & KeyModifier.Shift) != KeyModifier.None))
283 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
284 || ((m1 & KeyModifier.Control) == KeyModifier.Control
285 && (m2 & KeyModifier.Control) != KeyModifier.None))
286 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
287 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
288 && (m2 & KeyModifier.Alt) != KeyModifier.None))
289 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
292 public override string ToString ()
294 string str = Char.ToString ((char) key);
295 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
297 if (m != KeyModifier.None)
299 if ((m & KeyModifier.Shift) != KeyModifier.None)
301 if ((m & KeyModifier.Control) != KeyModifier.None)
303 if ((m & KeyModifier.Alt) != KeyModifier.None)
305 if ((m & KeyModifier.AltGr) != KeyModifier.None)
307 if ((m & KeyModifier.Super) != KeyModifier.None)
309 if ((m & KeyModifier.Hyper) != KeyModifier.None)
316 public class KeySeq : Xex.TermValue
318 public List<Key> keyseq = new List<Key> ();
320 public override Xex.TermValue Clone ()
322 KeySeq ks = new KeySeq ();
323 ks.keyseq.InsertRange (0, keyseq);
329 public KeySeq (MPlist plist)
331 foreach (MPlist p in plist)
334 keyseq.Add (new Key (p.Symbol));
335 else if (p.IsInteger)
336 keyseq.Add (new Key ((char) p.Integer));
338 keyseq.Add (new Key (p.Plist));
340 throw new Exception ("Invalid Key Sequence: " + plist);
344 public KeySeq (MText mt) : base ()
346 for (int i = 0; i < mt.Length; i++)
347 keyseq.Add (new Key ((uint) mt[i]));
350 private static uint parse_integer (string str)
352 if (Char.IsDigit (str[0]))
354 if (str[0] == '0' && str.Length > 2 && str[1] == 'x')
357 for (int idx = 2; idx < str.Length; idx++)
360 if (c >= '0' && c <= '9')
361 i = i * 16 + (c - '0');
362 else if (c >= 'A' && c <= 'F')
363 i = i * 16 + 10 + (c - 'A');
364 else if (c >= 'a' && c <= 'f')
365 i = i * 16 + 10 + (c - 'a');
371 return UInt32.Parse (str);
373 else if (str[0] == '?')
378 public KeySeq (XmlNode node)
380 XmlAttributeCollection acol = node.Attributes;
383 if (acol != null && (n = acol["keys"]) != null)
385 foreach (char c in n.Value)
386 keyseq.Add (new Key ((uint) c));
389 for (node = node.FirstChild; node != null; node = node.NextSibling)
391 if (node.Name == "key-event")
392 keyseq.Add (new Key ((MSymbol) node.InnerText));
394 keyseq.Add (new Key (parse_integer (node.InnerText)));
398 public KeySeq (Xex.Term[] args)
400 foreach (Xex.Term term in args)
403 keyseq.Add (new Key ((MSymbol) ((string) term.Symval)));
405 keyseq.Add (new Key (term.Strval));
409 public override string ToString ()
413 foreach (Key key in keyseq)
417 foreach (Key k in keyseq)
418 str += " " + k.ToString ();
422 foreach (Key key in keyseq)
423 str += key.ToString ();
428 public class Variable
431 public MText description;
434 public object[] candidates;
436 public Variable (MPlist p)
440 description = parse_description (p);
441 if (description == null)
442 description = new MText ("No description");
445 type = (p.IsMText ? typeof (MText)
446 : p.IsInteger ? typeof (int)
447 : p.IsSymbol ? typeof (MSymbol)
451 candidates = new object[p.Count];
452 for (int i = 0; ! p.IsEmpty; i++, p = p.next)
453 candidates[i] = p.val;
456 private static Type parse_value (XmlNode node, out object value)
458 string typename = node.Attributes["type"].Value;
461 if (typename == "integer")
464 if (! Int32.TryParse (node.InnerText, out i))
469 else if (typename == "string")
471 MText mt = node.InnerText;
473 type = typeof (MText);
475 else if (typename == "symbol")
477 MSymbol sym = node.InnerText;
479 type = typeof (MSymbol);
484 type = typeof (object);
489 public Variable (XmlNode node)
491 name = node.Attributes["id"].Value;
492 for (node = node.FirstChild; node != null; node = node.NextSibling)
493 if (node.NodeType == XmlNodeType.Element)
495 if (node.Name == "description")
496 description = parse_description (node);
497 else if (node.Name == "value")
498 type = parse_value (node, out value);
499 else if (node.Name == "valiable-value-candidate")
501 XmlNodeList n_list = node.ChildNodes;
502 candidates = new object[n_list.Count];
503 for (int i = 0; i < n_list.Count; i++)
506 parse_value (n_list[i], out val);
513 public override string ToString ()
515 return ("(" + name + " \"" + (string) description
516 + "\" " + type + " " + value + " " + candidates + ")");
523 public MText description;
524 public List<KeySeq> keys;
526 public Command (MPlist p)
530 description = parse_description (p);
531 if (description == null)
532 description = "No description";
533 keys = new List<KeySeq> ();
534 for (p = p.next; ! p.IsEmpty; p = p.next)
537 keys.Add (new KeySeq (p.Text));
539 keys.Add (new KeySeq (p.Plist));
543 public Command (XmlNode node)
545 name = node.Attributes["id"].Value;
546 keys = new List<KeySeq> ();
547 for (node = node.FirstChild; node != null; node = node.NextSibling)
549 if (node.Name == "description")
550 description = parse_description (node);
551 else if (node.Name == "keyseq")
552 keys.Add (new KeySeq (node));
556 public override string ToString ()
558 string str = "(" + name + " \"" + (string) description;
559 foreach (KeySeq keyseq in keys)
565 internal class Plugin
568 private Assembly assembly;
569 private Type plugin_type;
571 public Plugin (string name)
576 public MethodInfo GetMethod (Xex.Symbol name)
578 if (assembly == null)
580 assembly = Assembly.LoadFrom (name + ".dll");
581 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
584 MethodInfo info = plugin_type.GetMethod ((string) name);
586 throw new Exception ("Invalid plugin method: " + name);
590 public override string ToString ()
592 return String.Format ("(module {0}", name);
596 internal class PluginMethod : Xex.Function
598 private Plugin plugin;
599 private MethodInfo method_info;
600 object[] parameters = new object[2];
602 public PluginMethod (Plugin plugin, string name)
603 : base ((Xex.Symbol) name, 0, -1)
605 this.plugin = plugin;
608 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
611 args = (Xex.Term[]) args.Clone ();
612 for (int i = 0; i < args.Length; i++)
614 args[i] = args[i].Eval (domain);
618 if (method_info == null)
619 method_info = plugin.GetMethod (name);
620 parameters[0] = domain.context;
621 parameters[1] = args;
622 return (Xex.Term) method_info.Invoke (null, parameters);
626 internal abstract class Marker : Xex.TermValue
630 public Marker (MSymbol name)
635 public abstract int Position (MInputContext ic);
636 public abstract void Mark (MInputContext ic);
638 public class Named : Marker
642 public Named (MSymbol name) : this (name, 0) { }
644 private Named (MSymbol name, int p) : base (name) { pos = p; }
646 public override int Position (MInputContext ic) { return pos; }
648 public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
650 public override Xex.TermValue Clone ()
652 return new Named (name, pos);
656 public class Predefined : Marker
658 public Predefined (MSymbol name) : base (name) { }
660 public override int Position (MInputContext ic)
662 switch (name.Name[1]) {
664 case '>': return ic.preedit.Length;
665 case '-': return ic.cursor_pos - 1;
666 case '+': return ic.cursor_pos + 1;
668 if (ic.cursor_pos > 0)
670 int pos = ic.cursor_pos;
672 ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
678 if (ic.cursor_pos < ic.preedit.Length - 1)
680 int pos = ic.cursor_pos;
682 ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
686 return ic.preedit.Length;
688 return name.Name[1] - '0';
692 public override void Mark (MInputContext ic)
694 throw new Exception ("Can't set predefined marker: " + name);
697 public override Xex.TermValue Clone ()
699 return new Predefined (name);
703 static internal Dictionary<MSymbol,Predefined> predefined_markers;
707 predefined_markers = new Dictionary<MSymbol,Predefined> ();
708 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
709 "@0", "@1", "@2", "@3", "@4",
710 "@5", "@6", "@7", "@8", "@9" };
711 foreach (MSymbol s in symlist)
712 predefined_markers[s] = new Predefined (s);
715 public static Marker Get (MInputContext ic, MSymbol name)
720 if (predefined_markers.TryGetValue (name, out pred))
722 if (name.Name[0] == '@')
723 throw new Exception ("Invalid marker name: " + name);
724 m = (Marker) ic.markers.Get (name);
727 m = new Named (name);
728 ic.markers.Put (name, m);
737 public Dictionary<Key, Map> submaps;
738 public Xex.Term actions;
740 public void Add (KeySeq keys, int index, Xex.Term actions)
745 submaps = new Dictionary<Key, Map> ();
747 submaps.TryGetValue (keys.keyseq[index], out sub);
750 Key key = keys.keyseq[index];
751 submaps[key] = sub = new Map ();
753 if (index + 1 < keys.keyseq.Count)
754 sub.Add (keys, index + 1, actions);
756 this.actions = actions;
759 public Xex.Term Lookup (KeySeq keys, int index)
763 if (index + 1 == keys.keyseq.Count)
765 if (submaps.TryGetValue (keys.keyseq[index], out sub))
766 return sub.Lookup (keys, index + 1);
770 private void describe (MText mt, KeySeq keyseq)
772 if (keyseq.keyseq.Count > 0)
774 mt.Cat (" (").Cat (keyseq.ToString ());
775 mt.Cat (' ').Cat (actions.ToString ());
779 foreach (KeyValuePair<Key, Map> kv in submaps)
781 keyseq.keyseq.Add (kv.Key);
782 kv.Value.describe (mt, keyseq);
783 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
787 public override string ToString ()
789 MText mt = "(" + name.Name;
790 KeySeq keyseq = new KeySeq ();
792 describe (mt, keyseq);
802 public MPlist branches = new MPlist ();
804 public State (MSymbol name)
809 public override string ToString ()
811 MText mt = "(" + name.Name;
814 mt.Cat (" \"" + title + "\"");
815 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
816 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
817 return (string) mt + ")";
822 internal Xex.Domain domain;
824 private LoadStatus load_status = LoadStatus.None;
825 private MDatabase.Tag tag;
826 private MDatabase mdb;
828 private MText description;
829 internal MText title;
830 internal Command[] commands;
831 internal Xex.Symbol[] var_names;
832 internal Dictionary<MSymbol, Plugin> plugins;
833 internal Dictionary<MSymbol, Map> maps;
834 internal MPlist states;
836 static MInputMethod ()
838 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
839 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
840 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
841 im_domain.DefSubr (Fselect, "select", true, 1, 1);
842 im_domain.DefSubr (Fshow, "show", true, 0, 0);
843 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
844 im_domain.DefSubr (Fmove, "move", true, 1, 1);
845 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
846 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
847 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
848 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
849 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
850 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
851 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
852 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
853 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
854 im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
856 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
857 List<MDatabase> list = MDatabase.List (tag);
858 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
859 foreach (MDatabase mdb in list)
861 im_table[mdb.tag] = new MInputMethod (mdb.tag);
863 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil, "global");
864 im_global = im_table[tag];
868 private MInputMethod (MDatabase.Tag tag)
873 // Instance Properties
874 public MSymbol Language { get { return tag[1]; } }
875 public MSymbol Name { get { return tag[2]; } }
876 public MSymbol SubName { get { return tag[3]; } }
878 public bool Info (out MText description,
880 out Xex.Variable[] variables,
881 out Command[] commands)
883 if ((load_status & LoadStatus.Header) != LoadStatus.Header
892 description = this.description;
894 if (var_names == null)
898 variables = new Xex.Variable[var_names.Length];
900 foreach (Xex.Symbol name in var_names)
901 variables[i++] = domain.GetVar (name, false);
903 commands = this.commands;
907 public static MInputMethod Find (MSymbol language, MSymbol name)
909 return Find (language, name, MSymbol.nil);
912 public static MInputMethod Find (MSymbol language, MSymbol name,
915 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
919 return (im_table.TryGetValue (tag, out im) ? im : null);
924 return ((load_status == LoadStatus.Full) || load_body ());
927 public static MInputMethod[] List ()
929 MInputMethod[] array = new MInputMethod[im_table.Count];
932 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
933 array[i++] = kv.Value;
937 private bool load_header ()
939 mdb = MDatabase.Find (tag);
943 MSymbol format = mdb.Format;
945 if (format == MSymbol.plist)
946 load ((MPlist) mdb.Load (Mmap), false);
949 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
950 load (doc.DocumentElement, false);
952 } catch (Exception e) {
953 Console.WriteLine ("{0}\n", e);
954 load_status = LoadStatus.Error;
957 load_status |= LoadStatus.Header;
961 private bool load_body ()
963 domain = new Xex.Domain (im_domain, null);
964 mdb = MDatabase.Find (tag);
968 object obj = mdb.Load ();
970 load ((MPlist) obj, true);
972 load ((XmlDocument) obj, true);
973 } catch (Exception e) {
974 Console.WriteLine (e);
975 load_status = LoadStatus.Error;
978 load_status = LoadStatus.Full;
982 private void load (MPlist plist, bool full)
984 maps = new Dictionary<MSymbol, Map> ();
985 states = new MPlist ();
987 for (; ! plist.IsEmpty; plist = plist.next)
990 MPlist pl = plist.Plist;
993 MSymbol sym = pl.Symbol;
996 if (sym == Mdescription)
997 description = parse_description (pl);
998 else if (sym == Mtitle)
1003 else if (sym == Mvariable)
1004 parse_variables (pl);
1005 else if (sym == Mcommand)
1006 parse_commands (pl);
1011 else if (sym == Minclude)
1013 else if (sym == Mmacro)
1015 else if (sym == Mmap)
1017 else if (sym == Mstate)
1022 if (description == null)
1023 description = (MText) "No description";
1025 title = new MText (tag[2].Name);
1026 if (commands == null)
1027 commands = new Command[0];
1032 State state = new State ((MSymbol) "init");
1033 plist = new MPlist ();
1034 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1035 state.branches.Add (kv.Key, null);
1036 states.Add (state.name, state);
1040 private void load (XmlNode node, bool full)
1042 bool skip_header = load_status == LoadStatus.Header;
1044 maps = new Dictionary<MSymbol, Map> ();
1045 states = new MPlist ();
1047 if (node.NodeType == XmlNodeType.Document)
1048 node = node.FirstChild;
1049 while (node.NodeType != XmlNodeType.Element)
1050 node = node.NextSibling;
1051 for (node = node.FirstChild; node != null; node = node.NextSibling)
1053 if (node.NodeType != XmlNodeType.Element)
1057 if (node.Name == "description")
1058 description = parse_description (node);
1059 else if (node.Name == "title")
1060 title = parse_title (node);
1061 else if (node.Name == "variable-list")
1062 parse_variables (node);
1063 else if (node.Name == "command-list")
1064 parse_commands (node);
1068 if (node.Name == "module-list")
1069 parse_plugins (node);
1070 else if (node.Name == "macro-list")
1071 parse_macros (node);
1072 else if (node.Name == "map-list")
1074 else if (node.Name == "state-list")
1075 parse_states (node);
1078 if (description == null)
1079 description = (MText) "No description";
1081 title = new MText (tag[2].Name);
1082 if (commands == null)
1083 commands = new Command[0];
1088 State state = new State ((MSymbol) "init");
1089 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1090 state.branches.Add (kv.Key, null);
1091 states.Add (state.name, state);
1095 private static void transform (MPlist plist)
1097 for (; ! plist.IsEmpty; plist = plist.next)
1101 MPlist p = new MPlist ();
1102 p.Add (MSymbol.symbol, Minsert);
1103 p.Add (MSymbol.mtext, plist.Text);
1104 plist.Set (MSymbol.plist, p);
1106 else if (plist.IsInteger)
1108 MPlist p = new MPlist ();
1109 p.Add (MSymbol.symbol, Minsert);
1110 p.Add (MSymbol.integer, plist.Integer);
1111 plist.Set (MSymbol.plist, p);
1113 else if (plist.IsPlist)
1115 MPlist pl = plist.Plist;
1119 if (pl.Symbol == Madd)
1120 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1121 else if (pl.Symbol == Msub)
1122 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1123 else if (pl.Symbol == Mmul)
1124 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1125 else if (pl.Symbol == Mdiv)
1126 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1127 else if (pl.Symbol == Minsert)
1129 // (insert (CANDIDATES ...))
1130 // => (candidates CANDIDATES ...)
1131 if (pl.next.IsPlist)
1133 pl.Set (MSymbol.symbol, Mcandidates);
1135 MPlist p = pl.Plist;
1136 pl.Set (p.key, p.val);
1137 for (p = p.next; ! p.IsEmpty; p = p.next);
1138 pl.Add (p.key, p.val);
1141 else if (pl.Symbol == Mif)
1145 transform (pl.next);
1147 else if (pl.Symbol == Mcond)
1149 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1152 MPlist p = pl.Plist;
1160 else if (pl.Symbol == Mdelete
1161 || pl.Symbol == Mmove
1162 || pl.Symbol == Mmark)
1167 MSymbol sym = pl.Symbol;
1168 MPlist p = new MPlist ();
1169 p.Add (MSymbol.symbol, Mmarker);
1170 p.Add (MSymbol.symbol, sym);
1171 pl.Set (MSymbol.plist, p);
1174 else if (pl.Symbol == Mpushback)
1178 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1181 else if (pl.IsMText)
1183 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1184 pl.Push (MSymbol.symbol, Mcandidates);
1187 else if (plist.IsSymbol)
1189 MSymbol sym = plist.Symbol;
1191 if (sym.Name.Length >= 3
1192 && sym.Name[0] == '@'
1193 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1195 int pos = int.Parse (sym.Name.Substring (1));
1196 MPlist p = new MPlist ();
1200 p.Add (MSymbol.symbol, Msurrounding_text_p);
1204 if (sym.Name[1] == '+')
1206 p.Add (MSymbol.symbol, Mchar_at);
1207 p.Add (MSymbol.integer, pos);
1209 plist.Set (MSymbol.plist, p);
1215 private static MText parse_description (MPlist plist)
1221 plist = plist.Plist;
1222 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1223 && plist.next.IsMText)
1224 return plist.next.Text;
1229 private static MText parse_description (XmlNode node)
1231 if (node.HasChildNodes)
1232 node = node.FirstChild;
1233 return node.InnerText;
1236 private static MText parse_title (XmlNode node)
1238 return node.InnerText;
1241 private void new_variable (Xex.Symbol name, string desc, int val,
1242 MPlist pl, Xex.Variable vari)
1250 range = new int[pl.Count * 2];
1251 for (int i = 0; i < range.Length; i++)
1255 MPlist p = pl.Plist;
1257 if (! p.IsInteger || ! p.next.IsInteger)
1258 throw new Exception ("Invalid range: " + p);
1259 range[i * 2] = p.Integer;
1260 range[i * 2 + 1] = p.next.Integer;
1262 else if (pl.IsInteger)
1263 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1265 throw new Exception ("Invalid range: " + pl);
1269 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1272 Xex.Term term = new Xex.Term (val);
1274 vari.DefaultValue = term;
1279 private void new_variable (Xex.Symbol name, string desc, MText val,
1280 MPlist pl, Xex.Variable vari)
1288 range = new string[pl.Count * 2];
1289 for (int i = 0; i < range.Length; i++)
1292 range[i] = (string) pl.Text;
1294 throw new Exception ("Invalid range: " + pl);
1298 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1301 Xex.Term term = new Xex.Term ((string) val);
1303 vari.DefaultValue = term;
1308 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1309 MPlist pl, Xex.Variable vari)
1312 Xex.Symbol sym = val.Name;
1318 range = new Xex.Symbol[pl.Count * 2];
1319 for (int i = 0; i < range.Length; i++)
1322 range[i] = pl.Symbol.Name;
1324 throw new Exception ("Invalid range: " + pl);
1328 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1331 Xex.Term term = new Xex.Term (sym);
1333 vari.DefaultValue = term;
1338 private void parse_variables (MPlist plist)
1340 var_names = new Xex.Symbol[plist.Count];
1342 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1344 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1345 throw new Exception ("Invalid variable: " + plist);
1347 MPlist p = plist.Plist;
1348 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1349 var_names[i] = name;
1351 string desc = (string) parse_description (p);
1353 = im_global != null ? im_global.domain.GetVar (name, false) : null;
1355 domain.Defvar (vari);
1361 new_variable (name, desc, p.Integer, p.next, vari);
1363 new_variable (name, desc, p.Text, p.next, vari);
1364 else if (p.IsSymbol)
1365 new_variable (name, desc, p.Symbol, p.next, vari);
1367 throw new Exception ("Invalid variable type: " + p.val);
1372 private void parse_variables (XmlNode node)
1374 XmlNodeList node_list = node.ChildNodes;
1376 var_names = new Xex.Symbol[node_list.Count];
1377 for (int i = 0; i < node_list.Count; i++)
1379 Xex.Symbol name = node_list[i].Attributes[0].Value;
1380 Xex.Variable vari = im_global.domain.GetVar (name, false);
1383 domain.Defvar (vari);
1385 domain.Defvar (node_list[i]);
1387 var_names[i] = name;
1391 private void parse_commands (MPlist plist)
1393 commands = new Command[plist.Count];
1395 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1396 if (plist.IsPlist && plist.Plist.IsSymbol)
1397 commands[i++] = new Command (plist.Plist);
1400 private void parse_commands (XmlNode node)
1402 XmlNodeList node_list = node.ChildNodes;
1404 commands = new Command[node_list.Count];
1405 for (int i = 0; i < node_list.Count; i++)
1407 if (node_list[i].NodeType == XmlNodeType.Element)
1408 commands[i] = new Command (node_list[i]);
1412 private void parse_plugins (MPlist plist)
1414 plugins = new Dictionary<MSymbol, Plugin> ();
1416 for (; ! plist.IsEmpty; plist = plist.Next)
1418 MPlist p = plist.Plist;
1419 MSymbol sym = p.Symbol;
1420 Plugin plugin = new Plugin (sym.Name);
1422 for (p = p.next; ! p.IsEmpty; p = p.next)
1424 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1425 domain.Defun (func);
1430 private void parse_plugins (XmlNode node)
1432 plugins = new Dictionary<MSymbol, Plugin> ();
1434 foreach (XmlNode n in node.ChildNodes)
1436 Plugin plugin = new Plugin (n.Attributes[0].Value);
1437 foreach (XmlNode nn in n.ChildNodes)
1439 Xex.Function func = new PluginMethod (plugin,
1440 nn.Attributes[0].Value);
1441 domain.Defun (func);
1446 private void parse_macros (XmlNode node)
1448 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1449 if (nn.NodeType == XmlNodeType.Element)
1450 domain.Defun (nn, true);
1451 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1452 if (nn.NodeType == XmlNodeType.Element)
1453 domain.Defun (nn, false);
1456 private void parse_maps (XmlNode node)
1460 private void parse_states (XmlNode node)
1464 private void parse_include (MPlist plist)
1466 if (! plist.IsPlist)
1468 MPlist p = plist.Plist;
1469 MSymbol language, name, subname;
1470 language = p.Symbol;
1473 name = subname = MSymbol.nil;
1479 subname = MSymbol.nil;
1484 MInputMethod im = MInputMethod.Find (language, name, subname);
1490 if (! plist.IsSymbol)
1492 MSymbol target_type = plist.Symbol;
1494 MSymbol target_name = MSymbol.nil;
1496 target_name = plist.Symbol;
1497 if (target_type == Mmacro)
1499 if (target_name == MSymbol.nil)
1500 im.domain.CopyFunc (domain);
1502 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1504 else if (target_type == Mmap)
1506 if (target_name == MSymbol.nil)
1508 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1509 maps[kv.Key] = kv.Value;
1514 if (im.maps.TryGetValue (target_name, out map))
1515 maps[target_name] = map;
1518 else if (target_type == Mstate)
1520 if (target_name == MSymbol.nil)
1522 for (p = im.states; ! p.IsEmpty; p = p.next)
1523 states.Add (p.key, p.val);
1527 object state = im.states.Get (target_name);
1529 states.Add (target_name, state);
1534 private Xex.Term parse_cond (MPlist plist)
1536 Xex.Term[] args = new Xex.Term[plist.Count];
1538 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1540 if (! plist.IsPlist)
1541 throw new Exception ("Invalid cond args: " + plist);
1542 MPlist p = plist.Plist;
1543 List<Xex.Term> arg = new List<Xex.Term> (parse_action (p));
1544 args[i] = new Xex.Term (arg);
1546 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1549 private Xex.Term[] parse_action (MPlist plist)
1551 Xex.Term[] terms = new Xex.Term[plist.Count];
1553 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1556 terms[i] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1557 else if (plist.IsMText)
1558 terms[i] = new Xex.Term ((string) plist.Text);
1559 else if (plist.IsInteger)
1560 terms[i] = new Xex.Term (plist.Integer);
1561 else if (plist.IsPlist)
1563 MPlist p = plist.Plist;
1565 throw new Exception ("Invalid action: " + p);
1566 MSymbol name = p.Symbol;
1569 terms[i] = parse_cond (p);
1570 else if (name == Mset || name == Madd || name == Msub
1571 || name == Mmul || name == Mdiv)
1574 throw new Exception ("Invalid action: " + p);
1575 Xex.Symbol varname = p.Symbol.Name;
1576 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1577 varname, parse_action (p.next));
1580 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1584 throw new Exception ("Invalid action: " + plist);
1590 private void parse_macros (MPlist plist)
1592 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1595 MPlist p = pl.Plist;
1599 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
1601 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1604 MPlist p = pl.Plist;
1609 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
1610 parse_action (p.next), false);
1614 private void parse_maps (MPlist plist)
1616 for (; ! plist.IsEmpty; plist = plist.next)
1619 MPlist pl = plist.Plist;
1623 Map map = new Map ();
1624 map.name = pl.Symbol;
1625 maps[map.name] = map;
1626 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1630 MPlist p = pl.Plist;
1633 keys = new KeySeq (p.Text);
1635 keys = new KeySeq (p.Plist);
1642 new Xex.Term (domain, Nprogn, parse_action (p)));
1647 private void parse_states (MPlist plist)
1649 for (; ! plist.IsEmpty; plist = plist.next)
1652 MPlist pl = plist.Plist;
1663 State state = new State (pl.Symbol);
1664 state.title = title;
1666 states = new MPlist ();
1667 states.Add (state.name, state);
1668 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1672 MPlist p = pl.Plist;
1675 MSymbol map_name = p.Symbol;
1677 Xex.Term term = new Xex.Term (domain, Nprogn, parse_action (p));
1678 state.branches.Add (map_name, term);
1683 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1686 ((MInputContext) domain.context).insert (args[0]);
1690 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1694 ((MInputContext) domain.context).insert_candidates (args[0]);
1698 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1701 MSymbol name = (string) args[0].Symval;
1702 return new Xex.Term (Marker.Get ((MInputContext) domain.context, name));
1705 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1708 int c = ((MInputContext) domain.context).char_at (args[0].Intval);
1709 return new Xex.Term (c);
1712 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1715 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1719 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1722 MInputContext ic = (MInputContext) domain.context;
1725 ic.select (args[0].Intval);
1727 ic.select ((MSymbol) ((string) args[0].Symval));
1731 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1734 ((MInputContext) domain.context).show ();
1738 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1741 ((MInputContext) domain.context).hide ();
1745 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1749 ((MInputContext) domain.context).move (args[0].Intval);
1752 Marker m = (Marker) args[0].Objval;
1753 MInputContext ic = (MInputContext) domain.context;
1754 ((MInputContext) domain.context).move (m.Position (ic));
1759 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1762 Marker m = (Marker) args[0].Objval;
1763 m.Mark ((MInputContext) domain.context);
1767 private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
1770 return new Xex.Term (new KeySeq (args));
1773 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1776 MInputContext ic = (MInputContext) domain.context;
1779 ic.pushback (args[0].Intval);
1780 else if (args[0].IsStr)
1781 ic.pushback (new KeySeq (args[0].Strval));
1783 ic.pushback ((KeySeq) args[0].Objval);
1787 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1790 ((MInputContext) domain.context).pop ();
1794 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1797 int n = args.Length == 0 ? -2 : args[0].Intval;
1798 ((MInputContext) domain.context).undo (n);
1802 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1805 ((MInputContext) domain.context).commit ();
1809 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1812 ((MInputContext) domain.context).commit ();
1813 args = new Xex.Term[2];
1814 args[0] = args[1] = Tcatch_tag;
1815 return Xex.Fthrow (domain, vari, args);
1818 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1821 ((MInputContext) domain.context).shift (args[0].Symval);
1825 public override string ToString ()
1827 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1828 if (commands != null)
1830 str += " (commands";
1831 foreach (Command cmd in commands)
1835 if (var_names != null)
1837 str += " (variables";
1838 foreach (Xex.Symbol var in var_names)
1842 if (plugins != null)
1845 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1846 str += " " + kv.Value;
1850 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1851 str += " " + kv.Value;
1853 foreach (MPlist p in states)
1859 public class MInputContext
1861 internal static Xex.Symbol Ncandidates_group_size = "candidates-group-size";
1862 private static MSymbol Mat_less_than = "@<";
1863 private static MSymbol Mat_greater_than = "@>";
1864 private static MSymbol Mat_minus = "@-";
1865 private static MSymbol Mat_plus = "@+";
1866 private static MSymbol Mat_open_square_bracket = "@[";
1867 private static MSymbol Mat_close_square_bracket = "@]";
1869 public MInputMethod im;
1870 private MText produced;
1871 private bool active;
1872 private MText status;
1873 private bool status_changed;
1874 internal MText preedit;
1875 private bool preedit_changed;
1876 internal int cursor_pos;
1877 private bool cursor_pos_changed;
1878 private Candidates candidates;
1879 private MPlist candidate_group;
1880 private int candidate_index;
1881 private int candidate_from, candidate_to;
1882 private bool candidate_show;
1883 private bool candidate_changed;
1885 private Stack<MInputMethod.State> states;
1886 internal MInputMethod.KeySeq keys;
1887 private int key_head;
1888 private int state_key_head;
1889 private object state_var_values;
1890 private int commit_key_head;
1891 private MText state_preedit;
1892 private int state_pos;
1893 internal MPlist markers = new MPlist ();
1894 internal MText preceding_text = new MText ();
1895 internal MText following_text = new MText ();
1896 private bool key_unhandled;
1898 internal Xex.Domain domain;
1900 public MInputContext (MInputMethod im)
1903 domain = new Xex.Domain (im.domain, this);
1904 states = new Stack<MInputMethod.State> ();
1905 states.Push ((MInputMethod.State) im.states.val);
1906 keys = new MInputMethod.KeySeq ();
1909 private void adjust_markers (int from, int to, object inserted)
1911 int ins = (inserted == null ? 0
1912 : inserted is int ? 1
1913 : ((MText) inserted).Length);
1914 int diff = ins - (to - from);
1916 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1918 int pos = plist.Integer;
1922 plist.val = pos + diff;
1927 if (cursor_pos >= to)
1929 else if (cursor_pos > from)
1933 private void preedit_replace (int from, int to, int c)
1935 preedit.Del (from, to);
1936 preedit.Ins (from, c);
1937 adjust_markers (from, to, c);
1940 private void preedit_replace (int from, int to, MText mt)
1942 preedit[from, to] = mt;
1943 adjust_markers (from, to, mt);
1946 internal void insert (Xex.Term arg)
1949 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1951 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1952 preedit_changed = true;
1953 cursor_pos_changed = true;
1956 private class Candidates
1963 public Block (int index, Xex.Term term)
1967 Data = (MText) term.Strval;
1970 MPlist plist = new MPlist ();
1972 foreach (Xex.Term t in term.Listval)
1973 p = p.Add (MSymbol.mtext, (MText) t.Strval);
1978 public Block (int index, MPlist plist)
1983 else if (plist.IsPlist)
1986 throw new Exception ("Invalid candidate: " + plist);
1991 get { return (Data is MText
1992 ? ((MText) Data).Length
1993 : ((MPlist) Data).Count); }
1996 public object this[int i]
1999 if (Data is MText) return ((MText) Data)[i];
2000 return ((MPlist) Data)[i];
2005 private Block[] blocks;
2006 private int row = 0;
2007 private int index = 0;
2008 public object[] group;
2010 private bool IsFixed { get { return group != null; } }
2013 Block last = blocks[blocks.Length - 1];
2014 return last.Index + last.Count; }
2018 get { return (IsFixed ? index % group.Length
2019 : index - blocks[row].Index); }
2022 public object Group {
2023 get { return (IsFixed ? group : blocks[row].Data); }
2026 public int GroupLength
2031 int nitems = group.Length;
2032 int start = index - (index % nitems);
2034 return (start + nitems <= total ? nitems : total - start);
2036 return blocks[row].Count;
2040 public object Current {
2042 return (IsFixed ? group[index % group.Length]
2043 : blocks[row][index - blocks[row].Index]);
2047 public Candidates (MPlist list, int column)
2049 int nblocks = list.Count;
2051 blocks = new Block[nblocks];
2052 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
2053 start += (blocks[i] = new Block (index, list)).Count;
2055 group = new object[column];
2058 public Candidates (List<Xex.Term> list, int column)
2060 int nblocks = list.Count;
2062 blocks = new Block[nblocks];
2063 for (int i = 0, start = 0; i < nblocks; i++)
2064 start += (blocks[i] = new Block (index, list[i])).Count;
2066 group = new object[column];
2069 public static void Detach (MInputContext ic)
2071 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
2072 ic.candidates = null;
2073 ic.preedit_changed = true;
2074 ic.cursor_pos_changed = true;
2075 ic.candidate_changed = true;
2078 // Fill the array "group" by candidates stating from INDEX.
2079 // INDEX must be a multiple of "column". Set NTIMES to the
2080 // number of valid candidates in "group". Update "block" if
2081 // necessary. Return "group".
2083 private int fill_group (int start)
2085 int nitems = group.Length;
2087 Block b = blocks[r];
2089 if (start < b.Index)
2090 while (start < b.Index)
2093 while (start >= b.Index + b.Count)
2097 int count = b.Count;
2099 for (int i = 0; i < nitems; i++, start++)
2104 if (r == blocks.Length)
2110 group[i] = b[start];
2115 // Update "row" to what contains the first candidate of
2116 // the previous candidate-group, update "current_index", and
2117 // update "group" if necessary. Return the previous
2118 // candidate-group. Set NITEMS to the number of valid
2119 // candidates contained in that group.
2121 public int PrevGroup ()
2128 nitems = group.Length;
2129 if ((index -= col + nitems) < 0)
2130 index = (Total / nitems) * nitems;
2131 nitems = fill_group (index);
2135 row = row > 0 ? row-- : blocks.Length - 1;
2136 nitems = blocks[row].Count;
2137 index = blocks[row].Index;
2139 index += col < nitems ? col : nitems - 1;
2143 public int NextGroup ()
2150 nitems = group.Length;
2151 if ((index += nitems - col) >= Total)
2153 nitems = fill_group (index);
2157 row = row < blocks.Length - 1 ? row + 1 : 0;
2158 nitems = blocks[row].Count;
2159 index = blocks[row].Count;
2161 index += col < nitems ? col : nitems - 1;
2171 int nitems = PrevGroup ();
2172 index += col < nitems - 1 ? col : nitems - 1;
2181 int nitems = GroupLength;
2183 if (col == nitems - 1)
2185 nitems = NextGroup ();
2192 public void First ()
2199 index += GroupLength - (Column + 1);
2202 public void Select (int col)
2204 int maxcol = GroupLength - 1;
2207 index = index - Column + col;
2211 private void update_candidate ()
2213 object candidate = candidates.Current;
2215 if (candidate is MText)
2217 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2218 candidate_to = candidate_from + ((MText) candidate).Length;
2222 preedit_replace (candidate_from, candidate_to, (int) candidate);
2223 candidate_to = candidate_from + 1;
2225 preedit.PushProp (candidate_from, candidate_to,
2226 MInputMethod.Mcandidates, this);
2227 cursor_pos = candidate_from;
2228 preedit_changed = true;
2229 cursor_pos_changed = true;
2230 candidate_changed = true;
2233 internal void insert_candidates (Xex.Term arg)
2236 Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
2239 column = v.Value.Intval;
2240 candidates = new Candidates (arg.Listval, column);
2241 candidate_from = candidate_to = cursor_pos;
2242 update_candidate ();
2245 internal void select (int n)
2247 if (candidates != null)
2249 candidates.Select (n);
2250 update_candidate ();
2254 internal void select (MSymbol sym)
2256 if (candidates != null)
2258 if (sym == Mat_less_than)
2259 candidates.First ();
2260 else if (sym == Mat_greater_than)
2262 else if (sym == Mat_minus)
2264 else if (sym == Mat_plus)
2266 else if (sym == Mat_open_square_bracket)
2267 candidates.PrevGroup ();
2268 else if (sym == Mat_close_square_bracket)
2269 candidates.NextGroup ();
2273 internal int marker (MSymbol sym)
2275 int pos = cursor_pos;
2277 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2279 switch (sym.Name[0])
2281 case '<': pos = 0; break;
2282 case '>': pos = preedit.Length; break;
2283 case '-': pos = cursor_pos - 1; break;
2284 case '+': pos = cursor_pos + 1; break;
2289 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2296 if (cursor_pos < preedit.Length - 1)
2299 preedit.FindProp (MInputMethod.Mcandidates, pos,
2303 pos = preedit.Length;
2306 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2311 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2313 pos = int.Parse (sym.Name.Substring (2));
2317 object val = markers.Get (sym);
2325 internal int char_at (int pos)
2332 if (preceding_text.Length < -pos)
2334 MPlist plist = new MPlist ();
2335 plist.Push (MSymbol.integer, pos);
2336 if (MInputMethod.GetSurroundingText != null
2337 && MInputMethod.GetSurroundingText (this, plist)
2339 && preceding_text.Length < plist.Text.Length)
2340 preceding_text = plist.Text;
2342 c = (-pos < preceding_text.Length
2343 ? preceding_text[preceding_text.Length + pos] : -1);
2345 else if (pos >= 0 && pos < preedit.Length)
2349 pos -= preedit.Length;
2350 if (pos >= following_text.Length)
2352 MPlist plist = new MPlist ();
2353 plist.Push (MSymbol.integer, pos + 1);
2354 if (MInputMethod.GetSurroundingText != null
2355 && MInputMethod.GetSurroundingText (this, plist)
2357 && following_text.Length < plist.Text.Length)
2358 following_text = plist.Text;
2360 c = (pos < following_text.Length ? following_text[pos] : -1);
2365 internal void delete (int pos)
2367 if (pos < cursor_pos)
2368 preedit_replace (pos, cursor_pos, null);
2370 preedit_replace (cursor_pos, pos, null);
2371 preedit_changed = true;
2372 cursor_pos_changed = true;
2375 internal void show ()
2377 candidate_show = true;
2378 candidate_changed = true;
2381 internal void hide ()
2383 candidate_show = false;
2384 candidate_changed = true;
2387 internal void move (int pos)
2391 else if (pos > preedit.Length)
2392 pos = preedit.Length;
2393 if (pos != cursor_pos)
2396 preedit_changed = true;
2400 internal void mark (MSymbol sym)
2402 MPlist slot = markers.Find (sym);
2405 markers.Push (sym, cursor_pos);
2407 slot.val = cursor_pos;
2410 internal void pushback (int n)
2423 if (key_head > keys.keyseq.Count)
2424 key_head = keys.keyseq.Count;
2428 internal void pushback (MInputMethod.KeySeq keyseq)
2432 if (key_head < keys.keyseq.Count)
2433 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2434 for (int i = 0; i < keyseq.keyseq.Count; i++)
2435 keys.keyseq.Add (keyseq.keyseq[i]);
2438 internal void pop ()
2440 if (key_head < keys.keyseq.Count)
2441 keys.keyseq.RemoveRange (key_head, 1);
2444 internal void undo (int n)
2447 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2449 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2453 internal void commit ()
2455 produced.Cat (preedit);
2457 preedit_changed = true;
2460 internal void shift (MSymbol sym)
2462 MInputMethod.State state;
2464 if (sym == MSymbol.t)
2466 if (states.Count > 1)
2467 state = states.Pop ();
2469 state = states.Peek ();
2473 state = (MInputMethod.State) im.states.Get (sym);
2475 throw new Exception ("Unknown state: " + state.name);
2478 state = states.Pop ();
2479 if (state == (MInputMethod.State) im.states.val)
2486 state_key_head = key_head;
2487 state_pos = cursor_pos;
2488 state_preedit = preedit.Dup ();
2489 if (state != states.Peek ())
2491 states.Push (state);
2492 state_var_values = domain.SaveValues ();
2493 status = state.title;
2496 status_changed = true;
2498 = (Xex) state.branches.Get (MSymbol.t);
2499 if (on_entry != null)
2500 on_entry.Eval (domain);
2505 internal void reset ()
2508 state_preedit.Del ();
2512 key_head = commit_key_head = 0;
2514 states.Push ((MInputMethod.State) im.states.Val);
2519 internal object GetCandidates (out int column)
2522 if (cursor_pos == 0)
2524 Candidates candidates
2525 = (Candidates) preedit.GetProp (cursor_pos - 1,
2526 MInputMethod.Mcandidates);
2527 if (candidates == null)
2529 column = candidates.Column;
2530 return candidates.Current;
2533 internal void HandleKey ()
2535 MInputMethod.State state = states.Peek ();