2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
14 using Xex = System.Xml.Expression.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (MInputContext ic, MPlist args);
22 public static Callback PreeditStart, PreeditDone, PreeditDraw;
23 public static Callback StatusStart, StatusDone, StatusDraw;
24 public static Callback CandidateStart, CandidateDone, CandidateDraw;
25 public static Callback SetSpot;
26 public static Callback Toggle;
27 public static Callback Reset;
28 public static Callback GetSurroundingText;
29 public static Callback DeleteSurroundingText;
31 internal static Xex.Domain im_domain = new Xex.Domain (null);
32 private static MSymbol Minput_method = "input-method";
33 private static MSymbol Mdescription = "description";
34 private static MSymbol Mvariable = "variable";
35 private static MSymbol Mcommand = "command";
36 private static MSymbol Mmodule = "module";
37 private static MSymbol Mtitle = "title";
38 private static MSymbol Minclude = "include";
39 private static MSymbol Mmacro = "macro";
40 private static MSymbol Mmap = "map";
41 private static MSymbol Mmap_list = "map-list";
42 private static MSymbol Mstate = "state";
43 internal static MSymbol Mcandidates = "candidates";
44 private static MSymbol Minsert = "insert";
45 private static MSymbol Mdelete = "delete";
46 private static MSymbol Mmove = "move";
47 private static MSymbol Mmark = "mark";
48 private static MSymbol Mmarker = "marker";
49 private static MSymbol Mset = "set";
50 private static MSymbol Madd = "add";
51 private static MSymbol Msub = "sub";
52 private static MSymbol Mmul = "mul";
53 private static MSymbol Mdiv = "div";
54 private static MSymbol Mif = "if";
55 private static MSymbol Mcond = "cond";
56 private static MSymbol Mchar_at = "char-at";
57 private static MSymbol Msurrounding_text_p = "surrounding-text-p";
58 private static MSymbol Mpushback = "pushback";
59 private static MSymbol Mkeyseq = "keyseq";
61 private static Xex.Symbol Nprogn = "progn";
62 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
63 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
65 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
66 = new Dictionary<MDatabase.Tag, MInputMethod> ();
68 internal static MInputMethod im_global = null;
71 private enum LoadStatus
81 public enum KeyModifier
87 Control_L = 0x01000000,
88 Control_R = 0x02000000,
104 private static Dictionary<string, uint> keysyms
105 = new Dictionary<string, uint> ();
106 private static Dictionary<string, KeyModifier> keymodifiers
107 = new Dictionary<string, KeyModifier> ();
108 private static uint keysym_base = 0x200000;
109 private static uint char_mask = ~((uint) KeyModifier.All);
113 keysyms["bs"] = keysyms["backspace"] = 0x08;
114 keysyms["tab"] = 0x09;
115 keysyms["lf"] = keysyms["linefeed"] = 0x10;
116 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
117 keysyms["esc"] = keysyms["escape"] = 0x1B;
118 keysyms["spc"] = keysyms["space"] = 0x20;
119 keysyms["del"] = keysyms["delete"] = 0x7F;
120 keymodifiers["shift-l"] = KeyModifier.Shift_L;
121 keymodifiers["shift-r"] = KeyModifier.Shift_R;
122 keymodifiers["shift"] = KeyModifier.Shift;
123 keymodifiers["control-l"] = KeyModifier.Control_L;
124 keymodifiers["control-r"] = KeyModifier.Control_R;
125 keymodifiers["control"] = KeyModifier.Control;
126 keymodifiers["alt-l"] = KeyModifier.Alt_L;
127 keymodifiers["alt-r"] = KeyModifier.Alt_R;
128 keymodifiers["alt"] = KeyModifier.Alt;
129 keymodifiers["altgr"] = KeyModifier.AltGr;
130 keymodifiers["super"] = KeyModifier.Super;
131 keymodifiers["hyper"] = KeyModifier.Hyper;
134 private static uint decode_keysym (MSymbol keysym)
137 string name = keysym.Name;
139 if (name.Length == 1)
141 name = name.ToLower ();
142 if (! keysyms.TryGetValue (name, out key))
143 keysyms[name] = key = keysym_base++;
147 private static uint combine_modifiers (uint c, KeyModifier modifiers)
149 if (c < 0x7F && c != 0x20)
151 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
152 && Char.IsLower ((char) c))
154 modifiers &= ~KeyModifier.Shift;
155 c = Char.ToUpper ((char) c);
157 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
159 modifiers &= ~KeyModifier.Control;
163 return c | (uint) modifiers;
171 public Key (uint c, KeyModifier modifiers)
173 key = combine_modifiers (c, modifiers);
176 public Key (MSymbol keysym, KeyModifier modifiers)
178 key = combine_modifiers (decode_keysym (keysym), modifiers);
181 public Key (MSymbol keysym)
183 string str = keysym.Name;
184 int len = str.Length;
186 KeyModifier modifiers = KeyModifier.None;
188 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
191 modifiers |= KeyModifier.Shift;
192 else if (str[i] == 'C')
193 modifiers |= KeyModifier.Control;
194 else if (str[i] == 'A')
195 modifiers |= KeyModifier.Alt;
196 else if (str[i] == 'G')
197 modifiers |= KeyModifier.AltGr;
198 else if (str[i] == 's')
199 modifiers |= KeyModifier.Super;
200 else if (str[i] == 'H')
201 modifiers |= KeyModifier.Hyper;
204 key = combine_modifiers (str[i], modifiers);
206 key = combine_modifiers (decode_keysym (keysym), modifiers);
209 public Key (MPlist plist)
211 KeyModifier modifiers = KeyModifier.None;
214 for (p = plist; ! p.IsEmpty; p = p.next)
218 if (! p.next.IsEmpty)
219 throw new Exception ("Invalid Key: " + plist);
222 else if (! p.IsSymbol)
223 throw new Exception ("Invalid Key: " + plist);
226 string name = p.Symbol.Name.ToLower ();
229 if (! keymodifiers.TryGetValue (name, out m))
234 if (p.IsEmpty || ! p.next.IsEmpty)
235 throw new Exception ("Invalid Key: " + plist);
237 key = combine_modifiers ((uint) p.Integer, modifiers);
239 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
242 public bool HasModifier
244 get { return ((key & (uint) KeyModifier.All) != 0); }
247 public bool Match (Key k)
251 if ((k.key & char_mask) != (key & char_mask))
253 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
254 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
255 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
256 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
257 && (m2 & KeyModifier.Shift) != KeyModifier.None))
258 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
259 || ((m1 & KeyModifier.Control) == KeyModifier.Control
260 && (m2 & KeyModifier.Control) != KeyModifier.None))
261 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
262 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
263 && (m2 & KeyModifier.Alt) != KeyModifier.None))
264 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
267 public override string ToString ()
269 string str = Char.ToString ((char) key);
270 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
272 if (m != KeyModifier.None)
274 if ((m & KeyModifier.Shift) != KeyModifier.None)
276 if ((m & KeyModifier.Control) != KeyModifier.None)
278 if ((m & KeyModifier.Alt) != KeyModifier.None)
280 if ((m & KeyModifier.AltGr) != KeyModifier.None)
282 if ((m & KeyModifier.Super) != KeyModifier.None)
284 if ((m & KeyModifier.Hyper) != KeyModifier.None)
291 public class KeySeq : Xex.TermValue
293 public List<Key> keyseq = new List<Key> ();
295 public override Xex.TermValue Clone ()
297 KeySeq ks = new KeySeq ();
298 ks.keyseq.InsertRange (0, keyseq);
304 public KeySeq (MPlist plist)
306 foreach (MPlist p in plist)
309 keyseq.Add (new Key (p.Symbol));
310 else if (p.IsInteger)
311 keyseq.Add (new Key ((char) p.Integer));
313 keyseq.Add (new Key (p.Plist));
315 throw new Exception ("Invalid Key Sequence: " + plist);
319 public KeySeq (MText mt) : base ()
321 for (int i = 0; i < mt.Length; i++)
322 keyseq.Add (new Key ((uint) mt[i]));
325 private static uint parse_integer (string str)
327 if (Char.IsDigit (str[0]))
329 if (str[0] == '0' && str.Length > 2 && str[1] == 'x')
332 for (int idx = 2; idx < str.Length; idx++)
335 if (c >= '0' && c <= '9')
336 i = i * 16 + (c - '0');
337 else if (c >= 'A' && c <= 'F')
338 i = i * 16 + 10 + (c - 'A');
339 else if (c >= 'a' && c <= 'f')
340 i = i * 16 + 10 + (c - 'a');
346 return UInt32.Parse (str);
348 else if (str[0] == '?')
353 public KeySeq (XmlNode node)
355 XmlAttributeCollection acol = node.Attributes;
358 if (acol != null && (n = acol["keys"]) != null)
360 foreach (char c in n.Value)
361 keyseq.Add (new Key ((uint) c));
364 for (node = node.FirstChild; node != null; node = node.NextSibling)
366 if (node.Name == "key-event")
367 keyseq.Add (new Key ((MSymbol) node.InnerText));
369 keyseq.Add (new Key (parse_integer (node.InnerText)));
373 public KeySeq (Xex.Term[] args)
375 foreach (Xex.Term term in args)
378 keyseq.Add (new Key ((MSymbol) ((string) term.Symval)));
380 keyseq.Add (new Key (term.Strval));
384 public override string ToString ()
388 foreach (Key key in keyseq)
392 foreach (Key k in keyseq)
393 str += " " + k.ToString ();
397 foreach (Key key in keyseq)
398 str += key.ToString ();
403 public class Variable
406 public MText description;
409 public object[] candidates;
411 public Variable (MPlist p)
415 description = parse_description (p);
416 if (description == null)
417 description = new MText ("No description");
420 type = (p.IsMText ? typeof (MText)
421 : p.IsInteger ? typeof (int)
422 : p.IsSymbol ? typeof (MSymbol)
426 candidates = new object[p.Count];
427 for (int i = 0; ! p.IsEmpty; i++, p = p.next)
428 candidates[i] = p.val;
431 private static Type parse_value (XmlNode node, out object value)
433 string typename = node.Attributes["type"].Value;
436 if (typename == "integer")
439 if (! Int32.TryParse (node.InnerText, out i))
444 else if (typename == "string")
446 MText mt = node.InnerText;
448 type = typeof (MText);
450 else if (typename == "symbol")
452 MSymbol sym = node.InnerText;
454 type = typeof (MSymbol);
459 type = typeof (object);
464 public Variable (XmlNode node)
466 name = node.Attributes["id"].Value;
467 for (node = node.FirstChild; node != null; node = node.NextSibling)
468 if (node.NodeType == XmlNodeType.Element)
470 if (node.Name == "description")
471 description = parse_description (node);
472 else if (node.Name == "value")
473 type = parse_value (node, out value);
474 else if (node.Name == "valiable-value-candidate")
476 XmlNodeList n_list = node.ChildNodes;
477 candidates = new object[n_list.Count];
478 for (int i = 0; i < n_list.Count; i++)
481 parse_value (n_list[i], out val);
488 public override string ToString ()
490 return ("(" + name + " \"" + (string) description
491 + "\" " + type + " " + value + " " + candidates + ")");
498 public MText description;
499 public List<KeySeq> keys;
501 public Command (MPlist p)
505 description = parse_description (p);
506 if (description == null)
507 description = "No description";
508 keys = new List<KeySeq> ();
509 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")
528 keys.Add (new KeySeq (node));
532 public override string ToString ()
534 string str = "(" + name + " \"" + (string) description;
535 foreach (KeySeq keyseq in keys)
541 internal class Plugin
544 private Assembly assembly;
545 private Type plugin_type;
547 public Plugin (string name)
552 public MethodInfo GetMethod (Xex.Symbol name)
554 if (assembly == null)
556 assembly = Assembly.LoadFrom (name + ".dll");
557 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
560 MethodInfo info = plugin_type.GetMethod ((string) name);
562 throw new Exception ("Invalid plugin method: " + name);
566 public override string ToString ()
568 return String.Format ("(module {0}", name);
572 internal class PluginMethod : Xex.Function
574 private Plugin plugin;
575 private MethodInfo method_info;
576 object[] parameters = new object[2];
578 public PluginMethod (Plugin plugin, string name)
579 : base ((Xex.Symbol) name, 0, -1)
581 this.plugin = plugin;
584 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
587 args = (Xex.Term[]) args.Clone ();
588 for (int i = 0; i < args.Length; i++)
590 args[i] = args[i].Eval (domain);
594 if (method_info == null)
595 method_info = plugin.GetMethod (name);
596 parameters[0] = domain.context;
597 parameters[1] = args;
598 return (Xex.Term) method_info.Invoke (null, parameters);
602 internal abstract class Marker : Xex.TermValue
606 public Marker (MSymbol name)
611 public abstract int Position (MInputContext ic);
612 public abstract void Mark (MInputContext ic);
614 public class Named : Marker
618 public Named (MSymbol name) : this (name, 0) { }
620 private Named (MSymbol name, int p) : base (name) { pos = p; }
622 public override int Position (MInputContext ic) { return pos; }
624 public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
626 public override Xex.TermValue Clone ()
628 return new Named (name, pos);
632 public class Predefined : Marker
634 public Predefined (MSymbol name) : base (name) { }
636 public override int Position (MInputContext ic)
638 switch (name.Name[1]) {
640 case '>': return ic.preedit.Length;
641 case '-': return ic.cursor_pos - 1;
642 case '+': return ic.cursor_pos + 1;
644 if (ic.cursor_pos > 0)
646 int pos = ic.cursor_pos;
648 ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
654 if (ic.cursor_pos < ic.preedit.Length - 1)
656 int pos = ic.cursor_pos;
658 ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
662 return ic.preedit.Length;
664 return name.Name[1] - '0';
668 public override void Mark (MInputContext ic)
670 throw new Exception ("Can't set predefined marker: " + name);
673 public override Xex.TermValue Clone ()
675 return new Predefined (name);
679 static internal Dictionary<MSymbol,Predefined> predefined_markers;
683 predefined_markers = new Dictionary<MSymbol,Predefined> ();
684 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
685 "@0", "@1", "@2", "@3", "@4",
686 "@5", "@6", "@7", "@8", "@9" };
687 foreach (MSymbol s in symlist)
688 predefined_markers[s] = new Predefined (s);
691 public static Marker Get (MInputContext ic, MSymbol name)
696 if (predefined_markers.TryGetValue (name, out pred))
698 if (name.Name[0] == '@')
699 throw new Exception ("Invalid marker name: " + name);
700 m = (Marker) ic.markers.Get (name);
703 m = new Named (name);
704 ic.markers.Put (name, m);
713 public Dictionary<Key, Map> submaps;
714 public Xex.Term actions;
716 public void Add (KeySeq keys, int index, Xex.Term actions)
721 submaps = new Dictionary<Key, Map> ();
723 submaps.TryGetValue (keys.keyseq[index], out sub);
726 Key key = keys.keyseq[index];
727 submaps[key] = sub = new Map ();
729 if (index + 1 < keys.keyseq.Count)
730 sub.Add (keys, index + 1, actions);
732 this.actions = actions;
735 public Xex.Term Lookup (KeySeq keys, int index)
739 if (index + 1 == keys.keyseq.Count)
741 if (submaps.TryGetValue (keys.keyseq[index], out sub))
742 return sub.Lookup (keys, index + 1);
746 private void describe (MText mt, KeySeq keyseq)
748 if (keyseq.keyseq.Count > 0)
750 mt.Cat (" (").Cat (keyseq.ToString ());
751 mt.Cat (' ').Cat (actions.ToString ());
755 foreach (KeyValuePair<Key, Map> kv in submaps)
757 keyseq.keyseq.Add (kv.Key);
758 kv.Value.describe (mt, keyseq);
759 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
763 public override string ToString ()
765 MText mt = "(" + name.Name;
766 KeySeq keyseq = new KeySeq ();
768 describe (mt, keyseq);
778 public MPlist branches = new MPlist ();
780 public State (MSymbol name)
785 public override string ToString ()
787 MText mt = "(" + name.Name;
790 mt.Cat (" \"" + title + "\"");
791 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
792 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
793 return (string) mt + ")";
798 internal Xex.Domain domain = new Xex.Domain (im_domain, null);
800 private LoadStatus load_status = LoadStatus.None;
801 private MDatabase.Tag tag;
802 private MDatabase mdb;
804 private MText description;
805 internal MText title;
806 internal Command[] commands;
807 internal Xex.Symbol[] var_names;
808 internal Dictionary<MSymbol, Plugin> plugins;
809 internal Dictionary<MSymbol, Map> maps;
810 internal MPlist states;
812 static MInputMethod ()
814 im_domain.DefSubr (Finsert, "insert", true, 1, 1);
815 im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
816 im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
817 im_domain.DefSubr (Fselect, "select", true, 1, 1);
818 im_domain.DefSubr (Fshow, "show", true, 0, 0);
819 im_domain.DefSubr (Fhide, "hide", true, 0, 0);
820 im_domain.DefSubr (Fmove, "move", true, 1, 1);
821 im_domain.DefSubr (Fmark, "mark", true, 1, 1);
822 im_domain.DefSubr (Fpushback, "pushback", true, 1, 1);
823 im_domain.DefSubr (Fpop, "pop", true, 0, 0);
824 im_domain.DefSubr (Fundo, "undo", true, 0, 1);
825 im_domain.DefSubr (Fcommit, "commit", true, 0, 0);
826 im_domain.DefSubr (Funhandle, "unhandle", true, 0, 0);
827 im_domain.DefSubr (Fshift, "shift", true, 1, 1);
828 im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
829 im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
830 im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
832 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
833 List<MDatabase> list = MDatabase.List (tag);
834 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
835 foreach (MDatabase mdb in list)
836 im_table[mdb.tag] = new MInputMethod (mdb.tag);
840 private MInputMethod (MDatabase.Tag tag)
845 // Instance Properties
846 public MSymbol Language { get { return tag[1]; } }
847 public MSymbol Name { get { return tag[2]; } }
848 public MSymbol SubName { get { return tag[3]; } }
850 public bool Info (out MText description,
852 out Xex.Variable[] variables,
853 out Command[] commands)
855 if ((load_status & LoadStatus.Header) != LoadStatus.Header
864 description = this.description;
866 if (var_names == null)
870 variables = new Xex.Variable[var_names.Length];
872 foreach (Xex.Symbol name in var_names)
873 variables[i++] = domain.GetVar (name, false);
875 commands = this.commands;
879 public static MInputMethod Find (MSymbol language, MSymbol name)
881 return Find (language, name, MSymbol.nil);
884 public static MInputMethod Find (MSymbol language, MSymbol name,
887 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
891 return (im_table.TryGetValue (tag, out im) ? im : null);
896 return ((load_status == LoadStatus.Full) || load_body ());
899 public static MInputMethod[] List ()
901 MInputMethod[] array = new MInputMethod[im_table.Count];
904 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
905 array[i++] = kv.Value;
909 private bool load_header ()
911 mdb = MDatabase.Find (tag);
914 mdb.name_table = Xex.Symbol.Table;
916 MSymbol format = mdb.Format;
918 if (format == MSymbol.plist)
919 load ((MPlist) mdb.Load (Mmap), false);
922 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
923 load (doc.DocumentElement, false);
925 } catch (Exception e) {
926 Console.WriteLine ("{0}\n", e);
927 load_status = LoadStatus.Error;
930 load_status |= LoadStatus.Header;
934 private bool load_body ()
936 mdb = MDatabase.Find (tag);
939 mdb.name_table = Xex.Symbol.Table;
941 object obj = mdb.Load ();
943 load ((MPlist) obj, true);
945 load ((XmlDocument) obj, true);
946 } catch (Exception e) {
947 Console.WriteLine (e);
948 load_status = LoadStatus.Error;
951 load_status = LoadStatus.Full;
955 private void load (MPlist plist, bool full)
957 maps = new Dictionary<MSymbol, Map> ();
958 states = new MPlist ();
960 for (; ! plist.IsEmpty; plist = plist.next)
963 MPlist pl = plist.Plist;
966 MSymbol sym = pl.Symbol;
969 if (sym == Mdescription)
970 description = parse_description (pl);
971 else if (sym == Mtitle)
976 else if (sym == Mvariable)
977 parse_variables (pl);
978 else if (sym == Mcommand)
984 else if (sym == Minclude)
986 else if (sym == Mmacro)
988 else if (sym == Mmap)
990 else if (sym == Mstate)
995 if (description == null)
996 description = (MText) "No description";
998 title = new MText (tag[2].Name);
999 if (commands == null)
1000 commands = new Command[0];
1005 State state = new State ((MSymbol) "init");
1006 plist = new MPlist ();
1007 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1008 state.branches.Add (kv.Key, null);
1009 states.Add (state.name, state);
1013 private void load (XmlNode node, bool full)
1015 bool skip_header = load_status == LoadStatus.Header;
1017 maps = new Dictionary<MSymbol, Map> ();
1018 states = new MPlist ();
1020 if (node.NodeType == XmlNodeType.Document)
1021 node = node.FirstChild;
1022 while (node.NodeType != XmlNodeType.Element)
1023 node = node.NextSibling;
1024 for (node = node.FirstChild; node != null; node = node.NextSibling)
1026 if (node.NodeType != XmlNodeType.Element)
1030 if (node.Name == "description")
1031 description = parse_description (node);
1032 else if (node.Name == "title")
1033 title = parse_title (node);
1034 else if (node.Name == "variable-list")
1035 parse_variables (node);
1036 else if (node.Name == "command-list")
1037 parse_commands (node);
1041 if (node.Name == "module-list")
1042 parse_plugins (node);
1043 else if (node.Name == "macro-list")
1044 parse_macros (node);
1045 else if (node.Name == "map-list")
1047 else if (node.Name == "state-list")
1048 parse_states (node);
1051 if (description == null)
1052 description = (MText) "No description";
1054 title = new MText (tag[2].Name);
1055 if (commands == null)
1056 commands = new Command[0];
1061 State state = new State ((MSymbol) "init");
1062 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1063 state.branches.Add (kv.Key, null);
1064 states.Add (state.name, state);
1068 private static void transform (MPlist plist)
1070 for (; ! plist.IsEmpty; plist = plist.next)
1074 MPlist p = new MPlist ();
1075 p.Add (MSymbol.symbol, Minsert);
1076 p.Add (MSymbol.mtext, plist.Text);
1077 plist.Set (MSymbol.plist, p);
1079 else if (plist.IsInteger)
1081 MPlist p = new MPlist ();
1082 p.Add (MSymbol.symbol, Minsert);
1083 p.Add (MSymbol.integer, plist.Integer);
1084 plist.Set (MSymbol.plist, p);
1086 else if (plist.IsPlist)
1088 MPlist pl = plist.Plist;
1092 if (pl.Symbol == Madd)
1093 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1094 else if (pl.Symbol == Msub)
1095 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1096 else if (pl.Symbol == Mmul)
1097 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1098 else if (pl.Symbol == Mdiv)
1099 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1100 else if (pl.Symbol == Minsert)
1102 // (insert (CANDIDATES ...))
1103 // => (candidates CANDIDATES ...)
1104 if (pl.next.IsPlist)
1106 pl.Set (MSymbol.symbol, Mcandidates);
1108 MPlist p = pl.Plist;
1109 pl.Set (p.key, p.val);
1110 for (p = p.next; ! p.IsEmpty; p = p.next);
1111 pl.Add (p.key, p.val);
1114 else if (pl.Symbol == Mif)
1118 transform (pl.next);
1120 else if (pl.Symbol == Mcond)
1122 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1125 MPlist p = pl.Plist;
1133 else if (pl.Symbol == Mdelete
1134 || pl.Symbol == Mmove
1135 || pl.Symbol == Mmark)
1140 MSymbol sym = pl.Symbol;
1141 MPlist p = new MPlist ();
1142 p.Add (MSymbol.symbol, Mmarker);
1143 p.Add (MSymbol.symbol, sym);
1144 pl.Set (MSymbol.plist, p);
1147 else if (pl.Symbol == Mpushback)
1151 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1154 else if (pl.IsMText)
1156 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1157 pl.Push (MSymbol.symbol, Mcandidates);
1160 else if (plist.IsSymbol)
1162 MSymbol sym = plist.Symbol;
1164 if (sym.Name.Length >= 3
1165 && sym.Name[0] == '@'
1166 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1168 int pos = int.Parse (sym.Name.Substring (1));
1169 MPlist p = new MPlist ();
1173 p.Add (MSymbol.symbol, Msurrounding_text_p);
1177 if (sym.Name[1] == '+')
1179 p.Add (MSymbol.symbol, Mchar_at);
1180 p.Add (MSymbol.integer, pos);
1182 plist.Set (MSymbol.plist, p);
1188 private static MText parse_description (MPlist plist)
1194 plist = plist.Plist;
1195 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1196 && plist.next.IsMText)
1197 return plist.next.Text;
1202 private static MText parse_description (XmlNode node)
1204 if (node.HasChildNodes)
1205 node = node.FirstChild;
1206 return node.InnerText;
1209 private static MText parse_title (XmlNode node)
1211 return node.InnerText;
1214 private void new_variable (Xex.Symbol name, string desc, int val,
1215 MPlist pl, Xex.Variable vari)
1223 range = new int[pl.Count * 2];
1224 for (int i = 0; i < range.Length; i++)
1228 MPlist p = pl.Plist;
1230 if (! p.IsInteger || ! p.next.IsInteger)
1231 throw new Exception ("Invalid range: " + p);
1232 range[i * 2] = p.Integer;
1233 range[i * 2 + 1] = p.next.Integer;
1235 else if (pl.IsInteger)
1236 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1238 throw new Exception ("Invalid range: " + pl);
1242 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1245 Xex.Term term = new Xex.Term (val);
1247 vari.DefaultValue = term;
1252 private void new_variable (Xex.Symbol name, string desc, MText val,
1253 MPlist pl, Xex.Variable vari)
1261 range = new string[pl.Count * 2];
1262 for (int i = 0; i < range.Length; i++)
1265 range[i] = (string) pl.Text;
1267 throw new Exception ("Invalid range: " + pl);
1271 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1274 Xex.Term term = new Xex.Term ((string) val);
1276 vari.DefaultValue = term;
1281 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1282 MPlist pl, Xex.Variable vari)
1285 Xex.Symbol sym = val.Name;
1291 range = new Xex.Symbol[pl.Count * 2];
1292 for (int i = 0; i < range.Length; i++)
1295 range[i] = pl.Symbol.Name;
1297 throw new Exception ("Invalid range: " + pl);
1301 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1304 Xex.Term term = new Xex.Term (sym);
1306 vari.DefaultValue = term;
1311 private Xex.Variable get_global_var (Xex.Symbol name)
1313 if (im_global == null || this != im_global)
1315 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1317 im_global = im_table[tag];
1318 if (! im_global.Open ())
1319 throw new Exception ("Failed to load global");
1321 return im_global.domain.GetVar (name, false);
1324 private void parse_variables (MPlist plist)
1326 var_names = new Xex.Symbol[plist.Count];
1328 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1330 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1331 throw new Exception ("Invalid variable: " + plist);
1333 MPlist p = plist.Plist;
1334 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1335 var_names[i] = name;
1337 string desc = (string) parse_description (p);
1338 Xex.Variable vari = get_global_var (name);
1340 domain.Defvar (vari);
1346 new_variable (name, desc, p.Integer, p.next, vari);
1348 new_variable (name, desc, p.Text, p.next, vari);
1349 else if (p.IsSymbol)
1350 new_variable (name, desc, p.Symbol, p.next, vari);
1352 throw new Exception ("Invalid variable type: " + p.val);
1357 private void parse_variables (XmlNode node)
1359 XmlNodeList node_list = node.ChildNodes;
1361 var_names = new Xex.Symbol[node_list.Count];
1362 for (int i = 0; i < node_list.Count; i++)
1364 Xex.Symbol name = node_list[i].Attributes[0].Value;
1365 Xex.Variable vari = get_global_var (name);
1367 domain.Defvar (vari);
1368 domain.Defvar (node_list[i]);
1369 var_names[i] = name;
1373 private void parse_commands (MPlist plist)
1375 commands = new Command[plist.Count];
1377 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1378 if (plist.IsPlist && plist.Plist.IsSymbol)
1379 commands[i++] = new Command (plist.Plist);
1382 private void parse_commands (XmlNode node)
1384 XmlNodeList node_list = node.ChildNodes;
1386 commands = new Command[node_list.Count];
1387 for (int i = 0; i < node_list.Count; i++)
1389 if (node_list[i].NodeType == XmlNodeType.Element)
1390 commands[i] = new Command (node_list[i]);
1394 private void parse_plugins (MPlist plist)
1396 plugins = new Dictionary<MSymbol, Plugin> ();
1398 for (; ! plist.IsEmpty; plist = plist.Next)
1400 MPlist p = plist.Plist;
1401 MSymbol sym = p.Symbol;
1402 Plugin plugin = new Plugin (sym.Name);
1404 for (p = p.next; ! p.IsEmpty; p = p.next)
1406 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1407 domain.Defun (func);
1412 private void parse_plugins (XmlNode node)
1414 plugins = new Dictionary<MSymbol, Plugin> ();
1416 foreach (XmlNode n in node.ChildNodes)
1418 Plugin plugin = new Plugin (n.Attributes[0].Value);
1419 foreach (XmlNode nn in n.ChildNodes)
1421 Xex.Function func = new PluginMethod (plugin,
1422 nn.Attributes[0].Value);
1423 domain.Defun (func);
1428 private void parse_macros (XmlNode node)
1430 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1431 if (nn.NodeType == XmlNodeType.Element)
1432 domain.Defun (nn, true);
1433 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1434 if (nn.NodeType == XmlNodeType.Element)
1435 domain.Defun (nn, false);
1438 private void parse_maps (XmlNode node)
1442 private void parse_states (XmlNode node)
1446 private void parse_include (MPlist plist)
1448 if (! plist.IsPlist)
1450 MPlist p = plist.Plist;
1451 MSymbol language, name, subname;
1452 language = p.Symbol;
1455 name = subname = MSymbol.nil;
1461 subname = MSymbol.nil;
1466 MInputMethod im = MInputMethod.Find (language, name, subname);
1472 if (! plist.IsSymbol)
1474 MSymbol target_type = plist.Symbol;
1476 MSymbol target_name = MSymbol.nil;
1478 target_name = plist.Symbol;
1479 if (target_type == Mmacro)
1481 if (target_name == MSymbol.nil)
1482 im.domain.CopyFunc (domain);
1484 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1486 else if (target_type == Mmap)
1488 if (target_name == MSymbol.nil)
1490 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1491 maps[kv.Key] = kv.Value;
1496 if (im.maps.TryGetValue (target_name, out map))
1497 maps[target_name] = map;
1500 else if (target_type == Mstate)
1502 if (target_name == MSymbol.nil)
1504 for (p = im.states; ! p.IsEmpty; p = p.next)
1505 states.Add (p.key, p.val);
1509 object state = im.states.Get (target_name);
1511 states.Add (target_name, state);
1516 private Xex.Term parse_cond (MPlist plist)
1518 Xex.Term[] args = new Xex.Term[plist.Count];
1520 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1522 if (! plist.IsPlist)
1523 throw new Exception ("Invalid cond args: " + plist);
1524 MPlist p = plist.Plist;
1525 List<Xex.Term> arg = new List<Xex.Term> (parse_action (p));
1526 args[i] = new Xex.Term (arg);
1528 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1531 private Xex.Term[] parse_action (MPlist plist)
1533 Xex.Term[] terms = new Xex.Term[plist.Count];
1535 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1538 terms[i] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1539 else if (plist.IsMText)
1540 terms[i] = new Xex.Term ((string) plist.Text);
1541 else if (plist.IsInteger)
1542 terms[i] = new Xex.Term (plist.Integer);
1543 else if (plist.IsPlist)
1545 MPlist p = plist.Plist;
1547 throw new Exception ("Invalid action: " + p);
1548 MSymbol name = p.Symbol;
1551 terms[i] = parse_cond (p);
1552 else if (name == Mset || name == Madd || name == Msub
1553 || name == Mmul || name == Mdiv)
1556 throw new Exception ("Invalid action: " + p);
1557 Xex.Symbol varname = p.Symbol.Name;
1558 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1559 varname, parse_action (p.next));
1562 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1566 throw new Exception ("Invalid action: " + plist);
1572 private void parse_macros (MPlist plist)
1574 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1577 MPlist p = pl.Plist;
1581 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
1583 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1586 MPlist p = pl.Plist;
1591 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
1592 parse_action (p.next), false);
1596 private void parse_maps (MPlist plist)
1598 for (; ! plist.IsEmpty; plist = plist.next)
1601 MPlist pl = plist.Plist;
1605 Map map = new Map ();
1606 map.name = pl.Symbol;
1607 maps[map.name] = map;
1608 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1612 MPlist p = pl.Plist;
1615 keys = new KeySeq (p.Text);
1617 keys = new KeySeq (p.Plist);
1624 new Xex.Term (domain, Nprogn, parse_action (p)));
1629 private void parse_states (MPlist plist)
1631 for (; ! plist.IsEmpty; plist = plist.next)
1634 MPlist pl = plist.Plist;
1645 State state = new State (pl.Symbol);
1646 state.title = title;
1648 states = new MPlist ();
1649 states.Add (state.name, state);
1650 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1654 MPlist p = pl.Plist;
1657 MSymbol map_name = p.Symbol;
1659 Xex.Term term = new Xex.Term (domain, Nprogn, parse_action (p));
1660 state.branches.Add (map_name, term);
1665 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1668 ((MInputContext) domain.context).insert (args[0]);
1672 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1676 ((MInputContext) domain.context).insert_candidates (args[0]);
1680 private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
1683 MSymbol name = (string) args[0].Symval;
1684 return new Xex.Term (Marker.Get ((MInputContext) domain.context, name));
1687 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1690 int c = ((MInputContext) domain.context).char_at (args[0].Intval);
1691 return new Xex.Term (c);
1694 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1697 ((MInputContext) domain.context).delete ((int) args[0].Intval);
1701 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1704 MInputContext ic = (MInputContext) domain.context;
1707 ic.select (args[0].Intval);
1709 ic.select ((MSymbol) ((string) args[0].Symval));
1713 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1716 ((MInputContext) domain.context).show ();
1720 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1723 ((MInputContext) domain.context).hide ();
1727 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1731 ((MInputContext) domain.context).move (args[0].Intval);
1734 Marker m = (Marker) args[0].Objval;
1735 MInputContext ic = (MInputContext) domain.context;
1736 ((MInputContext) domain.context).move (m.Position (ic));
1741 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1744 Marker m = (Marker) args[0].Objval;
1745 m.Mark ((MInputContext) domain.context);
1749 private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
1752 return new Xex.Term (new KeySeq (args));
1755 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1758 MInputContext ic = (MInputContext) domain.context;
1761 ic.pushback (args[0].Intval);
1762 else if (args[0].IsStr)
1763 ic.pushback (new KeySeq (args[0].Strval));
1765 ic.pushback ((KeySeq) args[0].Objval);
1769 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1772 ((MInputContext) domain.context).pop ();
1776 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1779 int n = args.Length == 0 ? -2 : args[0].Intval;
1780 ((MInputContext) domain.context).undo (n);
1784 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1787 ((MInputContext) domain.context).commit ();
1791 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1794 ((MInputContext) domain.context).commit ();
1795 args = new Xex.Term[2];
1796 args[0] = args[1] = Tcatch_tag;
1797 return Xex.Fthrow (domain, vari, args);
1800 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
1803 ((MInputContext) domain.context).shift (args[0].Symval);
1807 public override string ToString ()
1809 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
1810 if (commands != null)
1812 str += " (commands";
1813 foreach (Command cmd in commands)
1817 if (var_names != null)
1819 str += " (variables";
1820 foreach (Xex.Symbol var in var_names)
1824 if (plugins != null)
1827 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
1828 str += " " + kv.Value;
1832 foreach (KeyValuePair<MSymbol, Map> kv in maps)
1833 str += " " + kv.Value;
1835 foreach (MPlist p in states)
1841 public class MInputContext
1843 internal static Xex.Symbol Ncandidates_group_size = "candidates-group-size";
1844 private static MSymbol Mat_less_than = "@<";
1845 private static MSymbol Mat_greater_than = "@>";
1846 private static MSymbol Mat_minus = "@-";
1847 private static MSymbol Mat_plus = "@+";
1848 private static MSymbol Mat_open_square_bracket = "@[";
1849 private static MSymbol Mat_close_square_bracket = "@]";
1851 public MInputMethod im;
1852 private MText produced;
1853 private bool active;
1854 private MText status;
1855 internal MText preedit;
1856 internal int cursor_pos;
1857 private Candidates candidates;
1858 private MPlist candidate_group;
1859 private int candidate_index;
1860 private int candidate_from, candidate_to;
1861 private bool candidate_show;
1863 private Stack<MInputMethod.State> states;
1864 internal MInputMethod.KeySeq keys;
1865 private int key_head;
1866 private int state_key_head;
1867 private object state_var_values;
1868 private int commit_key_head;
1869 private MText state_preedit;
1870 private int state_pos;
1871 internal MPlist markers = new MPlist ();
1872 internal MText preceding_text = new MText ();
1873 internal MText following_text = new MText ();
1874 private bool key_unhandled;
1876 internal Xex.Domain domain;
1879 public enum ChangedStatus
1885 CandidateList = 0x08,
1886 CandidateIndex = 0x10,
1887 CandidateShow = 0x20,
1890 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
1891 | ChangedStatus.CandidateIndex
1892 | ChangedStatus.CandidateShow);
1893 private ChangedStatus changed;
1895 public ChangedStatus Changed { get { return changed; } }
1897 public MInputContext (MInputMethod im)
1900 domain = new Xex.Domain (im.domain, this);
1901 states = new Stack<MInputMethod.State> ();
1902 states.Push ((MInputMethod.State) im.states.val);
1903 keys = new MInputMethod.KeySeq ();
1906 private void adjust_markers (int from, int to, object inserted)
1908 int ins = (inserted == null ? 0
1909 : inserted is int ? 1
1910 : ((MText) inserted).Length);
1911 int diff = ins - (to - from);
1913 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
1915 int pos = plist.Integer;
1919 plist.val = pos + diff;
1924 if (cursor_pos >= to)
1926 else if (cursor_pos > from)
1930 private void preedit_replace (int from, int to, int c)
1932 preedit.Del (from, to);
1933 preedit.Ins (from, c);
1934 adjust_markers (from, to, c);
1937 private void preedit_replace (int from, int to, MText mt)
1939 preedit[from, to] = mt;
1940 adjust_markers (from, to, mt);
1943 internal void insert (Xex.Term arg)
1946 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
1948 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
1949 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
1952 private class Candidates
1959 public Block (int index, Xex.Term term)
1963 Data = (MText) term.Strval;
1966 MPlist plist = new MPlist ();
1968 foreach (Xex.Term t in term.Listval)
1969 p = p.Add (MSymbol.mtext, (MText) t.Strval);
1974 public Block (int index, MPlist plist)
1979 else if (plist.IsPlist)
1982 throw new Exception ("Invalid candidate: " + plist);
1987 get { return (Data is MText
1988 ? ((MText) Data).Length
1989 : ((MPlist) Data).Count); }
1992 public object this[int i]
1995 if (Data is MText) return ((MText) Data)[i];
1996 return ((MPlist) Data)[i];
2001 private Block[] blocks;
2002 private int row = 0;
2003 private int index = 0;
2004 public object[] group;
2006 private bool IsFixed { get { return group != null; } }
2009 Block last = blocks[blocks.Length - 1];
2010 return last.Index + last.Count; }
2014 get { return (IsFixed ? index % group.Length
2015 : index - blocks[row].Index); }
2018 public object Group {
2019 get { return (IsFixed ? group : blocks[row].Data); }
2022 public int GroupLength
2027 int nitems = group.Length;
2028 int start = index - (index % nitems);
2030 return (start + nitems <= total ? nitems : total - start);
2032 return blocks[row].Count;
2036 public object Current {
2038 return (IsFixed ? group[index % group.Length]
2039 : blocks[row][index - blocks[row].Index]);
2043 public Candidates (MPlist list, int column)
2045 int nblocks = list.Count;
2047 blocks = new Block[nblocks];
2048 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
2049 start += (blocks[i] = new Block (index, list)).Count;
2051 group = new object[column];
2054 public Candidates (List<Xex.Term> list, int column)
2056 int nblocks = list.Count;
2058 blocks = new Block[nblocks];
2059 for (int i = 0, start = 0; i < nblocks; i++)
2060 start += (blocks[i] = new Block (index, list[i])).Count;
2062 group = new object[column];
2065 public static void Detach (MInputContext ic)
2067 ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
2068 ic.candidates = null;
2069 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2073 // Fill the array "group" by candidates stating from INDEX.
2074 // INDEX must be a multiple of "column". Set NTIMES to the
2075 // number of valid candidates in "group". Update "block" if
2076 // necessary. Return "group".
2078 private int fill_group (int start)
2080 int nitems = group.Length;
2082 Block b = blocks[r];
2084 if (start < b.Index)
2085 while (start < b.Index)
2088 while (start >= b.Index + b.Count)
2092 int count = b.Count;
2094 for (int i = 0; i < nitems; i++, start++)
2099 if (r == blocks.Length)
2105 group[i] = b[start];
2110 // Update "row" to what contains the first candidate of
2111 // the previous candidate-group, update "current_index", and
2112 // update "group" if necessary. Return the previous
2113 // candidate-group. Set NITEMS to the number of valid
2114 // candidates contained in that group.
2116 public int PrevGroup ()
2123 nitems = group.Length;
2124 if ((index -= col + nitems) < 0)
2125 index = (Total / nitems) * nitems;
2126 nitems = fill_group (index);
2130 row = row > 0 ? row-- : blocks.Length - 1;
2131 nitems = blocks[row].Count;
2132 index = blocks[row].Index;
2134 index += col < nitems ? col : nitems - 1;
2138 public int NextGroup ()
2145 nitems = group.Length;
2146 if ((index += nitems - col) >= Total)
2148 nitems = fill_group (index);
2152 row = row < blocks.Length - 1 ? row + 1 : 0;
2153 nitems = blocks[row].Count;
2154 index = blocks[row].Count;
2156 index += col < nitems ? col : nitems - 1;
2166 int nitems = PrevGroup ();
2167 index += col < nitems - 1 ? col : nitems - 1;
2176 int nitems = GroupLength;
2178 if (col == nitems - 1)
2180 nitems = NextGroup ();
2187 public void First ()
2194 index += GroupLength - (Column + 1);
2197 public void Select (int col)
2199 int maxcol = GroupLength - 1;
2202 index = index - Column + col;
2206 private void update_candidate ()
2208 object candidate = candidates.Current;
2210 if (candidate is MText)
2212 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2213 candidate_to = candidate_from + ((MText) candidate).Length;
2217 preedit_replace (candidate_from, candidate_to, (int) candidate);
2218 candidate_to = candidate_from + 1;
2220 preedit.PushProp (candidate_from, candidate_to,
2221 MInputMethod.Mcandidates, this);
2222 cursor_pos = candidate_from;
2223 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2227 internal void insert_candidates (Xex.Term arg)
2230 Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
2233 column = v.Value.Intval;
2234 candidates = new Candidates (arg.Listval, column);
2235 candidate_from = candidate_to = cursor_pos;
2236 update_candidate ();
2239 internal void select (int n)
2241 if (candidates != null)
2243 candidates.Select (n);
2244 update_candidate ();
2248 internal void select (MSymbol sym)
2250 if (candidates != null)
2252 if (sym == Mat_less_than)
2253 candidates.First ();
2254 else if (sym == Mat_greater_than)
2256 else if (sym == Mat_minus)
2258 else if (sym == Mat_plus)
2260 else if (sym == Mat_open_square_bracket)
2261 candidates.PrevGroup ();
2262 else if (sym == Mat_close_square_bracket)
2263 candidates.NextGroup ();
2267 internal int marker (MSymbol sym)
2269 int pos = cursor_pos;
2271 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2273 switch (sym.Name[0])
2275 case '<': pos = 0; break;
2276 case '>': pos = preedit.Length; break;
2277 case '-': pos = cursor_pos - 1; break;
2278 case '+': pos = cursor_pos + 1; break;
2283 preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
2290 if (cursor_pos < preedit.Length - 1)
2293 preedit.FindProp (MInputMethod.Mcandidates, pos,
2297 pos = preedit.Length;
2300 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2305 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2307 pos = int.Parse (sym.Name.Substring (2));
2311 object val = markers.Get (sym);
2319 internal int char_at (int pos)
2326 if (preceding_text.Length < -pos)
2328 MPlist plist = new MPlist ();
2329 plist.Push (MSymbol.integer, pos);
2330 if (MInputMethod.GetSurroundingText != null
2331 && MInputMethod.GetSurroundingText (this, plist)
2333 && preceding_text.Length < plist.Text.Length)
2334 preceding_text = plist.Text;
2336 c = (-pos < preceding_text.Length
2337 ? preceding_text[preceding_text.Length + pos] : -1);
2339 else if (pos >= 0 && pos < preedit.Length)
2343 pos -= preedit.Length;
2344 if (pos >= following_text.Length)
2346 MPlist plist = new MPlist ();
2347 plist.Push (MSymbol.integer, pos + 1);
2348 if (MInputMethod.GetSurroundingText != null
2349 && MInputMethod.GetSurroundingText (this, plist)
2351 && following_text.Length < plist.Text.Length)
2352 following_text = plist.Text;
2354 c = (pos < following_text.Length ? following_text[pos] : -1);
2359 internal void delete (int pos)
2361 if (pos < cursor_pos)
2362 preedit_replace (pos, cursor_pos, null);
2364 preedit_replace (cursor_pos, pos, null);
2365 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2368 internal void show ()
2370 candidate_show = true;
2371 changed |= ChangedStatus.CandidateShow;
2374 internal void hide ()
2376 candidate_show = false;
2377 changed |= ChangedStatus.CandidateShow;
2380 internal void move (int pos)
2384 else if (pos > preedit.Length)
2385 pos = preedit.Length;
2386 if (pos != cursor_pos)
2389 changed |= ChangedStatus.Preedit;
2393 internal void mark (MSymbol sym)
2395 MPlist slot = markers.Find (sym);
2398 markers.Push (sym, cursor_pos);
2400 slot.val = cursor_pos;
2403 internal void pushback (int n)
2416 if (key_head > keys.keyseq.Count)
2417 key_head = keys.keyseq.Count;
2421 internal void pushback (MInputMethod.KeySeq keyseq)
2425 if (key_head < keys.keyseq.Count)
2426 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2427 for (int i = 0; i < keyseq.keyseq.Count; i++)
2428 keys.keyseq.Add (keyseq.keyseq[i]);
2431 internal void pop ()
2433 if (key_head < keys.keyseq.Count)
2434 keys.keyseq.RemoveRange (key_head, 1);
2437 internal void undo (int n)
2440 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2442 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2446 internal void commit ()
2448 produced.Cat (preedit);
2450 changed |= ChangedStatus.Preedit;
2453 internal void shift (MSymbol sym)
2455 MInputMethod.State state;
2457 if (sym == MSymbol.t)
2459 if (states.Count > 1)
2460 state = states.Pop ();
2462 state = states.Peek ();
2466 state = (MInputMethod.State) im.states.Get (sym);
2468 throw new Exception ("Unknown state: " + state.name);
2471 state = states.Pop ();
2472 if (state == (MInputMethod.State) im.states.val)
2479 state_key_head = key_head;
2480 state_pos = cursor_pos;
2481 state_preedit = preedit.Dup ();
2482 if (state != states.Peek ())
2484 states.Push (state);
2485 state_var_values = domain.SaveValues ();
2486 status = state.title;
2489 changed |= ChangedStatus.StateTitle;
2491 = (Xex) state.branches.Get (MSymbol.t);
2492 if (on_entry != null)
2493 on_entry.Eval (domain);
2498 internal void reset ()
2501 state_preedit.Del ();
2505 key_head = commit_key_head = 0;
2507 states.Push ((MInputMethod.State) im.states.Val);
2512 internal object GetCandidates (out int column)
2515 if (cursor_pos == 0)
2517 Candidates candidates
2518 = (Candidates) preedit.GetProp (cursor_pos - 1,
2519 MInputMethod.Mcandidates);
2520 if (candidates == null)
2522 column = candidates.Column;
2523 return candidates.Current;
2526 internal void HandleKey ()
2530 public bool Toggle ()