2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
12 using Xex = System.Xml.Expression.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (Context 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_flag = "surrounding-text-flag";
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 ChangedStatus
88 CandidateIndex = 0x10,
92 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
93 | ChangedStatus.CandidateIndex
94 | ChangedStatus.CandidateShow);
96 public enum KeyModifier
100 Shift_R = 0x00800000,
102 Control_L = 0x01000000,
103 Control_R = 0x02000000,
104 Control = 0x03000000,
119 private static Dictionary<string, uint> keysyms
120 = new Dictionary<string, uint> ();
121 private static Dictionary<string, KeyModifier> keymodifiers
122 = new Dictionary<string, KeyModifier> ();
123 private static uint keysym_base = 0x200000;
124 private static uint char_mask = ~((uint) KeyModifier.All);
128 keysyms["bs"] = keysyms["backspace"] = 0x08;
129 keysyms["tab"] = 0x09;
130 keysyms["lf"] = keysyms["linefeed"] = 0x10;
131 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
132 keysyms["esc"] = keysyms["escape"] = 0x1B;
133 keysyms["spc"] = keysyms["space"] = 0x20;
134 keysyms["del"] = keysyms["delete"] = 0x7F;
135 keymodifiers["shift-l"] = KeyModifier.Shift_L;
136 keymodifiers["shift-r"] = KeyModifier.Shift_R;
137 keymodifiers["shift"] = KeyModifier.Shift;
138 keymodifiers["control-l"] = KeyModifier.Control_L;
139 keymodifiers["control-r"] = KeyModifier.Control_R;
140 keymodifiers["control"] = KeyModifier.Control;
141 keymodifiers["alt-l"] = KeyModifier.Alt_L;
142 keymodifiers["alt-r"] = KeyModifier.Alt_R;
143 keymodifiers["alt"] = KeyModifier.Alt;
144 keymodifiers["altgr"] = KeyModifier.AltGr;
145 keymodifiers["super"] = KeyModifier.Super;
146 keymodifiers["hyper"] = KeyModifier.Hyper;
149 private static uint decode_keysym (MSymbol keysym)
152 string name = keysym.Name;
154 if (name.Length == 1)
156 name = name.ToLower ();
157 if (! keysyms.TryGetValue (name, out key))
158 keysyms[name] = key = keysym_base++;
162 private static uint combine_modifiers (uint c, KeyModifier modifiers)
164 if (c < 0x7F && c != 0x20)
166 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
167 && Char.IsLower ((char) c))
169 modifiers &= ~KeyModifier.Shift;
170 c = Char.ToUpper ((char) c);
172 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
174 modifiers &= ~KeyModifier.Control;
178 return c | (uint) modifiers;
181 public Key (uint c) { key = c; }
182 public Key (int c) { key = (uint) c; }
184 public Key (uint c, KeyModifier modifiers)
186 key = combine_modifiers (c, modifiers);
189 public Key (MSymbol keysym, KeyModifier modifiers)
191 key = combine_modifiers (decode_keysym (keysym), modifiers);
194 public Key (MSymbol keysym)
196 string str = keysym.Name;
197 int len = str.Length;
199 KeyModifier modifiers = KeyModifier.None;
201 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
204 modifiers |= KeyModifier.Shift;
205 else if (str[i] == 'C')
206 modifiers |= KeyModifier.Control;
207 else if (str[i] == 'A')
208 modifiers |= KeyModifier.Alt;
209 else if (str[i] == 'G')
210 modifiers |= KeyModifier.AltGr;
211 else if (str[i] == 's')
212 modifiers |= KeyModifier.Super;
213 else if (str[i] == 'H')
214 modifiers |= KeyModifier.Hyper;
217 key = combine_modifiers (str[i], modifiers);
219 key = combine_modifiers (decode_keysym (keysym), modifiers);
222 public Key (MPlist plist)
224 KeyModifier modifiers = KeyModifier.None;
227 for (p = plist; ! p.IsEmpty; p = p.next)
231 if (! p.next.IsEmpty)
232 throw new Exception ("Invalid Key: " + plist);
235 else if (! p.IsSymbol)
236 throw new Exception ("Invalid Key: " + plist);
239 string name = p.Symbol.Name.ToLower ();
242 if (! keymodifiers.TryGetValue (name, out m))
247 if (p.IsEmpty || ! p.next.IsEmpty)
248 throw new Exception ("Invalid Key: " + plist);
250 key = combine_modifiers ((uint) p.Integer, modifiers);
252 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
255 public bool HasModifier
257 get { return ((key & (uint) KeyModifier.All) != 0); }
260 public bool Match (Key k)
264 if ((k.key & char_mask) != (key & char_mask))
266 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
267 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
268 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
269 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
270 && (m2 & KeyModifier.Shift) != KeyModifier.None))
271 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
272 || ((m1 & KeyModifier.Control) == KeyModifier.Control
273 && (m2 & KeyModifier.Control) != KeyModifier.None))
274 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
275 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
276 && (m2 & KeyModifier.Alt) != KeyModifier.None))
277 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
280 public override string ToString ()
282 string str = Char.ToString ((char) key);
283 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
285 if (m != KeyModifier.None)
287 if ((m & KeyModifier.Shift) != KeyModifier.None)
289 if ((m & KeyModifier.Control) != KeyModifier.None)
291 if ((m & KeyModifier.Alt) != KeyModifier.None)
293 if ((m & KeyModifier.AltGr) != KeyModifier.None)
295 if ((m & KeyModifier.Super) != KeyModifier.None)
297 if ((m & KeyModifier.Hyper) != KeyModifier.None)
304 public class KeySeq : Xex.TermValue
306 public List<Key> keyseq = new List<Key> ();
308 public override Xex.TermValue Clone ()
310 KeySeq ks = new KeySeq ();
311 ks.keyseq.InsertRange (0, keyseq);
317 public KeySeq (MPlist plist)
319 foreach (MPlist p in plist)
322 keyseq.Add (new Key (p.Symbol));
323 else if (p.IsInteger)
324 keyseq.Add (new Key ((char) p.Integer));
326 keyseq.Add (new Key (p.Plist));
328 throw new Exception ("Invalid Key Sequence: " + plist);
332 public KeySeq (MText mt) : base ()
334 for (int i = 0; i < mt.Length; i++)
335 keyseq.Add (new Key ((uint) mt[i]));
338 public KeySeq (List<Xex.Term> list)
340 int len = list.Count;
342 for (int i = 0; i < len; i++)
345 keyseq.Add (new Key (list[i].Intval));
346 else if (list[i].IsStr)
347 keyseq.Add (new Key (list[i].Strval));
348 else if (list[i].IsSymbol)
349 keyseq.Add (new Key ((string) list[i].Symval));
351 throw new Exception ("Invalid key: " + list[i]);
355 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
357 Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain);
358 return (term.IsStr ? new KeySeq ((MText) term.Strval)
359 : new KeySeq (term.Listval));
362 public override string ToString ()
366 foreach (Key key in keyseq)
370 foreach (Key k in keyseq)
371 str += " " + k.ToString ();
375 foreach (Key key in keyseq)
376 str += key.ToString ();
384 public MText description;
385 public List<KeySeq> keys;
387 public Command (MPlist p)
391 description = parse_description (p);
392 if (description == null)
393 description = "No description";
394 keys = new List<KeySeq> ();
395 Console.WriteLine ("cmd:" + p);
396 for (p = p.next; ! p.IsEmpty; p = p.next)
399 keys.Add (new KeySeq (p.Text));
401 keys.Add (new KeySeq (p.Plist));
405 public Command (XmlNode node)
407 name = node.Attributes[0].Value;
408 keys = new List<KeySeq> ();
409 for (node = node.FirstChild; node != null; node = node.NextSibling)
411 if (node.Name == "description")
412 description = parse_description (node);
413 else if (node.Name == "keyseq")
414 keys.Add ((KeySeq) KeySeq.parser (null, node));
418 public override string ToString ()
420 string str = "(" + name + " \"" + (string) description;
421 foreach (KeySeq keyseq in keys)
427 internal class Plugin
430 private Assembly assembly;
431 private Type plugin_type;
433 public Plugin (string name)
438 public MethodInfo GetMethod (Xex.Symbol name)
440 if (assembly == null)
442 assembly = Assembly.LoadFrom (name + ".dll");
443 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
446 MethodInfo info = plugin_type.GetMethod ((string) name);
448 throw new Exception ("Invalid plugin method: " + name);
452 public override string ToString ()
454 return String.Format ("(module {0}", name);
458 internal class PluginMethod : Xex.Function
460 private Plugin plugin;
461 private MethodInfo method_info;
462 object[] parameters = new object[2];
464 public PluginMethod (Plugin plugin, string name)
465 : base ((Xex.Symbol) name, 0, -1)
467 this.plugin = plugin;
470 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
473 args = (Xex.Term[]) args.Clone ();
474 for (int i = 0; i < args.Length; i++)
476 args[i] = args[i].Eval (domain);
480 if (method_info == null)
481 method_info = plugin.GetMethod (name);
482 parameters[0] = domain.context;
483 parameters[1] = args;
484 return (Xex.Term) method_info.Invoke (null, parameters);
488 internal abstract class Marker : Xex.TermValue
490 private MSymbol name;
492 public Marker (MSymbol name)
497 public abstract int Position (Context ic);
498 public abstract void Mark (Context ic);
500 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
502 MSymbol name = node.InnerText;
504 return Get ((Context) domain.context, name);
507 public class Named : Marker
511 public Named (MSymbol name) : this (name, 0) { }
513 private Named (MSymbol name, int p) : base (name) { pos = p; }
515 public override int Position (Context ic) { return pos; }
517 public override void Mark (Context ic) { pos = ic.cursor_pos; }
519 public override Xex.TermValue Clone ()
521 return new Named (name, pos);
525 public class Predefined : Marker
527 public Predefined (MSymbol name) : base (name) { }
529 public override int Position (Context ic)
531 switch (name.Name[1]) {
533 case '>': return ic.preedit.Length;
534 case '-': return ic.cursor_pos - 1;
535 case '+': return ic.cursor_pos + 1;
537 if (ic.cursor_pos > 0)
539 int pos = ic.cursor_pos;
541 ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to);
546 if (ic.cursor_pos < ic.preedit.Length - 1)
548 int pos = ic.cursor_pos;
550 ic.preedit.FindProp (Mcandidates, pos, out from, out pos);
553 return ic.preedit.Length;
555 return name.Name[1] - '0';
559 public override void Mark (Context ic)
561 throw new Exception ("Can't set predefined marker: " + name);
564 public override Xex.TermValue Clone ()
566 return new Predefined (name);
570 static internal Dictionary<MSymbol,Predefined> predefined_markers;
574 predefined_markers = new Dictionary<MSymbol,Predefined> ();
575 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
576 "@0", "@1", "@2", "@3", "@4",
577 "@5", "@6", "@7", "@8", "@9" };
578 foreach (MSymbol s in symlist)
579 predefined_markers[s] = new Predefined (s);
582 public static Marker Get (Context ic, MSymbol name)
587 if (predefined_markers.TryGetValue (name, out pred))
589 if (name.Name[0] == '@')
590 throw new Exception ("Invalid marker name: " + name);
591 m = (Marker) ic.markers.Get (name);
594 m = new Named (name);
595 ic.markers.Put (name, m);
601 internal class Candidates
608 public Block (int index, Xex.Term term)
612 Data = (MText) term.Strval;
615 MPlist plist = new MPlist ();
617 foreach (Xex.Term t in term.Listval)
618 p = p.Add (MSymbol.mtext, (MText) t.Strval);
623 public Block (int index, MPlist plist)
628 else if (plist.IsPlist)
631 throw new Exception ("Invalid candidate: " + plist);
636 get { return (Data is MText
637 ? ((MText) Data).Length
638 : ((MPlist) Data).Count); }
641 public object this[int i]
644 if (Data is MText) return ((MText) Data)[i];
645 return ((MPlist) Data)[i];
650 private Block[] blocks;
652 private int index = 0;
653 public object[] group;
655 private bool IsFixed { get { return group != null; } }
658 Block last = blocks[blocks.Length - 1];
659 return last.Index + last.Count; }
663 get { return (IsFixed ? index % group.Length
664 : index - blocks[row].Index); }
667 public object Group {
668 get { return (IsFixed ? group : blocks[row].Data); }
671 public int GroupLength
676 int nitems = group.Length;
677 int start = index - (index % nitems);
679 return (start + nitems <= total ? nitems : total - start);
681 return blocks[row].Count;
685 public object Current {
687 return (IsFixed ? group[index % group.Length]
688 : blocks[row][index - blocks[row].Index]);
692 public Candidates (MPlist list, int column)
694 int nblocks = list.Count;
696 blocks = new Block[nblocks];
697 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
698 start += (blocks[i] = new Block (index, list)).Count;
700 group = new object[column];
703 public Candidates (List<Xex.Term> list, int column)
705 int nblocks = list.Count;
707 blocks = new Block[nblocks];
708 for (int i = 0, start = 0; i < nblocks; i++)
709 start += (blocks[i] = new Block (index, list[i])).Count;
711 group = new object[column];
714 public static void Detach (Context ic)
716 ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
717 ic.candidates = null;
718 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
722 // Fill the array "group" by candidates stating from INDEX.
723 // INDEX must be a multiple of "column". Set NTIMES to the
724 // number of valid candidates in "group". Update "block" if
725 // necessary. Return "group".
727 private int fill_group (int start)
729 int nitems = group.Length;
734 while (start < b.Index)
737 while (start >= b.Index + b.Count)
743 for (int i = 0; i < nitems; i++, start++)
748 if (r == blocks.Length)
759 // Update "row" to what contains the first candidate of
760 // the previous candidate-group, update "current_index", and
761 // update "group" if necessary. Return the previous
762 // candidate-group. Set NITEMS to the number of valid
763 // candidates contained in that group.
765 public int PrevGroup ()
772 nitems = group.Length;
773 if ((index -= col + nitems) < 0)
774 index = (Total / nitems) * nitems;
775 nitems = fill_group (index);
779 row = row > 0 ? row-- : blocks.Length - 1;
780 nitems = blocks[row].Count;
781 index = blocks[row].Index;
783 index += col < nitems ? col : nitems - 1;
787 public int NextGroup ()
794 nitems = group.Length;
795 if ((index += nitems - col) >= Total)
797 nitems = fill_group (index);
801 row = row < blocks.Length - 1 ? row + 1 : 0;
802 nitems = blocks[row].Count;
803 index = blocks[row].Count;
805 index += col < nitems ? col : nitems - 1;
815 int nitems = PrevGroup ();
816 index += col < nitems - 1 ? col : nitems - 1;
825 int nitems = GroupLength;
827 if (col == nitems - 1)
829 nitems = NextGroup ();
843 index += GroupLength - (Column + 1);
846 public void Select (int col)
848 int maxcol = GroupLength - 1;
851 index = index - Column + col;
855 internal abstract class Selector : Xex.TermValue
857 private Selector () { }
859 public abstract void Select (Candidates candidates);
861 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
863 MSymbol name = node.InnerText;
866 if (predefined_selectors.TryGetValue (name, out pred))
868 if (name.Name[0] == '@')
869 throw new Exception ("Invalid selector name: " + name);
871 if (! Int32.TryParse (node.InnerText, out index))
872 throw new Exception ("Invalid selector name: " + name);
873 return new Numbered (index);
876 public override Xex.TermValue Clone () { return this; }
878 public class Numbered : Selector
882 public Numbered (int index) { this.index = index; }
884 public override void Select (Candidates can) { can.Select (index); }
887 public class Predefined : Selector
891 internal Predefined (MSymbol sym) { this.tag = sym.Name[1]; }
893 public override void Select (Candidates candidates)
897 case '<': candidates.First (); break;
898 case '>': candidates.Last (); break;
899 case '-': candidates.Prev (); break;
900 case '+': candidates.Next (); break;
901 case '[': candidates.PrevGroup (); break;
902 case ']': candidates.NextGroup (); break;
908 static new Dictionary<MSymbol, Predefined> predefined_selectors;
912 predefined_selectors = new Dictionary<MSymbol, Predefined> ();
913 MSymbol[] symlist = new MSymbol[] { "@<", "@=", "@>", "@-", "@+",
915 foreach (MSymbol s in symlist)
916 predefined_selectors[s] = new Predefined (s);
923 public Dictionary<Key, Map> submaps;
924 public Xex.Term actions;
926 public void Add (KeySeq keys, int index, Xex.Term actions)
931 submaps = new Dictionary<Key, Map> ();
933 submaps.TryGetValue (keys.keyseq[index], out sub);
936 Key key = keys.keyseq[index];
937 submaps[key] = sub = new Map ();
939 if (index + 1 < keys.keyseq.Count)
940 sub.Add (keys, index + 1, actions);
942 this.actions = actions;
945 public Xex.Term Lookup (KeySeq keys, ref index)
947 if (index < keys.keyseq.Count)
950 if (submaps.TryGetValue (keys.keyseq[index], out sub))
953 return sub.Lookup (keys, ref index);
960 private void describe (MText mt, KeySeq keyseq)
962 if (keyseq.keyseq.Count > 0)
964 mt.Cat (" (").Cat (keyseq.ToString ());
965 mt.Cat (' ').Cat (actions.ToString ());
969 foreach (KeyValuePair<Key, Map> kv in submaps)
971 keyseq.keyseq.Add (kv.Key);
972 kv.Value.describe (mt, keyseq);
973 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
977 public override string ToString ()
979 MText mt = "(" + name.Name;
980 KeySeq keyseq = new KeySeq ();
982 describe (mt, keyseq);
992 public MPlist branches = new MPlist ();
994 public State (MSymbol name)
999 public override string ToString ()
1001 MText mt = "(" + name.Name;
1004 mt.Cat (" \"" + title + "\"");
1005 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
1006 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
1007 return (string) mt + ")";
1012 internal Xex.Domain domain = new Xex.Domain (im_domain, null);
1014 private LoadStatus load_status = LoadStatus.None;
1015 private MDatabase.Tag tag;
1016 private MDatabase mdb;
1018 private MText description;
1019 internal MText title;
1020 internal Command[] commands;
1021 internal Xex.Symbol[] var_names;
1022 internal Dictionary<MSymbol, Plugin> plugins;
1023 internal Dictionary<MSymbol, Map> maps;
1024 internal MPlist states;
1026 static MInputMethod ()
1028 im_domain.DefTerm ("keyseq", KeySeq.parser);
1029 im_domain.DefTerm ("marker", Marker.parser);
1030 im_domain.DefTerm ("selector", Selector.parser);
1032 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1033 im_domain.DefSubr (Finsert_candidates, "candidates", false, 1, -1);
1034 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1035 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1036 im_domain.DefSubr (Fshow, "show", false, 0, 0);
1037 im_domain.DefSubr (Fhide, "hide", false, 0, 0);
1038 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1039 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1040 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1041 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1042 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1043 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1044 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1045 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1046 im_domain.DefSubr (Fshift_back, "shiftback", false, 0, 0);
1047 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1048 im_domain.DefSubr (Fkey_count, "key-count", false, 1, 1);
1049 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag",
1052 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
1053 List<MDatabase> list = MDatabase.List (tag);
1054 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
1055 foreach (MDatabase mdb in list)
1056 im_table[mdb.tag] = new MInputMethod (mdb.tag);
1060 private MInputMethod (MDatabase.Tag tag)
1065 // Instance Properties
1066 public MSymbol Language { get { return tag[1]; } }
1067 public MSymbol Name { get { return tag[2]; } }
1068 public MSymbol SubName { get { return tag[3]; } }
1070 public bool Info (out MText description,
1072 out Xex.Variable[] variables,
1073 out Command[] commands)
1075 if ((load_status & LoadStatus.Header) != LoadStatus.Header
1076 && ! load_header ())
1084 description = this.description;
1086 if (var_names == null)
1090 variables = new Xex.Variable[var_names.Length];
1092 foreach (Xex.Symbol name in var_names)
1093 variables[i++] = domain.GetVar (name, false);
1095 commands = this.commands;
1099 public static MInputMethod Find (MSymbol language, MSymbol name)
1101 return Find (language, name, MSymbol.nil);
1104 public static MInputMethod Find (MSymbol language, MSymbol name,
1107 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
1111 return (im_table.TryGetValue (tag, out im) ? im : null);
1116 return ((load_status == LoadStatus.Full) || load_body ());
1119 public static MInputMethod[] List ()
1121 MInputMethod[] array = new MInputMethod[im_table.Count];
1124 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
1125 array[i++] = kv.Value;
1129 private bool load_header ()
1131 mdb = MDatabase.Find (tag);
1134 mdb.name_table = Xex.Symbol.Table;
1136 MSymbol format = mdb.Format;
1138 if (format == MSymbol.plist)
1139 load ((MPlist) mdb.Load (Mmap), false);
1142 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
1143 load (doc.DocumentElement, false);
1145 } catch (Exception e) {
1146 Console.WriteLine ("{0}\n", e);
1147 load_status = LoadStatus.Error;
1150 load_status |= LoadStatus.Header;
1154 private bool load_body ()
1156 mdb = MDatabase.Find (tag);
1159 mdb.name_table = Xex.Symbol.Table;
1161 object obj = mdb.Load ();
1163 load ((MPlist) obj, true);
1165 load ((XmlDocument) obj, true);
1166 } catch (Exception e) {
1167 Console.WriteLine (e);
1168 load_status = LoadStatus.Error;
1171 load_status = LoadStatus.Full;
1175 private void load (MPlist plist, bool full)
1177 maps = new Dictionary<MSymbol, Map> ();
1178 states = new MPlist ();
1180 for (; ! plist.IsEmpty; plist = plist.next)
1183 MPlist pl = plist.Plist;
1186 MSymbol sym = pl.Symbol;
1189 if (sym == Mdescription)
1190 description = parse_description (pl);
1191 else if (sym == Mtitle)
1196 else if (sym == Mvariable)
1197 parse_variables (pl);
1198 else if (sym == Mcommand)
1199 parse_commands (pl);
1204 else if (sym == Minclude)
1206 else if (sym == Mmacro)
1208 else if (sym == Mmap)
1210 else if (sym == Mstate)
1215 if (description == null)
1216 description = (MText) "No description";
1218 title = new MText (tag[2].Name);
1219 if (commands == null)
1220 commands = new Command[0];
1225 State state = new State ((MSymbol) "init");
1226 plist = new MPlist ();
1227 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1228 state.branches.Add (kv.Key, null);
1229 states.Add (state.name, state);
1233 private void load (XmlNode node, bool full)
1235 bool skip_header = load_status == LoadStatus.Header;
1237 maps = new Dictionary<MSymbol, Map> ();
1238 states = new MPlist ();
1240 if (node.NodeType == XmlNodeType.Document)
1241 node = node.FirstChild;
1242 while (node.NodeType != XmlNodeType.Element)
1243 node = node.NextSibling;
1244 for (node = node.FirstChild; node != null; node = node.NextSibling)
1246 if (node.NodeType != XmlNodeType.Element)
1250 if (node.Name == "description")
1251 description = parse_description (node);
1252 else if (node.Name == "title")
1253 title = parse_title (node);
1254 else if (node.Name == "variable-list")
1255 parse_variables (node);
1256 else if (node.Name == "command-list")
1257 parse_commands (node);
1261 if (node.Name == "module-list")
1262 parse_plugins (node);
1263 else if (node.Name == "macro-list")
1264 parse_macros (node);
1265 else if (node.Name == "map-list")
1267 else if (node.Name == "state-list")
1268 parse_states (node);
1271 if (description == null)
1272 description = (MText) "No description";
1274 title = new MText (tag[2].Name);
1275 if (commands == null)
1276 commands = new Command[0];
1281 State state = new State ((MSymbol) "init");
1282 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1283 state.branches.Add (kv.Key, null);
1284 states.Add (state.name, state);
1288 private static void transform (MPlist plist)
1290 for (; ! plist.IsEmpty; plist = plist.next)
1294 MPlist p = new MPlist ();
1295 p.Add (MSymbol.symbol, Minsert);
1296 p.Add (MSymbol.mtext, plist.Text);
1297 plist.Set (MSymbol.plist, p);
1299 else if (plist.IsInteger)
1301 MPlist p = new MPlist ();
1302 p.Add (MSymbol.symbol, Minsert);
1303 p.Add (MSymbol.integer, plist.Integer);
1304 plist.Set (MSymbol.plist, p);
1306 else if (plist.IsPlist)
1308 MPlist pl = plist.Plist;
1312 if (pl.Symbol == Madd)
1313 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1314 else if (pl.Symbol == Msub)
1315 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1316 else if (pl.Symbol == Mmul)
1317 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1318 else if (pl.Symbol == Mdiv)
1319 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1320 else if (pl.Symbol == Minsert)
1322 // (insert (CANDIDATES ...))
1323 // => (candidates CANDIDATES ...)
1324 if (pl.next.IsPlist)
1326 pl.Set (MSymbol.symbol, Mcandidates);
1328 MPlist p = pl.Plist;
1329 pl.Set (p.key, p.val);
1330 for (p = p.next; ! p.IsEmpty; p = p.next);
1331 pl.Add (p.key, p.val);
1334 else if (pl.Symbol == Mif)
1338 transform (pl.next);
1340 else if (pl.Symbol == Mcond)
1342 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1345 MPlist p = pl.Plist;
1353 else if (pl.Symbol == Mdelete
1354 || pl.Symbol == Mmove
1355 || pl.Symbol == Mmark)
1360 MSymbol sym = pl.Symbol;
1361 MPlist p = new MPlist ();
1362 p.Add (MSymbol.symbol, Mmarker);
1363 p.Add (MSymbol.symbol, sym);
1364 pl.Set (MSymbol.plist, p);
1367 else if (pl.Symbol == Mpushback)
1371 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1374 else if (pl.IsMText)
1376 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1377 pl.Push (MSymbol.symbol, Mcandidates);
1380 else if (plist.IsSymbol)
1382 MSymbol sym = plist.Symbol;
1384 if (sym.Name.Length >= 3
1385 && sym.Name[0] == '@'
1386 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1388 int pos = int.Parse (sym.Name.Substring (1));
1389 MPlist p = new MPlist ();
1393 p.Add (MSymbol.symbol, Msurrounding_flag);
1397 if (sym.Name[1] == '+')
1399 p.Add (MSymbol.symbol, Mchar_at);
1400 p.Add (MSymbol.integer, pos);
1402 plist.Set (MSymbol.plist, p);
1408 private static MText parse_description (MPlist plist)
1414 plist = plist.Plist;
1415 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1416 && plist.next.IsMText)
1417 return plist.next.Text;
1422 private static MText parse_description (XmlNode node)
1424 if (node.HasChildNodes)
1425 node = node.FirstChild;
1426 return node.InnerText;
1429 private static MText parse_title (XmlNode node)
1431 return node.InnerText;
1434 private void new_variable (Xex.Symbol name, string desc, int val,
1435 MPlist pl, Xex.Variable vari)
1443 range = new int[pl.Count * 2];
1444 for (int i = 0; i < range.Length; i++)
1448 MPlist p = pl.Plist;
1450 if (! p.IsInteger || ! p.next.IsInteger)
1451 throw new Exception ("Invalid range: " + p);
1452 range[i * 2] = p.Integer;
1453 range[i * 2 + 1] = p.next.Integer;
1455 else if (pl.IsInteger)
1456 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1458 throw new Exception ("Invalid range: " + pl);
1462 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1465 Xex.Term term = new Xex.Term (val);
1467 vari.DefaultValue = term;
1472 private void new_variable (Xex.Symbol name, string desc, MText val,
1473 MPlist pl, Xex.Variable vari)
1481 range = new string[pl.Count * 2];
1482 for (int i = 0; i < range.Length; i++)
1485 range[i] = (string) pl.Text;
1487 throw new Exception ("Invalid range: " + pl);
1491 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1494 Xex.Term term = new Xex.Term ((string) val);
1496 vari.DefaultValue = term;
1501 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1502 MPlist pl, Xex.Variable vari)
1505 Xex.Symbol sym = val.Name;
1511 range = new Xex.Symbol[pl.Count * 2];
1512 for (int i = 0; i < range.Length; i++)
1515 range[i] = pl.Symbol.Name;
1517 throw new Exception ("Invalid range: " + pl);
1521 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1524 Xex.Term term = new Xex.Term (sym);
1526 vari.DefaultValue = term;
1531 private Xex.Variable get_global_var (Xex.Symbol name)
1533 if (im_global == null || this != im_global)
1535 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1537 im_global = im_table[tag];
1538 if (! im_global.Open ())
1539 throw new Exception ("Failed to load global");
1541 return im_global.domain.GetVar (name, false);
1544 private void parse_variables (MPlist plist)
1546 var_names = new Xex.Symbol[plist.Count];
1548 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1550 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1551 throw new Exception ("Invalid variable: " + plist);
1553 MPlist p = plist.Plist;
1554 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1555 var_names[i] = name;
1557 string desc = (string) parse_description (p);
1558 Xex.Variable vari = get_global_var (name);
1560 domain.Defvar (vari);
1566 new_variable (name, desc, p.Integer, p.next, vari);
1568 new_variable (name, desc, p.Text, p.next, vari);
1569 else if (p.IsSymbol)
1570 new_variable (name, desc, p.Symbol, p.next, vari);
1572 throw new Exception ("Invalid variable type: " + p.val);
1577 private void parse_variables (XmlNode node)
1579 XmlNodeList node_list = node.ChildNodes;
1581 var_names = new Xex.Symbol[node_list.Count];
1582 for (int i = 0; i < node_list.Count; i++)
1584 Xex.Symbol name = node_list[i].Attributes[0].Value;
1585 Xex.Variable vari = get_global_var (name);
1587 domain.Defvar (vari);
1588 domain.Defvar (node_list[i]);
1589 var_names[i] = name;
1593 private void parse_commands (MPlist plist)
1595 commands = new Command[plist.Count];
1597 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1598 if (plist.IsPlist && plist.Plist.IsSymbol)
1599 commands[i++] = new Command (plist.Plist);
1602 private void parse_commands (XmlNode node)
1604 XmlNodeList node_list = node.ChildNodes;
1606 commands = new Command[node_list.Count];
1607 for (int i = 0; i < node_list.Count; i++)
1609 if (node_list[i].NodeType == XmlNodeType.Element)
1610 commands[i] = new Command (node_list[i]);
1614 private void parse_plugins (MPlist plist)
1616 plugins = new Dictionary<MSymbol, Plugin> ();
1618 for (; ! plist.IsEmpty; plist = plist.Next)
1620 MPlist p = plist.Plist;
1621 MSymbol sym = p.Symbol;
1622 Plugin plugin = new Plugin (sym.Name);
1624 for (p = p.next; ! p.IsEmpty; p = p.next)
1626 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1627 domain.Defun (func);
1632 private void parse_plugins (XmlNode node)
1634 plugins = new Dictionary<MSymbol, Plugin> ();
1636 foreach (XmlNode n in node.ChildNodes)
1638 Plugin plugin = new Plugin (n.Attributes[0].Value);
1639 foreach (XmlNode nn in n.ChildNodes)
1641 Xex.Function func = new PluginMethod (plugin,
1642 nn.Attributes[0].Value);
1643 domain.Defun (func);
1648 private void parse_macros (XmlNode node)
1650 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1651 if (nn.NodeType == XmlNodeType.Element)
1652 domain.Defun (nn, true);
1653 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1654 if (nn.NodeType == XmlNodeType.Element)
1655 domain.Defun (nn, false);
1658 private void parse_maps (XmlNode node)
1662 private void parse_states (XmlNode node)
1666 private void parse_include (MPlist plist)
1668 if (! plist.IsPlist)
1670 MPlist p = plist.Plist;
1671 MSymbol language, name, subname;
1672 language = p.Symbol;
1675 name = subname = MSymbol.nil;
1681 subname = MSymbol.nil;
1686 MInputMethod im = MInputMethod.Find (language, name, subname);
1692 if (! plist.IsSymbol)
1694 MSymbol target_type = plist.Symbol;
1696 MSymbol target_name = MSymbol.nil;
1698 target_name = plist.Symbol;
1699 if (target_type == Mmacro)
1701 if (target_name == MSymbol.nil)
1702 im.domain.CopyFunc (domain);
1704 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1706 else if (target_type == Mmap)
1708 if (target_name == MSymbol.nil)
1710 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1711 maps[kv.Key] = kv.Value;
1716 if (im.maps.TryGetValue (target_name, out map))
1717 maps[target_name] = map;
1720 else if (target_type == Mstate)
1722 if (target_name == MSymbol.nil)
1724 for (p = im.states; ! p.IsEmpty; p = p.next)
1725 states.Add (p.key, p.val);
1729 object state = im.states.Get (target_name);
1731 states.Add (target_name, state);
1736 private Xex.Term parse_cond (MPlist plist)
1738 Xex.Term[] args = new Xex.Term[plist.Count];
1740 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1742 if (! plist.IsPlist)
1743 throw new Exception ("Invalid cond args: " + plist);
1744 MPlist p = plist.Plist;
1745 List<Xex.Term> arg = new List<Xex.Term> (parse_actions (p));
1746 args[i] = new Xex.Term (arg);
1748 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1751 private Xex.Term parse_integer (MPlist plist)
1755 Xex.Variable vari = domain.GetVar (Xex.Name (plist.Symbol.Name),
1757 Xex.Term[] args = Xex.Term[1];
1758 args[0] = new Xex.Term (vari);
1759 terms[i] = new Xex.Term (domain, Ninsert, args);
1761 else if (plist.IsMText)
1763 Xex.Term[] args = Xex.Term[1];
1764 args[0] = new Xex.Term ((string) plist.Text);
1765 terms[i] = new Xex.Term (domain, Ninsert, args);
1767 else if (plist.IsInteger)
1769 Xex.Term[] args = Xex.Term[1];
1770 args[0] = new Xex.Term (plist.Integer);
1771 terms[i] = new Xex.Term (domain, Ninsert, args);
1775 private Xex.Term parse_action (MPlist plist)
1779 MPlist p = plist.Plist;
1781 if (p.IsMText || p.IsPlist)
1782 return parse_integer (p);
1784 throw new Exception ("Invalid action: " + p);
1785 MSymbol name = p.Symbol;
1788 return parse_cond (p);
1789 else if (name == Mset || name == Madd || name == Msub
1790 || name == Mmul || name == Mdiv)
1793 throw new Exception ("Invalid action: " + p);
1794 Xex.Symbol varname = p.Symbol.Name;
1795 return new Xex.Term (domain, (Xex.Symbol) name.Name,
1796 varname, parse_actions (p.next));
1799 return new Xex.Term (domain, (Xex.Symbol) name.Name,
1802 else if (plist.MText || plist.Integer || plist.IsSymbol)
1803 retunr parse_insert (plist);
1805 throw new Exception ("Invalid action: " + plist);
1808 private Xex.Term[] parse_actions (MPlist plist)
1810 Xex.Term[] terms = new Xex.Term[plist.Count];
1812 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1813 terms[i] = parse_action (plist);
1818 private void parse_macros (MPlist plist)
1820 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1823 MPlist p = pl.Plist;
1827 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
1829 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1832 MPlist p = pl.Plist;
1837 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
1838 parse_actions (p.next), false);
1842 private void parse_maps (MPlist plist)
1844 for (; ! plist.IsEmpty; plist = plist.next)
1847 MPlist pl = plist.Plist;
1851 Map map = new Map ();
1852 map.name = pl.Symbol;
1853 maps[map.name] = map;
1854 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1858 MPlist p = pl.Plist;
1861 keys = new KeySeq (p.Text);
1863 keys = new KeySeq (p.Plist);
1870 new Xex.Term (domain, Nprogn, parse_actions (p)));
1875 private void parse_states (MPlist plist)
1877 for (; ! plist.IsEmpty; plist = plist.next)
1880 MPlist pl = plist.Plist;
1891 State state = new State (pl.Symbol);
1892 state.title = title;
1894 states = new MPlist ();
1895 states.Add (state.name, state);
1896 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1900 MPlist p = pl.Plist;
1903 MSymbol map_name = p.Symbol;
1905 Xex.Term term = new Xex.Term (domain, Nprogn, parse_actions (p));
1906 state.branches.Add (map_name, term);
1911 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1914 ((Context) domain.context).insert (args[0]);
1918 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1922 ((Context) domain.context).insert_candidates (args[0]);
1926 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1929 Context ic = (Context) domain.context;
1930 Marker m = (Marker) args[0].Objval;
1932 return new Xex.Term (ic.char_at (m.Position (ic)));
1935 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1938 ((Context) domain.context).delete ((int) args[0].Intval);
1942 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1945 Candidates can = ((Context) domain.context).candidates;
1948 ((Selector) args[0].Objval).Select (can);
1952 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1955 ((Context) domain.context).show ();
1959 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1962 ((Context) domain.context).hide ();
1966 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1970 ((Context) domain.context).move (args[0].Intval);
1973 Marker m = (Marker) args[0].Objval;
1974 Context ic = (Context) domain.context;
1975 ((Context) domain.context).move (m.Position (ic));
1980 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1983 Marker m = (Marker) args[0].Objval;
1984 m.Mark ((Context) domain.context);
1988 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1991 Context ic = (Context) domain.context;
1994 ic.pushback (args[0].Intval);
1995 else if (args[0].IsStr)
1996 ic.pushback (new KeySeq (args[0].Strval));
1998 ic.pushback ((KeySeq) args[0].Objval);
2002 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
2005 ((Context) domain.context).pop ();
2009 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
2012 int n = args.Length == 0 ? -2 : args[0].Intval;
2013 ((Context) domain.context).undo (n);
2017 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
2020 ((Context) domain.context).commit ();
2024 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
2027 ((Context) domain.context).commit ();
2028 args = new Xex.Term[2];
2029 args[0] = args[1] = Tcatch_tag;
2030 return Xex.Fthrow (domain, vari, args);
2033 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
2036 ((Context) domain.context).shift (args[0].Symval);
2040 private static Xex.Term Fshift_back (Xex.Domain domain, Xex.Variable vari,
2043 ((Context) domain.context).shift_back ();
2047 private static Xex.Term Fkey_count (Xex.Domain domain, Xex.Variable vari,
2050 return new Xex.Term (((Context) domain.context).key_head);
2053 private static Xex.Term Fsurrounding_flag (Xex.Domain domain,
2057 return new Xex.Term (((Context) domain.context).surrounding_flag);
2060 public override string ToString ()
2062 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
2063 if (commands != null)
2065 str += " (commands";
2066 foreach (Command cmd in commands)
2070 if (var_names != null)
2072 str += " (variables";
2073 foreach (Xex.Symbol var in var_names)
2077 if (plugins != null)
2080 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
2081 str += " " + kv.Value;
2085 foreach (KeyValuePair<MSymbol, Map> kv in maps)
2086 str += " " + kv.Value;
2088 foreach (MPlist p in states)
2093 public class Context
2095 internal static Xex.Symbol Ncandidates_group_size
2096 = "candidates-group-size";
2097 public MInputMethod im;
2098 private MText produced;
2099 private bool active;
2100 private MText status;
2101 internal MText preedit;
2102 internal int cursor_pos;
2103 internal Candidates candidates;
2104 private int candidate_from, candidate_to;
2105 private bool candidate_show;
2107 private List<State> state_list;
2109 // Sequence of input keys.
2110 internal KeySeq keys;
2112 // Index into KEYS specifying the next key to handle.
2113 internal int key_head;
2116 private int state_key_head;
2117 private object state_var_values;
2118 private int commit_key_head;
2119 private MText state_preedit;
2120 private int state_pos;
2121 internal MPlist markers = new MPlist ();
2122 internal MText preceding_text = new MText ();
2123 internal MText following_text = new MText ();
2125 // Set to false before calling the method 'handle_key', and set
2126 // to true when some key is unhandled.
2127 private bool key_unhandled;
2129 // The unhandled key. It has the meaning only when
2130 // 'key_unhandled' is true.
2131 private Key unhandled_key;
2133 internal Xex.Domain domain;
2135 internal ChangedStatus changed;
2137 public ChangedStatus Changed { get { return changed; } }
2139 public Context (MInputMethod im)
2142 domain = new Xex.Domain (im.domain, this);
2143 state_list = new List<State> ();
2144 state_list.Add ((State) im.states.val);
2145 keys = new KeySeq ();
2148 private void adjust_markers (int from, int to, object inserted)
2150 int ins = (inserted == null ? 0
2151 : inserted is int ? 1
2152 : ((MText) inserted).Length);
2153 int diff = ins - (to - from);
2155 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
2157 int pos = plist.Integer;
2161 plist.val = pos + diff;
2166 if (cursor_pos >= to)
2168 else if (cursor_pos > from)
2172 private void preedit_replace (int from, int to, int c)
2174 preedit.Del (from, to);
2175 preedit.Ins (from, c);
2176 adjust_markers (from, to, c);
2179 private void preedit_replace (int from, int to, MText mt)
2181 preedit[from, to] = mt;
2182 adjust_markers (from, to, mt);
2185 internal void insert (Xex.Term arg)
2188 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
2190 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
2191 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2194 private void update_candidate ()
2196 object candidate = candidates.Current;
2198 if (candidate is MText)
2200 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2201 candidate_to = candidate_from + ((MText) candidate).Length;
2205 preedit_replace (candidate_from, candidate_to, (int) candidate);
2206 candidate_to = candidate_from + 1;
2208 preedit.PushProp (candidate_from, candidate_to,
2210 cursor_pos = candidate_from;
2211 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2215 internal void insert_candidates (Xex.Term arg)
2218 Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
2221 column = v.Value.Intval;
2222 candidates = new Candidates (arg.Listval, column);
2223 candidate_from = candidate_to = cursor_pos;
2224 update_candidate ();
2227 internal void select (int n)
2229 if (candidates != null)
2231 candidates.Select (n);
2232 update_candidate ();
2236 internal int marker (MSymbol sym)
2238 int pos = cursor_pos;
2240 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2242 switch (sym.Name[0])
2244 case '<': pos = 0; break;
2245 case '>': pos = preedit.Length; break;
2246 case '-': pos = cursor_pos - 1; break;
2247 case '+': pos = cursor_pos + 1; break;
2252 preedit.FindProp (Mcandidates, pos - 1,
2259 if (cursor_pos < preedit.Length - 1)
2262 preedit.FindProp (Mcandidates, pos,
2266 pos = preedit.Length;
2269 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2274 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2276 pos = int.Parse (sym.Name.Substring (2));
2280 object val = markers.Get (sym);
2288 internal int char_at (int pos)
2295 if (preceding_text.Length < -pos)
2297 MPlist plist = new MPlist ();
2298 plist.Push (MSymbol.integer, pos);
2299 if (GetSurroundingText != null
2300 && GetSurroundingText (this, plist)
2302 && preceding_text.Length < plist.Text.Length)
2303 preceding_text = plist.Text;
2305 c = (-pos < preceding_text.Length
2306 ? preceding_text[preceding_text.Length + pos] : -1);
2308 else if (pos >= 0 && pos < preedit.Length)
2312 pos -= preedit.Length;
2313 if (pos >= following_text.Length)
2315 MPlist plist = new MPlist ();
2316 plist.Push (MSymbol.integer, pos + 1);
2317 if (GetSurroundingText != null
2318 && GetSurroundingText (this, plist)
2320 && following_text.Length < plist.Text.Length)
2321 following_text = plist.Text;
2323 c = (pos < following_text.Length ? following_text[pos] : -1);
2328 internal void delete (int pos)
2330 if (pos < cursor_pos)
2331 preedit_replace (pos, cursor_pos, null);
2333 preedit_replace (cursor_pos, pos, null);
2334 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2337 internal void show ()
2339 candidate_show = true;
2340 changed |= ChangedStatus.CandidateShow;
2343 internal void hide ()
2345 candidate_show = false;
2346 changed |= ChangedStatus.CandidateShow;
2349 internal void move (int pos)
2353 else if (pos > preedit.Length)
2354 pos = preedit.Length;
2355 if (pos != cursor_pos)
2358 changed |= ChangedStatus.Preedit;
2362 internal void mark (MSymbol sym)
2364 MPlist slot = markers.Find (sym);
2367 markers.Push (sym, cursor_pos);
2369 slot.val = cursor_pos;
2372 internal void pushback (int n)
2385 if (key_head > keys.keyseq.Count)
2386 key_head = keys.keyseq.Count;
2390 internal void pushback (KeySeq keyseq)
2394 if (key_head < keys.keyseq.Count)
2395 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2396 for (int i = 0; i < keyseq.keyseq.Count; i++)
2397 keys.keyseq.Add (keyseq.keyseq[i]);
2400 internal void pop ()
2402 if (key_head < keys.keyseq.Count)
2403 keys.keyseq.RemoveRange (key_head, 1);
2406 internal void undo (int n)
2409 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2411 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2415 internal void commit ()
2417 produced.Cat (preedit);
2419 changed |= ChangedStatus.Preedit;
2422 internal void shift (MSymbol sym)
2426 if (sym == MSymbol.t)
2428 state = state_list.Last ();
2429 if (state_list.Count > 1)
2430 state_list.RemoveAt (state_list.Count - 1)
2434 state = (State) im.states.Get (sym);
2436 throw new Exception ("Unknown state: " + state.name);
2440 state = state_list.Last;
2441 state_list.RemoveAt (state_list.Count -1);
2443 if (state == (State) im.states.val)
2450 state_key_head = key_head;
2451 state_pos = cursor_pos;
2452 state_preedit = preedit.Dup ();
2453 if (state != state_list.Last ())
2455 state_list.Add (state);
2456 state_var_values = domain.SaveValues ();
2457 status = state.title;
2460 changed |= ChangedStatus.StateTitle;
2462 = (Xex) state.branches.Get (MSymbol.t);
2463 if (on_entry != null)
2464 on_entry.Eval (domain);
2469 internal void reset ()
2472 state_preedit.Del ();
2476 key_head = commit_key_head = 0;
2477 state_list.Clear ();
2478 state_list.Add ((State) im.states.Val);
2483 internal object GetCandidates (out int column)
2486 if (cursor_pos == 0)
2488 Candidates candidates
2489 = (Candidates) preedit.GetProp (cursor_pos - 1, Mcandidates);
2490 if (candidates == null)
2492 column = candidates.Column;
2493 return candidates.Current;
2496 internal void HandleKey ()
2500 public bool Toggle ()
2506 public bool UnhandledKey (out const Key key)
2508 key = unhandled_key;
2509 return key_unhandled;
2512 public bool Produced (out const MText mt)
2515 return (produced.Length > 0);
2520 // true: All keys are handled and there's no text to commit.
2521 // false: Some key is unhandled or there's a text to commit.
2522 // The caller should use methods UnhandledKey and Produced.
2524 public bool Filter (Key key)
2526 if (check_reload (key))
2528 changed = ChangedStatus.None;
2530 preceding_text.Del ();
2531 following_text.Del ();
2533 key_unhandled = false;
2536 while (key_head < keys.Length)
2538 if (! handle_key ())
2540 unhandled_key = keys[key_head++];
2541 key_unhandled = true;
2547 keys.RemoveRange (0, key_head);
2548 return (! key_unhandled && produced.Length == 0);