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 event Callback PreeditChanged;
23 public static event Callback StatusChanged;
24 public static event Callback CandidateChanged;
25 public static Callback GetSurroundingText;
26 public static Callback DeleteSurroundingText;
28 internal static Xex.Domain im_domain
29 = new Xex.Domain ("input-method", null);
30 private static MSymbol Minput_method = "input-method";
31 private static MSymbol Mdescription = "description";
32 private static MSymbol Mvariable = "variable";
33 private static MSymbol Mcommand = "command";
34 private static MSymbol Mmodule = "module";
35 private static MSymbol Mtitle = "title";
36 private static MSymbol Minclude = "include";
37 private static MSymbol Mmacro = "macro";
38 private static MSymbol Mmap = "map";
39 private static MSymbol Mmap_list = "map-list";
40 private static MSymbol Mstate = "state";
41 internal static MSymbol Mcandidates = "candidates";
42 private static MSymbol Mat_minus_zero = "@-0";
44 private static Xex.Symbol Qmap = "map";
45 private static Xex.Symbol Qrule = "rule";
46 private static Xex.Symbol Qkeyseq = "keyseq";
47 private static Xex.Symbol Qprogn = "progn";
48 private static Xex.Symbol Qcatch = "catch";
49 private static Xex.Symbol Qinsert = "insert";
50 private static Xex.Symbol Qinsert_candidates = "insert-candidates";
51 private static Xex.Symbol Qchar_at = "char-at";
52 private static Xex.Symbol Qselect = "select";
53 private static Xex.Symbol Qdelete = "delete";
54 private static Xex.Symbol Qshift = "shift";
55 private static Xex.Symbol Qmove = "move";
56 private static Xex.Symbol Qmark = "mark";
57 private static Xex.Symbol Qset = "set";
58 private static Xex.Symbol Qadd = "add";
59 private static Xex.Symbol Qsub = "sub";
60 private static Xex.Symbol Qmul = "mul";
61 private static Xex.Symbol Qdiv = "div";
62 private static Xex.Symbol Qcond = "cond";
63 private static Xex.Symbol Qsname = "sname";
64 private static Xex.Symbol Qmname = "mname";
65 private static Xex.Symbol Qstate_hook = "state-hook";
66 private static Xex.Symbol Qcatch_all_branch = "catch-all-branch";
67 private static Xex.Symbol Qbranch = "branch";
68 private static Xex.Symbol Qstate = "state";
69 private static Xex.Symbol Qtitle = "title";
70 private static Xex.Symbol Qeq = "=";
71 private static Xex.Symbol Qeqeq = "==";
72 private static Xex.Symbol Qcandidates_group_size = "candidates-group-size";
74 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
75 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
77 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
78 = new Dictionary<MDatabase.Tag, MInputMethod> ();
80 internal static MInputMethod im_global = null;
83 protected enum LoadStatus
93 public enum ChangedStatus
100 CandidateIndex = 0x10,
101 CandidateShow = 0x20,
102 Preedit = PreeditText | CursorPos,
103 Candidate = CandidateList | CandidateIndex | CandidateShow,
106 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
107 | ChangedStatus.CandidateIndex
108 | ChangedStatus.CandidateShow);
111 public enum KeyModifier
114 Shift_L = 0x00400000,
115 Shift_R = 0x00800000,
117 Control_L = 0x01000000,
118 Control_R = 0x02000000,
119 Control = 0x03000000,
134 private static Dictionary<string, uint> keysyms
135 = new Dictionary<string, uint> ();
136 private static Dictionary<string, KeyModifier> keymodifiers
137 = new Dictionary<string, KeyModifier> ();
138 private static uint keysym_base = 0x200000;
139 private static uint char_mask = ~((uint) KeyModifier.All);
140 public static Key Reload;
144 keysyms["bs"] = keysyms["backspace"] = 0x08;
145 keysyms["tab"] = 0x09;
146 keysyms["lf"] = keysyms["linefeed"] = 0x10;
147 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
148 keysyms["esc"] = keysyms["escape"] = 0x1B;
149 keysyms["spc"] = keysyms["space"] = 0x20;
150 keysyms["del"] = keysyms["delete"] = 0x7F;
151 keymodifiers["shift-l"] = KeyModifier.Shift_L;
152 keymodifiers["shift-r"] = KeyModifier.Shift_R;
153 keymodifiers["shift"] = KeyModifier.Shift;
154 keymodifiers["control-l"] = KeyModifier.Control_L;
155 keymodifiers["control-r"] = KeyModifier.Control_R;
156 keymodifiers["control"] = KeyModifier.Control;
157 keymodifiers["alt-l"] = KeyModifier.Alt_L;
158 keymodifiers["alt-r"] = KeyModifier.Alt_R;
159 keymodifiers["alt"] = KeyModifier.Alt;
160 keymodifiers["altgr"] = KeyModifier.AltGr;
161 keymodifiers["super"] = KeyModifier.Super;
162 keymodifiers["hyper"] = KeyModifier.Hyper;
163 Reload = new Key (keysym_base);
164 keysyms["-reload"] = keysym_base++;
167 private static uint decode_keysym (MSymbol keysym)
170 string name = keysym.Name;
172 if (name.Length == 1)
174 name = name.ToLower ();
175 if (! keysyms.TryGetValue (name, out key))
176 keysyms[name] = key = keysym_base++;
180 private static uint combine_modifiers (uint c, KeyModifier modifiers)
182 if (c < 0x7F && c != 0x20)
184 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
185 && Char.IsLower ((char) c))
187 modifiers &= ~KeyModifier.Shift;
188 c = Char.ToUpper ((char) c);
190 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
192 modifiers &= ~KeyModifier.Control;
196 return c | (uint) modifiers;
199 public Key (uint c) { key = c; }
200 public Key (int c) { key = (uint) c; }
202 public Key (uint c, KeyModifier modifiers)
204 key = combine_modifiers (c, modifiers);
207 public Key (MSymbol keysym, KeyModifier modifiers)
209 key = combine_modifiers (decode_keysym (keysym), modifiers);
212 public Key (MSymbol keysym)
214 string str = keysym.Name;
215 int len = str.Length;
217 KeyModifier modifiers = KeyModifier.None;
219 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
222 modifiers |= KeyModifier.Shift;
223 else if (str[i] == 'C')
224 modifiers |= KeyModifier.Control;
225 else if (str[i] == 'A')
226 modifiers |= KeyModifier.Alt;
227 else if (str[i] == 'G')
228 modifiers |= KeyModifier.AltGr;
229 else if (str[i] == 's')
230 modifiers |= KeyModifier.Super;
231 else if (str[i] == 'H')
232 modifiers |= KeyModifier.Hyper;
235 key = combine_modifiers (str[i], modifiers);
237 key = combine_modifiers (decode_keysym (keysym), modifiers);
240 public Key (MPlist plist)
242 KeyModifier modifiers = KeyModifier.None;
245 for (p = plist; ! p.IsEmpty; p = p.next)
249 if (! p.next.IsEmpty)
250 throw new Exception ("Invalid Key: " + plist);
253 else if (! p.IsSymbol)
254 throw new Exception ("Invalid Key: " + plist);
257 string name = p.Symbol.Name.ToLower ();
260 if (! keymodifiers.TryGetValue (name, out m))
265 if (p.IsEmpty || ! p.next.IsEmpty)
266 throw new Exception ("Invalid Key: " + plist);
268 key = combine_modifiers ((uint) p.Integer, modifiers);
270 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
273 public bool HasModifier
275 get { return ((key & (uint) KeyModifier.All) != 0); }
278 public static bool operator== (Key k1, Key k2)
280 return k1.key == k2.key;
283 public static bool operator!= (Key k1, Key k2)
285 return k1.key != k2.key;
288 public override bool Equals (object o) { return key == ((Key) o).key; }
290 public override int GetHashCode () { return (int) key; }
292 public bool Match (Key k)
296 if ((k.key & char_mask) != (key & char_mask))
298 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
299 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
300 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
301 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
302 && (m2 & KeyModifier.Shift) != KeyModifier.None))
303 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
304 || ((m1 & KeyModifier.Control) == KeyModifier.Control
305 && (m2 & KeyModifier.Control) != KeyModifier.None))
306 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
307 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
308 && (m2 & KeyModifier.Alt) != KeyModifier.None))
309 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
314 return (int) (key & 0x1FFFFF);
317 public override string ToString ()
322 foreach (KeyValuePair<string, uint> kv in keysyms)
323 if ((uint) c == kv.Value)
331 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
333 if (m != KeyModifier.None)
335 if ((m & KeyModifier.Shift) != KeyModifier.None)
337 if ((m & KeyModifier.Control) != KeyModifier.None)
339 if ((m & KeyModifier.Alt) != KeyModifier.None)
341 if ((m & KeyModifier.AltGr) != KeyModifier.None)
343 if ((m & KeyModifier.Super) != KeyModifier.None)
345 if ((m & KeyModifier.Hyper) != KeyModifier.None)
352 internal class KeySeq : Xex.TermValue
354 public List<Key> keyseq = new List<Key> ();
356 public override Xex.TermValue Clone ()
358 KeySeq ks = new KeySeq ();
359 ks.keyseq.InsertRange (0, keyseq);
365 public KeySeq (MPlist plist)
367 foreach (MPlist p in plist)
370 keyseq.Add (new Key (p.Symbol));
371 else if (p.IsInteger)
372 keyseq.Add (new Key ((char) p.Integer));
374 keyseq.Add (new Key (p.Plist));
376 throw new Exception ("Invalid Key Sequence: " + plist);
380 public KeySeq (MText mt) : base ()
382 for (int i = 0; i < mt.Length; i++)
383 keyseq.Add (new Key ((uint) mt[i]));
386 public KeySeq (List<Xex.Term> list)
388 int len = list.Count;
390 for (int i = 0; i < len; i++)
393 keyseq.Add (new Key (list[i].Intval));
394 else if (list[i].IsStr)
395 keyseq.Add (new Key (list[i].Strval));
396 else if (list[i].IsSymbol)
397 keyseq.Add (new Key ((string) list[i].Symval));
399 throw new Exception ("Invalid key: " + list[i]);
403 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
405 Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain);
406 return (term.IsStr ? new KeySeq ((MText) term.Strval)
407 : new KeySeq (term.Listval));
410 public override string ToString ()
413 foreach (Key key in keyseq)
414 if (key.HasModifier || key.ToChar () < 0x20)
417 foreach (Key k in keyseq)
421 mt.Cat (k.ToString ());
423 return (string) mt.Cat (")");
426 foreach (Key k in keyseq)
430 if (c == '\\' || c == '"')
434 return (string) mt.Cat ("\"");
441 public MText description;
442 internal List<KeySeq> keys;
444 public Command (MPlist p)
448 description = parse_description (p);
449 if (description == null)
450 description = "No description";
451 keys = new List<KeySeq> ();
452 for (p = p.next; ! p.IsEmpty; p = p.next)
455 keys.Add (new KeySeq (p.Text));
457 keys.Add (new KeySeq (p.Plist));
461 public Command (XmlNode node)
463 name = node.Attributes[0].Value;
464 keys = new List<KeySeq> ();
465 for (node = node.FirstChild; node != null; node = node.NextSibling)
467 if (node.Name == "description")
468 description = parse_description (node);
469 else if (node.Name == "keyseq")
470 keys.Add ((KeySeq) KeySeq.parser (null, node));
474 public override string ToString ()
476 string str = "(" + name + " \"" + (string) description;
477 foreach (KeySeq keyseq in keys)
483 internal class Plugin
486 private Assembly assembly;
487 private Type plugin_type;
489 public Plugin (string name)
494 public MethodInfo GetMethod (Xex.Symbol name)
496 if (assembly == null)
498 assembly = Assembly.LoadFrom (name + ".dll");
499 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
502 MethodInfo info = plugin_type.GetMethod ((string) name);
504 throw new Exception ("Invalid plugin method: " + name);
508 public override string ToString ()
510 return String.Format ("(module {0}", name);
514 internal class PluginMethod : Xex.Function
516 private Plugin plugin;
517 private MethodInfo method_info;
518 object[] parameters = new object[2];
520 public PluginMethod (Plugin plugin, string name)
521 : base ((Xex.Symbol) name, 0, -1)
523 this.plugin = plugin;
526 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
529 args = (Xex.Term[]) args.Clone ();
530 for (int i = 0; i < args.Length; i++)
532 args[i] = args[i].Eval (domain);
536 if (method_info == null)
537 method_info = plugin.GetMethod (name);
538 parameters[0] = domain.context;
539 parameters[1] = args;
540 return (Xex.Term) method_info.Invoke (null, parameters);
544 internal abstract class Marker : Xex.TermValue
546 private MSymbol name;
548 private Marker (MSymbol name)
553 public abstract int Position (Context ic);
555 public virtual void Mark (Context ic)
557 throw new Exception ("Can't set predefined marker: " + name);
559 public virtual int CharAt (Context ic)
561 return ic.preedit[Position (ic)];
564 public override string ToString ()
566 return "<marker>" + name.Name + "</marker>";
569 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
571 return Get ((MSymbol) node.InnerText);
574 public class Named : Marker
576 public Named (MSymbol name) : base (name) { }
578 public override int Position (Context ic)
580 MPlist p = ic.marker_positions.Find (name);
581 return (p == null ? 0 : p.Integer);
584 public override void Mark (Context ic)
586 ic.marker_positions.Put (name, ic.cursor_pos);
590 public class Predefined : Marker
594 public Predefined (MSymbol name) : base (name)
596 tag = ((string) name)[1];
599 public override int Position (Context ic)
603 case '>': return ic.preedit.Length;
604 case '-': return ic.cursor_pos - 1;
605 case '+': return ic.cursor_pos + 1;
607 if (ic.cursor_pos > 0)
609 int pos = ic.cursor_pos;
611 ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to);
616 if (ic.cursor_pos < ic.preedit.Length - 1)
618 int pos = ic.cursor_pos;
620 ic.preedit.FindProp (Mcandidates, pos, out from, out pos);
623 return ic.preedit.Length;
630 public class PredefinedAbsolute : Marker
634 public PredefinedAbsolute (MSymbol name) : base (name)
636 if (! int.TryParse (((string) name).Substring (1), out pos))
637 throw new Exception ("Invalid marker name: " + name);
640 public override int Position (Context ic)
642 return (pos < ic.preedit.Length ? pos : ic.preedit.Length);
646 public class PredefinedSurround : Marker
648 private int distance;
650 public PredefinedSurround (MSymbol name) : base (name)
652 if (! int.TryParse (((string) name).Substring (2), out distance))
653 throw new Exception ("Invalid marker name: " + name);
658 public override int Position (Context ic)
660 return ic.cursor_pos + distance;
663 public override int CharAt (Context ic)
665 int pos = ic.cursor_pos + distance;
667 return ic.GetSurroundingChar (pos);
668 else if (pos >= ic.preedit.Length)
669 return ic.GetSurroundingChar (pos - ic.preedit.Length);
670 return ic.preedit[pos];
674 static internal Dictionary<MSymbol,Predefined> predefined_markers;
678 predefined_markers = new Dictionary<MSymbol, Predefined> ();
679 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]" };
680 foreach (MSymbol s in symlist)
681 predefined_markers[s] = new Predefined (s);
684 public static Marker Get (MSymbol name)
686 string str = name.Name;
690 if (predefined_markers.TryGetValue (name, out pred))
693 throw new Exception ("Invalid marker name: " + name);
694 if (Char.IsDigit (str[1]))
695 return new PredefinedAbsolute (name);
696 if (str.Length == 2 || name == Mat_minus_zero
697 || ! (str[1] == '-' || str[1] == '+'))
698 throw new Exception ("Invalid marker name: " + name);
699 return new PredefinedSurround (name);
701 return new Named (name);
705 internal class Candidates
712 public Block (int index, Xex.Term term)
716 Data = (MText) term.Strval;
719 MPlist plist = new MPlist ();
721 foreach (Xex.Term t in term.Listval)
722 p = p.Add (MSymbol.mtext, (MText) t.Strval);
727 public Block (int index, MPlist plist)
732 else if (plist.IsPlist)
735 throw new Exception ("Invalid candidate: " + plist);
740 get { return (Data is MText
741 ? ((MText) Data).Length
742 : ((MPlist) Data).Count); }
745 public object this[int i]
748 if (Data is MText) return ((MText) Data)[i];
749 return ((MPlist) Data)[i];
754 private Block[] blocks;
756 private int index = 0;
757 public object[] group;
759 private bool IsFixed { get { return group != null; } }
762 Block last = blocks[blocks.Length - 1];
763 return last.Index + last.Count; }
767 get { return (IsFixed ? index % group.Length
768 : index - blocks[row].Index); }
771 public object Group {
772 get { return (IsFixed ? group : blocks[row].Data); }
775 public int GroupLength
780 int nitems = group.Length;
781 int start = index - (index % nitems);
783 return (start + nitems <= total ? nitems : total - start);
785 return blocks[row].Count;
789 public object Current {
791 return (IsFixed ? group[index % group.Length]
792 : blocks[row][index - blocks[row].Index]);
796 public Candidates (MPlist list, int column)
798 int nblocks = list.Count;
800 blocks = new Block[nblocks];
801 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
802 start += (blocks[i] = new Block (index, list)).Count;
804 group = new object[column];
807 public Candidates (Xex.Term[] candidates, int column)
809 int nblocks = candidates.Length;
811 blocks = new Block[nblocks];
812 for (int i = 0, start = 0; i < nblocks; i++)
813 start += (blocks[i] = new Block (index, candidates[i])).Count;
815 group = new object[column];
818 public static void Detach (Context ic)
820 ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
821 ic.candidates = null;
822 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
826 // Fill the array "group" by candidates stating from START.
827 // START must be a multiple of "column". Return the number of
828 // valid candidates in "group".
830 private int fill_group (int start)
832 int nitems = group.Length;
837 while (start < b.Index)
840 while (start >= b.Index + b.Count)
846 for (int i = 0; i < nitems; i++, start++)
851 if (r == blocks.Length)
862 // Update "row" to what contains the first candidate of
863 // the previous candidate-group, update "current_index", and
864 // update "group" if necessary. Return the previous
865 // candidate-group. Set NITEMS to the number of valid
866 // candidates contained in that group.
868 public int PrevGroup ()
875 nitems = group.Length;
876 if ((index -= col + nitems) < 0)
877 index = (Total / nitems) * nitems;
878 nitems = fill_group (index);
882 row = row > 0 ? row-- : blocks.Length - 1;
883 nitems = blocks[row].Count;
884 index = blocks[row].Index;
886 index += col < nitems ? col : nitems - 1;
890 public int NextGroup ()
897 nitems = group.Length;
898 if ((index += nitems - col) >= Total)
900 nitems = fill_group (index);
904 row = row < blocks.Length - 1 ? row + 1 : 0;
905 nitems = blocks[row].Count;
906 index = blocks[row].Count;
908 index += col < nitems ? col : nitems - 1;
918 int nitems = PrevGroup ();
919 index += col < nitems - 1 ? col : nitems - 1;
928 int nitems = GroupLength;
930 if (col == nitems - 1)
932 nitems = NextGroup ();
946 index += GroupLength - (Column + 1);
949 public void Select (int col)
951 int maxcol = GroupLength - 1;
954 index = index - Column + col;
957 public override string ToString ()
959 return (String.Format ("<candidates row={0} col={1}>", row, index)
965 internal class Selector : Xex.TermValue
967 static new Dictionary<MSymbol, Selector> selectors;
971 selectors = new Dictionary<MSymbol, Selector> ();
972 MSymbol[] symlist = new MSymbol[] { "@<", "@=", "@>", "@-", "@+",
974 foreach (MSymbol s in symlist)
975 selectors[s] = new Selector (s);
976 selectors["@first"] = new Selector ('<');
977 selectors["@current"] = new Selector ('=');
978 selectors["@last"] = new Selector ('>');
979 selectors["@previous"] = new Selector ('-');
980 selectors["@next"] = new Selector ('+');
981 selectors["@previous-candidate-change"] = new Selector ('[');
982 selectors["@next-candidate-change"] = new Selector (']');
987 private Selector (MSymbol sym) { tag = sym.Name[1]; }
989 private Selector (char tag) { this.tag = tag; }
991 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
993 return Get ((MSymbol) node.InnerText);
996 public static Xex.TermValue Get (MSymbol name)
999 if (! selectors.TryGetValue (name, out selector))
1000 throw new Exception ("Invalid selector name: " + name);
1004 public void Select (Candidates candidates)
1008 case '<': candidates.First (); break;
1009 case '>': candidates.Last (); break;
1010 case '-': candidates.Prev (); break;
1011 case '+': candidates.Next (); break;
1012 case '[': candidates.PrevGroup (); break;
1013 case ']': candidates.NextGroup (); break;
1021 public MSymbol name;
1022 public List<Entry> entries = new List<Entry> ();
1024 public Map (MSymbol name) { this.name = name; }
1028 public KeySeq keyseq;
1029 public Xex.Term[] actions;
1031 public Entry (Xex.Domain domain, KeySeq keyseq, Xex.Term[] actions)
1033 this.keyseq = keyseq;
1034 this.actions = actions;
1038 public override string ToString ()
1040 string str = "(" + name;
1041 foreach (Entry e in entries)
1042 str += " " + e.keyseq.ToString ();
1047 internal class Keymap
1049 public Dictionary<Key, Keymap> submaps;
1050 public Xex.Term[] map_actions, branch_actions;
1052 public Keymap () { }
1054 public void Add (KeySeq keys, int index,
1055 Xex.Term[] map_actions, Xex.Term[] branch_actions)
1057 if (index == keys.keyseq.Count)
1059 this.map_actions = map_actions;
1060 this.branch_actions = branch_actions;
1064 Key key = keys.keyseq[index];
1067 if (submaps == null)
1068 submaps = new Dictionary<Key, Keymap> ();
1070 submaps.TryGetValue (key, out sub);
1072 submaps[key] = sub = new Keymap ();
1073 sub.Add (keys, index + 1, map_actions, branch_actions);
1077 public void AddMap (Map map, Xex.Term[] branch_actions)
1079 foreach (Map.Entry entry in map.entries)
1080 Add (entry.keyseq, 0, entry.actions, branch_actions);
1083 public Keymap Lookup (KeySeq keys, ref int index)
1087 if (index < keys.keyseq.Count
1089 && submaps.TryGetValue (keys.keyseq[index], out sub))
1092 return sub.Lookup (keys, ref index);
1097 private void describe (MText mt, KeySeq keyseq)
1099 if (map_actions != null || branch_actions != null)
1103 mt.Cat ('(').Cat (keyseq.ToString ());
1104 if (map_actions != null)
1105 foreach (Xex.Term term in map_actions)
1106 mt.Cat (' ').Cat (term.ToString ());
1107 if (branch_actions != null)
1108 foreach (Xex.Term term in branch_actions)
1109 mt.Cat (' ').Cat (term.ToString ());
1112 if (submaps != null)
1113 foreach (KeyValuePair<Key, Keymap> kv in submaps)
1115 keyseq.keyseq.Add (kv.Key);
1116 kv.Value.describe (mt, keyseq);
1117 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
1121 public override string ToString ()
1124 KeySeq keyseq = new KeySeq ();
1126 describe (mt, keyseq);
1131 internal class State
1133 public Xex.Symbol name;
1135 public Xex.Term[] enter_actions, fallback_actions;
1136 public Keymap keymap = new Keymap ();
1138 public State (Xex.Symbol name, MText title)
1144 public State (MInputMethod im, XmlNode node)
1146 this.name = node.Attributes[Qsname].Value;
1147 XmlAttribute attr = node.Attributes[Qtitle];
1149 title = (MText) attr.Value;
1152 keymap = new Keymap ();
1153 for (node = node.FirstChild; node != null; node = node.NextSibling)
1155 if (node.Name == Qstate_hook)
1156 enter_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1157 else if (node.Name == Qcatch_all_branch)
1158 fallback_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1159 else if (node.Name == Qbranch)
1161 MSymbol mapname = node.Attributes[Qmname].Value;
1163 if (im.maps.TryGetValue (mapname, out map))
1164 keymap.AddMap (map, Xex.ParseTerms (im.domain,
1167 throw new Exception ("Unknown map: " + mapname);
1172 public State (MInputMethod im, MPlist plist)
1174 if (! plist.IsSymbol)
1175 throw new Exception ("Invalid state: " + plist);
1176 this.name = plist.Symbol.Name;
1185 keymap = new Keymap ();
1186 for (; ! plist.IsEmpty; plist = plist.next)
1188 if (! plist.IsPlist)
1189 throw new Exception ("Invalid branch: " + plist);
1190 MPlist p = plist.Plist;
1192 throw new Exception ("Invalid branch: " + p);
1193 MSymbol mapname = p.Symbol;
1194 if (mapname == MSymbol.t)
1195 enter_actions = im.parse_actions (p.next, false);
1196 else if (mapname == MSymbol.nil)
1197 fallback_actions = im.parse_actions (p.next, false);
1201 if (im.maps.TryGetValue (mapname, out map))
1202 keymap.AddMap (map, im.parse_actions (p.next, false));
1204 throw new Exception ("Unknown map: " + mapname);
1209 public override string ToString ()
1211 MText mt = "(" + name;
1214 mt.Cat (" \"" + title + "\"");
1215 mt.Cat (keymap.ToString ());
1216 return (string) mt + ")";
1221 internal Xex.Domain domain;
1223 protected LoadStatus load_status = LoadStatus.None;
1224 protected MDatabase.Tag tag;
1225 private MDatabase mdb;
1227 private MText description;
1228 internal MText title;
1229 internal Command[] commands;
1230 internal Xex.Symbol[] var_names;
1231 internal Dictionary<MSymbol, Plugin> plugins;
1232 internal Dictionary<MSymbol, Map> maps;
1233 internal Dictionary<Xex.Symbol, State> states;
1234 internal State initial_state;
1236 static MInputMethod ()
1238 im_domain.DefTerm ("keyseq", KeySeq.parser);
1239 im_domain.DefTerm ("marker", Marker.parser);
1240 im_domain.DefTerm ("selector", Selector.parser);
1242 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1243 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, -1);
1244 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1245 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1246 im_domain.DefSubr (Fshow, "show", false, 0, 0);
1247 im_domain.DefSubr (Fhide, "hide", false, 0, 0);
1248 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1249 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1250 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1251 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1252 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1253 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1254 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1255 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1256 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
1257 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1258 im_domain.DefSubr (Fkey_count, "key-count", false, 1, 1);
1259 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag",
1262 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
1263 List<MDatabase> list = MDatabase.List (tag);
1264 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
1265 foreach (MDatabase mdb in list)
1266 im_table[mdb.tag] = new MInputMethod (mdb.tag);
1270 private MInputMethod (MDatabase.Tag tag)
1273 domain = new Xex.Domain (tag[1].Name, im_domain, null);
1276 // Instance Properties
1277 public MSymbol Language { get { return tag[1]; } }
1278 public MSymbol Name { get { return tag[2]; } }
1279 public MSymbol SubName { get { return tag[3]; } }
1281 public bool Info (out MText description,
1283 out Xex.Variable[] variables,
1284 out Command[] commands)
1286 if ((load_status & LoadStatus.Header) != LoadStatus.Header
1287 && ! load_header ())
1295 description = this.description;
1297 if (var_names == null)
1301 variables = new Xex.Variable[var_names.Length];
1303 foreach (Xex.Symbol name in var_names)
1304 variables[i++] = domain.GetVar (name, false);
1306 commands = this.commands;
1310 public static MInputMethod Find (MSymbol language, MSymbol name)
1312 return Find (language, name, MSymbol.nil);
1315 public static MInputMethod Find (MSymbol language, MSymbol name,
1318 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
1322 return (im_table.TryGetValue (tag, out im) ? im : null);
1325 private bool Open ()
1327 return ((load_status == LoadStatus.Full) || load_body ());
1330 public static MInputMethod[] List ()
1332 MInputMethod[] array = new MInputMethod[im_table.Count];
1335 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
1336 array[i++] = kv.Value;
1340 private bool load_header ()
1342 mdb = MDatabase.Find (tag);
1345 mdb.name_table = Xex.Symbol.Table;
1347 MSymbol format = mdb.Format;
1349 if (format == MSymbol.plist)
1350 load ((MPlist) mdb.Load (Mmap), false);
1353 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
1354 load (doc.DocumentElement, false);
1356 } catch (Exception e) {
1357 Console.WriteLine ("{0}\n", e);
1358 load_status = LoadStatus.Error;
1361 load_status |= LoadStatus.Header;
1365 private bool load_body ()
1367 mdb = MDatabase.Find (tag);
1370 mdb.name_table = Xex.Symbol.Table;
1372 object obj = mdb.Load ();
1374 load ((MPlist) obj, true);
1376 load ((XmlDocument) obj, true);
1377 } catch (Exception e) {
1378 Console.WriteLine (e);
1379 load_status = LoadStatus.Error;
1382 load_status = LoadStatus.Full;
1386 private void add_default_state ()
1388 Xex.Symbol Qinit = "init";
1389 State state = new State (Qinit, title);
1390 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1391 state.keymap.AddMap (kv.Value, null);
1392 states[Qinit] = initial_state = state;
1395 private void load (MPlist plist, bool full)
1397 maps = new Dictionary<MSymbol, Map> ();
1398 states = new Dictionary<Xex.Symbol, State> ();
1400 for (; ! plist.IsEmpty; plist = plist.next)
1403 MPlist pl = plist.Plist;
1406 MSymbol sym = pl.Symbol;
1409 if (sym == Mdescription)
1410 description = parse_description (pl);
1411 else if (sym == Mtitle)
1416 else if (sym == Mvariable)
1417 parse_variables (pl);
1418 else if (sym == Mcommand)
1419 parse_commands (pl);
1424 else if (sym == Minclude)
1426 else if (sym == Mmacro)
1428 else if (sym == Mmap)
1430 else if (sym == Mstate)
1435 if (description == null)
1436 description = (MText) "No description";
1438 title = new MText (tag[2].Name);
1439 if (commands == null)
1440 commands = new Command[0];
1443 if (states.Count == 0)
1444 add_default_state ();
1447 private void load (XmlNode node, bool full)
1449 bool skip_header = load_status == LoadStatus.Header;
1451 maps = new Dictionary<MSymbol, Map> ();
1452 states = new Dictionary<Xex.Symbol, State> ();
1454 if (node.NodeType == XmlNodeType.Document)
1455 node = node.FirstChild;
1456 while (node.NodeType != XmlNodeType.Element)
1457 node = node.NextSibling;
1458 for (node = node.FirstChild; node != null; node = node.NextSibling)
1460 if (node.NodeType != XmlNodeType.Element)
1464 if (node.Name == "description")
1465 description = parse_description (node);
1466 else if (node.Name == "title")
1467 title = parse_title (node);
1468 else if (node.Name == "variable-list")
1469 parse_variables (node);
1470 else if (node.Name == "command-list")
1471 parse_commands (node);
1475 if (node.Name == "module-list")
1476 parse_plugins (node);
1477 else if (node.Name == "macro-list")
1478 parse_macros (node);
1479 else if (node.Name == "map-list")
1481 else if (node.Name == "state-list")
1482 parse_states (node);
1485 if (description == null)
1486 description = (MText) "No description";
1488 title = new MText (tag[2].Name);
1489 if (commands == null)
1490 commands = new Command[0];
1493 if (states.Count == 0)
1494 add_default_state ();
1497 private static MText parse_description (MPlist plist)
1503 plist = plist.Plist;
1504 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1505 && plist.next.IsMText)
1506 return plist.next.Text;
1511 private static MText parse_description (XmlNode node)
1513 if (node.HasChildNodes)
1514 node = node.FirstChild;
1515 return node.InnerText;
1518 private static MText parse_title (XmlNode node)
1520 return node.InnerText;
1523 private void new_variable (Xex.Symbol name, string desc, int val,
1524 MPlist pl, Xex.Variable vari)
1532 int nrange = pl.Count;
1533 range = new int[nrange * 2];
1534 for (int i = 0; i < nrange; i++)
1538 MPlist p = pl.Plist;
1540 if (! p.IsInteger || ! p.next.IsInteger)
1541 throw new Exception ("Invalid range: " + p);
1542 range[i * 2] = p.Integer;
1543 range[i * 2 + 1] = p.next.Integer;
1545 else if (pl.IsInteger)
1546 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1548 throw new Exception ("Invalid range: " + pl);
1552 domain.Defvar (new Xex.Variable.Int (domain, name, desc, val, range));
1555 Xex.Term term = new Xex.Term (val);
1557 vari.DefaultValue = term;
1562 private void new_variable (Xex.Symbol name, string desc, MText val,
1563 MPlist pl, Xex.Variable vari)
1571 range = new string[pl.Count * 2];
1572 for (int i = 0; i < range.Length; i++)
1575 range[i] = (string) pl.Text;
1577 throw new Exception ("Invalid range: " + pl);
1581 domain.Defvar (new Xex.Variable.Str (domain, name, desc, (string) val, range));
1584 Xex.Term term = new Xex.Term ((string) val);
1586 vari.DefaultValue = term;
1591 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1592 MPlist pl, Xex.Variable vari)
1595 Xex.Symbol sym = val.Name;
1601 range = new Xex.Symbol[pl.Count * 2];
1602 for (int i = 0; i < range.Length; i++)
1605 range[i] = pl.Symbol.Name;
1607 throw new Exception ("Invalid range: " + pl);
1611 domain.Defvar (new Xex.Variable.Sym (domain, name, desc, sym, range));
1614 Xex.Term term = new Xex.Term (sym);
1616 vari.DefaultValue = term;
1621 private Xex.Variable get_global_var (Xex.Symbol name)
1623 if (im_global == null || this != im_global)
1625 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1627 im_global = im_table[tag];
1628 if (! im_global.Open ())
1629 throw new Exception ("Failed to load global");
1631 return im_global.domain.GetVar (name, false);
1634 private void parse_variables (MPlist plist)
1636 var_names = new Xex.Symbol[plist.Count];
1638 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1640 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1641 throw new Exception ("Invalid variable: " + plist);
1643 MPlist p = plist.Plist;
1644 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1645 var_names[i] = name;
1647 string desc = (string) parse_description (p);
1648 Xex.Variable vari = get_global_var (name);
1650 domain.Defvar (vari);
1656 new_variable (name, desc, p.Integer, p.next, vari);
1658 new_variable (name, desc, p.Text, p.next, vari);
1659 else if (p.IsSymbol)
1660 new_variable (name, desc, p.Symbol, p.next, vari);
1662 throw new Exception ("Invalid variable type: " + p.val);
1667 private void parse_variables (XmlNode node)
1669 XmlNodeList node_list = node.ChildNodes;
1671 var_names = new Xex.Symbol[node_list.Count];
1672 for (int i = 0; i < node_list.Count; i++)
1674 Xex.Symbol name = node_list[i].Attributes[0].Value;
1675 Xex.Variable vari = get_global_var (name);
1677 domain.Defvar (vari);
1678 domain.Defvar (node_list[i]);
1679 var_names[i] = name;
1683 private void parse_commands (MPlist plist)
1685 commands = new Command[plist.Count];
1687 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1688 if (plist.IsPlist && plist.Plist.IsSymbol)
1689 commands[i++] = new Command (plist.Plist);
1692 private void parse_commands (XmlNode node)
1694 XmlNodeList node_list = node.ChildNodes;
1696 commands = new Command[node_list.Count];
1697 for (int i = 0; i < node_list.Count; i++)
1699 if (node_list[i].NodeType == XmlNodeType.Element)
1700 commands[i] = new Command (node_list[i]);
1704 private void parse_plugins (MPlist plist)
1706 plugins = new Dictionary<MSymbol, Plugin> ();
1708 for (; ! plist.IsEmpty; plist = plist.Next)
1710 MPlist p = plist.Plist;
1711 MSymbol sym = p.Symbol;
1712 Plugin plugin = new Plugin (sym.Name);
1714 for (p = p.next; ! p.IsEmpty; p = p.next)
1716 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1717 domain.Defun (func);
1722 private void parse_plugins (XmlNode node)
1724 plugins = new Dictionary<MSymbol, Plugin> ();
1726 foreach (XmlNode n in node.ChildNodes)
1728 Plugin plugin = new Plugin (n.Attributes[0].Value);
1729 foreach (XmlNode nn in n.ChildNodes)
1731 Xex.Function func = new PluginMethod (plugin,
1732 nn.Attributes[0].Value);
1733 domain.Defun (func);
1738 private void parse_macros (XmlNode node)
1740 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1741 if (nn.NodeType == XmlNodeType.Element)
1742 domain.Defun (nn, true);
1743 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1744 if (nn.NodeType == XmlNodeType.Element)
1745 domain.Defun (nn, false);
1748 private void parse_maps (XmlNode node)
1750 for (node = node.FirstChild; node != null; node = node.NextSibling)
1751 if (node.Name == Qmap)
1753 MSymbol name = node.Attributes[0].Value;
1754 Map map = new Map (name);
1756 for (XmlNode nd = node.FirstChild; nd != null; nd = nd.NextSibling)
1757 if (nd.Name == Qrule)
1759 XmlNode n = nd.FirstChild;
1760 if (n.Name != Qkeyseq)
1762 KeySeq keyseq = (KeySeq) KeySeq.parser (domain, n);
1763 Xex.Term[] actions = Xex.ParseTerms (domain, n.NextSibling);
1764 map.entries.Add (new Map.Entry (domain, keyseq, actions));
1769 private void parse_states (MPlist plist)
1771 for (; ! plist.IsEmpty; plist = plist.next)
1774 State state = new State (this, plist.Plist);
1775 states[state.name] = state;
1776 if (initial_state == null)
1777 initial_state = state;
1781 private void parse_states (XmlNode node)
1783 for (node = node.FirstChild; node != null; node = node.NextSibling)
1784 if (node.Name == Qstate)
1786 State state = new State (this, node);
1787 states[state.name] = state;
1788 if (initial_state == null)
1789 initial_state = state;
1793 private void parse_include (MPlist plist)
1795 if (! plist.IsPlist)
1797 MPlist p = plist.Plist;
1798 MSymbol language, name, subname;
1799 language = p.Symbol;
1802 name = subname = MSymbol.nil;
1808 subname = MSymbol.nil;
1813 MInputMethod im = MInputMethod.Find (language, name, subname);
1819 if (! plist.IsSymbol)
1821 MSymbol target_type = plist.Symbol;
1823 MSymbol target_name = MSymbol.nil;
1825 target_name = plist.Symbol;
1826 if (target_type == Mmacro)
1828 if (target_name == MSymbol.nil)
1829 im.domain.CopyFunc (domain);
1831 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1833 else if (target_type == Mmap)
1835 if (target_name == MSymbol.nil)
1837 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1838 maps[kv.Key] = kv.Value;
1843 if (im.maps.TryGetValue (target_name, out map))
1844 maps[target_name] = map;
1847 else if (target_type == Mstate)
1849 if (target_name == MSymbol.nil)
1851 foreach (KeyValuePair<Xex.Symbol, State> kv in im.states)
1852 states[kv.Key] = kv.Value;
1856 Xex.Symbol state_name = target_name.Name;
1858 if (im.states.TryGetValue (state_name, out state))
1859 states[state_name] = state;
1864 private Xex.Term parse_cond (MPlist plist)
1866 Xex.Term[] args = new Xex.Term[plist.Count];
1868 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1870 if (! plist.IsPlist)
1871 throw new Exception ("Invalid cond args: " + plist);
1872 MPlist p = plist.Plist;
1873 List<Xex.Term> arg = new List<Xex.Term> ();
1874 arg.Add (parse_action (p, true));
1875 for (p = p.next; ! p.IsEmpty; p = p.next)
1876 arg.Add (parse_action (p, false));
1877 args[i] = new Xex.Term (arg);
1879 return new Xex.Term (domain, Qcond, args);
1882 private Xex.Term parse_insert (MPlist plist)
1887 arg = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1888 else if (plist.IsMText)
1889 arg = new Xex.Term ((string) plist.Text);
1890 else if (plist.IsInteger)
1891 arg = new Xex.Term (plist.Integer);
1892 else if (plist.IsPlist)
1894 MPlist pl = plist.Plist;
1896 args = new Xex.Term[pl.Count];
1898 for (i = 0; ! pl.IsEmpty; i++, pl = pl.next)
1901 args[i] = new Xex.Term ((string) pl.Text);
1902 else if (pl.IsPlist)
1904 List<Xex.Term> list = new List<Xex.Term> ();
1905 for (MPlist p = pl.Plist; ! p.IsEmpty; p = p.next)
1908 list.Add (new Xex.Term ((string) p.Text));
1910 throw new Exception ("Invalid candidates: " + p);
1914 throw new Exception ("Invalid candidates: " + pl);
1916 return new Xex.Term (domain, Qinsert_candidates, args);
1919 throw new Exception ("Invalid arg to insert: " + plist);
1920 args = new Xex.Term[1];
1922 return new Xex.Term (domain, Qinsert, args);
1925 private Xex.Term parse_select (MPlist plist)
1927 Xex.Term[] args = new Xex.Term[1];
1928 if (plist.IsInteger)
1929 args[0] = new Xex.Term (plist.Integer);
1930 else if (! plist.IsSymbol)
1931 throw new Exception ("Invalid arg to select: " + plist);
1932 else if (plist.Symbol.Name[0] == '@')
1933 args[0] = new Xex.Term (Selector.Get (plist.Symbol));
1935 args[0] = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1936 return new Xex.Term (domain, Qselect, args);
1939 private Xex.Term parse_funcall_with_marker (MPlist plist, Xex.Symbol func)
1941 Xex.Term[] args = new Xex.Term[1];
1942 if (plist.IsInteger && func != Qmark)
1943 args[0] = new Xex.Term (plist.Integer);
1944 else if (plist.IsSymbol)
1945 args[0] = new Xex.Term (Marker.Get (plist.Symbol));
1947 throw new Exception ("Invalid arg to " + func + ": " + plist);
1948 return new Xex.Term (domain, func, args);
1951 private Xex.Term parse_char_at (MSymbol name)
1953 Xex.Term[] args = new Xex.Term[1];
1954 args[0] = new Xex.Term (Marker.Get (name));
1955 return new Xex.Term (domain, Qchar_at, args);
1958 private Xex.Term parse_shift (MPlist plist)
1960 Xex.Term[] args = new Xex.Term[1];
1961 if (! plist.IsSymbol)
1962 throw new Exception ("Invalid arg to shift: " + plist);
1963 args[0] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1964 return new Xex.Term (domain, Qshift, args);
1967 private Xex.Term parse_action (MPlist plist, bool as_funarg)
1971 MPlist p = plist.Plist;
1973 if (p.IsMText || p.IsPlist)
1974 return parse_insert (plist);
1976 throw new Exception ("Invalid action: " + p);
1977 MSymbol sym = p.Symbol;
1978 Xex.Symbol name = sym.Name;
1981 return parse_cond (p);
1982 if (name == Qinsert)
1983 return parse_insert (p);
1984 if (name == Qselect)
1985 return parse_select (p);
1986 if (name == Qdelete || name == Qmove || name == Qmark)
1987 return parse_funcall_with_marker (p, name);
1989 return parse_shift (p);
1990 if (((string) name)[0] == '@')
1991 return parse_char_at (sym);
1992 if (name == Qset || name == Qadd || name == Qsub
1993 || name == Qmul || name == Qdiv)
1996 throw new Exception ("Invalid action: " + p);
1997 Xex.Symbol varname = p.Symbol.Name;
1998 Xex.Term[] args = new Xex.Term[1];
1999 args[0] = parse_action (p.next, true);
2000 return new Xex.Term (domain, name, varname, args);
2007 return new Xex.Term (domain, name, null);
2009 return new Xex.Term (domain, name, parse_actions (p, true));
2012 else if (plist.IsSymbol)
2014 if (plist.Symbol.Name[0] == '@')
2015 return parse_char_at (plist.Symbol);
2016 return new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
2018 else if (plist.IsMText)
2019 return (as_funarg ? new Xex.Term ((string) plist.Text)
2020 : parse_insert (plist));
2021 else if (plist.IsInteger)
2022 return (as_funarg ? new Xex.Term (plist.Integer)
2023 : parse_insert (plist));
2025 throw new Exception ("Invalid action: " + plist);
2028 private Xex.Term[] parse_actions (MPlist plist, bool as_funarg)
2030 Xex.Term[] terms = new Xex.Term[plist.Count];
2032 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
2033 terms[i] = parse_action (plist, as_funarg);
2037 private void parse_macros (MPlist plist)
2039 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2042 MPlist p = pl.Plist;
2046 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
2048 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2051 MPlist p = pl.Plist;
2055 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
2056 parse_actions (p.next, false), false);
2060 private void parse_maps (MPlist plist)
2062 for (; ! plist.IsEmpty; plist = plist.next)
2065 MPlist pl = plist.Plist;
2069 Map map = new Map (pl.Symbol);
2070 maps[pl.Symbol] = map;
2071 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
2075 MPlist p = pl.Plist;
2078 keys = new KeySeq (p.Text);
2080 keys = new KeySeq (p.Plist);
2085 = p.IsEmpty ? null : parse_actions (p, false);
2086 map.entries.Add (new Map.Entry (domain, keys, actions));
2091 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
2095 ((Context) domain.context).insert (args[0].Intval, null);
2097 ((Context) domain.context).insert ((MText) args[0].Strval, null);
2101 private static Xex.Term Finsert_candidates (Xex.Domain domain,
2105 Context ic = (Context) domain.context;
2106 Xex.Variable v = ic.domain.GetVar (Qcandidates_group_size, false);
2107 int column = (v == null ? 0 : v.Value.Intval);
2108 Candidates candidates = new Candidates (args, column);
2109 object candidate = candidates.Current;
2111 if (candidate is MText)
2112 ic.insert ((MText) candidate, candidates);
2114 ic.insert ((int) candidate, candidates);
2118 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
2121 Context ic = (Context) domain.context;
2122 Marker m = (Marker) args[0].Objval;
2124 return new Xex.Term (m.CharAt (ic));
2127 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
2130 Context ic = (Context) domain.context;
2134 pos = args[0].Intval;
2137 Marker m = (Marker) args[0].Objval;
2138 pos = m.Position (ic);
2140 return new Xex.Term (ic.delete (pos));
2143 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
2146 Context ic = (Context) domain.context;
2147 Candidates can = ic.candidates;
2151 object candidate = can.Current;
2153 if (candidate is MText)
2154 ic.delete (ic.cursor_pos - ((MText) candidate).Length);
2156 ic.delete (ic.cursor_pos - 1);
2158 candidate = can.Select (args[0].Intval);
2160 candidate = ((Selector) args[0].Objval).Select (can);
2161 if (candidate is MText)
2162 ic.insert ((MTtext) candidate, can);
2164 ic.insert ((int) candidate, can);
2169 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
2172 ((Context) domain.context).show ();
2176 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
2179 ((Context) domain.context).hide ();
2183 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
2186 Context ic = (Context) domain.context;
2187 int pos = (args[0].IsInt ? args[0].Intval
2188 : ((Marker) args[0].Objval).Position (ic));
2193 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
2196 Marker m = (Marker) args[0].Objval;
2197 m.Mark ((Context) domain.context);
2201 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
2204 Context ic = (Context) domain.context;
2207 ic.pushback (args[0].Intval);
2208 else if (args[0].IsStr)
2209 ic.pushback (new KeySeq (args[0].Strval));
2211 ic.pushback ((KeySeq) args[0].Objval);
2215 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
2218 ((Context) domain.context).pop ();
2222 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
2225 int n = args.Length == 0 ? -2 : args[0].Intval;
2226 ((Context) domain.context).undo (n);
2230 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
2233 ((Context) domain.context).commit ();
2237 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
2240 ((Context) domain.context).commit ();
2241 args = new Xex.Term[2];
2242 args[0] = args[1] = Tcatch_tag;
2243 return Xex.Fthrow (domain, vari, args);
2246 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
2249 Context ic = (Context) domain.context;
2251 if (ic.im.states.TryGetValue (args[0].Symval, out state))
2252 ((Context) domain.context).shift (state);
2254 throw new Exception ("Unknown state: " + args[0].Symval);
2258 private static Xex.Term Fshiftback (Xex.Domain domain, Xex.Variable vari,
2261 ((Context) domain.context).shift (null);
2265 private static Xex.Term Fkey_count (Xex.Domain domain, Xex.Variable vari,
2268 return new Xex.Term (((Context) domain.context).key_head);
2271 private static Xex.Term Fsurrounding_flag (Xex.Domain domain,
2275 return new Xex.Term (GetSurroundingText == null ? 0 : 1);
2278 public override string ToString ()
2281 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
2282 if (commands != null)
2284 str += " (commands";
2285 foreach (Command cmd in commands)
2289 if (var_names != null)
2291 str += " (variables";
2292 foreach (Xex.Symbol var in var_names)
2296 if (plugins != null)
2299 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
2300 str += " " + kv.Value;
2304 foreach (KeyValuePair<MSymbol, Map> kv in maps)
2305 str += " " + kv.Value;
2307 foreach (KeyValuePair<Xex.Symbol, State> kv in states)
2309 str += " (" + kv.Key + " " + kv.Value.keymap + ")";
2314 public class Context
2316 internal MInputMethod im;
2317 internal Xex.Domain domain;
2318 private bool active;
2320 private MText status;
2321 private MText produced = new MText ();
2322 internal MText preedit = new MText ();
2323 internal int cursor_pos;
2324 internal MPlist marker_positions = new MPlist ();
2326 internal Candidates candidates;
2327 private bool candidate_show;
2328 public bool CandidateShow { get { return candidate_show; } }
2330 private State state, prev_state;
2331 private MText state_preedit = new MText ();
2332 private int state_key_head;
2333 private object state_var_values, state_initial_var_values;
2334 private int state_pos;
2336 private Keymap keymap;
2337 // Sequence of input keys.
2338 internal KeySeq keys = new KeySeq ();
2339 // Index into KEYS specifying the next key to handle.
2340 internal int key_head;
2342 internal MText preceding_text = new MText ();
2343 internal MText following_text = new MText ();
2345 // Set to false before calling the method 'handle_key', and set
2346 // to true when some key is unhandled.
2347 private bool key_unhandled;
2349 // The unhandled key. It has the meaning only when
2350 // 'key_unhandled' is true.
2351 private Key unhandled_key;
2353 internal ChangedStatus changed;
2355 private void set_cursor (string prefix, int pos)
2359 candidates = (Candidates) preedit.GetProp (cursor_pos - 1,
2365 internal void reset ()
2367 status = im.initial_state.title;
2371 set_cursor ("reset", 0);
2372 marker_positions.Clear ();
2374 candidate_show = false;
2376 state = prev_state = null;
2377 state_preedit.Del ();
2378 state_var_values = state_initial_var_values;
2380 shift (im.initial_state);
2382 preceding_text.Del ();
2383 following_text.Del ();
2385 changed = ChangedStatus.None;
2388 static Xex.Term[] catch_args = new Xex.Term[2];
2390 private bool take_actions (Xex.Term[] actions)
2392 catch_args[0] = Tcatch_tag;
2393 catch_args[1]= new Xex.Term (domain, Qprogn, actions);
2394 Xex.Term term = new Xex.Term (domain, Qcatch, catch_args);
2395 term = term.Eval (domain);
2396 return (! term.IsSymbol || term.Symval != Tcatch_tag.Symval);
2399 static MPlist callback_arg = new MPlist ();
2401 private bool get_surrounding_text (int len)
2403 if (len < 0 ? -len <= preceding_text.Length
2404 : len <= following_text.Length)
2406 if (GetSurroundingText == null)
2408 callback_arg.Set (MSymbol.integer, len);
2409 if (! GetSurroundingText (this, callback_arg)
2410 || ! callback_arg.IsMText)
2414 preceding_text = callback_arg.Text;
2415 return (-len <= preceding_text.Length);
2417 following_text = callback_arg.Text;
2418 return (len <= following_text.Length);
2421 internal int GetSurroundingChar (int pos)
2423 if (! get_surrounding_text (pos < 0 ? pos : pos + 1))
2426 return preceding_text[preceding_text.Length + pos];
2427 return following_text[pos];
2430 private void adjust_markers (int from, int to, int inserted)
2432 int diff = inserted - (to - from);
2434 for (MPlist p = marker_positions; ! p.IsEmpty; p = p.next)
2436 int pos = p.Integer;
2438 p.Set (p.Key, pos >= to ? pos + diff : from);
2440 if (cursor_pos >= to)
2441 set_cursor ("adjust", cursor_pos + diff);
2442 else if (cursor_pos > from)
2443 set_cursor ("adjust", from);
2446 private void preedit_replace (int from, int to, int c,
2447 Candidates candidates)
2449 preedit.Del (from, to);
2450 preedit.Ins (from, c);
2451 if (candidates != null)
2453 preedit.PushProp (cursor_pos - 1, cursor_pos,
2454 Mcandidates, candidates);
2455 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2458 adjust_markers (from, to, 1);
2461 private void preedit_replace (int from, int to, MText mt,
2462 Candidates candidates)
2464 preedit[from, to] = mt;
2465 if (candidates != null)
2467 preedit.PushProp (cursor_pos - mt.Length, cursor_pos,
2468 Mcandidates, candidates);
2469 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2472 adjust_markers (from, to, mt.Length);
2475 internal void insert (int c, Candidates candidates)
2477 preedit_replace (cursor_pos, cursor_pos, c, candidates);
2478 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2481 internal void insert (MText mt, Candidates candidates)
2483 preedit_replace (cursor_pos, cursor_pos, mt, candidates);
2484 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2487 internal int delete (int pos)
2489 int deleted = pos - cursor_pos;
2491 if (pos < cursor_pos)
2495 if (DeleteSurroundingText != null)
2497 callback_arg.Set (MSymbol.integer, pos);
2498 if (DeleteSurroundingText (this, callback_arg))
2500 if (callback_arg.IsInteger)
2501 deleted = callback_arg.Integer - cursor_pos;
2502 preceding_text.Del ();
2505 deleted = - cursor_pos;
2509 if (pos < cursor_pos)
2510 preedit_replace (pos, cursor_pos, null, null);
2514 if (pos > preedit.Length)
2516 if (DeleteSurroundingText != null)
2518 callback_arg.Set (MSymbol.integer, pos - preedit.Length);
2519 if (DeleteSurroundingText (this, callback_arg))
2521 if (callback_arg.IsInteger)
2522 deleted = callback_arg.Integer - cursor_pos;
2523 preceding_text.Del ();
2526 deleted = preedit.Length - cursor_pos;
2528 pos = preedit.Length;
2530 if (pos > cursor_pos)
2531 preedit_replace (cursor_pos, pos, null, null);
2534 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2538 internal void show ()
2540 candidate_show = true;
2541 changed |= ChangedStatus.CandidateShow;
2544 internal void hide ()
2546 candidate_show = false;
2547 changed |= ChangedStatus.CandidateShow;
2550 internal void move (int pos)
2554 else if (pos > preedit.Length)
2555 pos = preedit.Length;
2556 if (pos != cursor_pos)
2558 set_cursor ("move", pos);
2559 changed |= ChangedStatus.Preedit;
2563 internal void pushback (int n)
2576 if (key_head > keys.keyseq.Count)
2577 key_head = keys.keyseq.Count;
2581 internal void pushback (KeySeq keyseq)
2585 if (key_head < keys.keyseq.Count)
2586 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2587 for (int i = 0; i < keyseq.keyseq.Count; i++)
2588 keys.keyseq.Add (keyseq.keyseq[i]);
2591 internal void pop ()
2593 if (key_head < keys.keyseq.Count)
2594 keys.keyseq.RemoveRange (key_head, 1);
2597 internal void undo (int n)
2600 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2602 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2606 internal void commit ()
2608 produced.Cat (preedit);
2609 preedit_replace (0, preedit.Length, null, null);
2610 changed |= ChangedStatus.Preedit;
2613 internal void shift (State state)
2617 if (prev_state == null)
2622 if (state == im.initial_state)
2625 keys.keyseq.RemoveRange (0, key_head);
2627 if (state != this.state)
2629 domain.RestoreValues (state_initial_var_values);
2630 if (state.enter_actions != null)
2631 take_actions (state.enter_actions);
2637 if (state != this.state && state.enter_actions != null)
2638 take_actions (state.enter_actions);
2639 prev_state = this.state;
2642 if (this.state == null || this.state.title != state.title)
2643 this.changed |= ChangedStatus.StateTitle;
2645 keymap = state.keymap;
2648 public Context (MInputMethod im)
2650 if (im.load_status != LoadStatus.Full
2652 throw new Exception ("Openging " + im.tag + " failed");
2654 domain = new Xex.Domain ("context", im.domain, this);
2655 state_initial_var_values = domain.SaveValues ();
2658 if (PreeditChanged != null)
2660 callback_arg.Set (MSymbol.mtext, preedit);
2661 PreeditChanged (this, callback_arg);
2663 if (StatusChanged != null)
2665 callback_arg.Set (MSymbol.mtext, status);
2666 StatusChanged (this, callback_arg);
2670 public ChangedStatus Changed { get { return changed; } }
2672 internal object GetCandidates (out int column)
2675 if (cursor_pos == 0)
2677 Candidates candidates
2678 = (Candidates) preedit.GetProp (cursor_pos - 1, Mcandidates);
2679 if (candidates == null)
2681 column = candidates.Column;
2682 return candidates.Current;
2685 private void save_state ()
2687 state_var_values = domain.SaveValues ();
2688 state_preedit.Del ();
2689 state_preedit.Ins (0, preedit);
2690 state_key_head = key_head;
2691 state_pos = cursor_pos;
2694 private void restore_state ()
2696 domain.RestoreValues (state_var_values);
2698 preedit.Ins (0, state_preedit);
2699 set_cursor ("restore", state_pos);
2702 private bool handle_key ()
2704 Console.Write ("\nHandle ({0}[{1}]) in {2}",
2705 keys, key_head, state.name);
2707 Keymap sub = keymap.Lookup (keys, ref key_head);
2713 if (keymap.map_actions != null)
2715 if (! take_actions (keymap.map_actions))
2718 else if (keymap.submaps != null)
2720 for (int i = state_key_head; i < key_head; i++)
2721 preedit_replace (cursor_pos, cursor_pos,
2722 keys.keyseq[i].ToChar (), null);
2724 if (keymap.submaps == null)
2726 if (keymap.branch_actions != null)
2728 if (! take_actions (keymap.branch_actions))
2731 if (keymap != state.keymap)
2737 State current_state = state;
2739 if (keymap.branch_actions != null)
2741 if (! take_actions (keymap.branch_actions))
2744 if (state == current_state)
2746 if (state == im.initial_state
2747 && key_head < keys.keyseq.Count)
2749 if (keymap != state.keymap)
2751 else if (keymap.branch_actions == null)
2752 shift (im.initial_state);
2758 public bool Toggle ()
2764 public bool UnhandledKey (out Key key)
2766 key = unhandled_key;
2767 return key_unhandled;
2770 public MText Preedit { get { return preedit; } }
2771 public MText Produced { get { return produced; } }
2774 // true: All keys are handled and there's no text to commit.
2775 // false: Some key is left unhandled or there's a text to
2776 // commit. The caller should refer to UnhandledKey and
2779 public bool Filter (Key key)
2783 key_unhandled = true;
2784 unhandled_key = key;
2787 if (key == Key.Reload)
2789 changed = ChangedStatus.None;
2791 preceding_text.Del ();
2792 following_text.Del ();
2794 key_unhandled = false;
2795 keys.keyseq.Add (key);
2797 while (key_head < keys.keyseq.Count)
2799 if (! handle_key ())
2801 unhandled_key = keys.keyseq[key_head++];
2802 key_unhandled = true;
2808 keys.keyseq.RemoveRange (0, key_head);
2811 if ((changed & ChangedStatus.Preedit) != ChangedStatus.None
2812 && PreeditChanged != null)
2814 callback_arg.Set (MSymbol.mtext, preedit);
2815 PreeditChanged (this, callback_arg);
2817 if ((changed & ChangedStatus.StateTitle) != ChangedStatus.None
2818 && StatusChanged != null)
2820 callback_arg.Set (MSymbol.mtext, status);
2821 StatusChanged (this, callback_arg);
2823 if ((changed & ChangedStatus.Candidate) != ChangedStatus.None
2824 && CandidateChanged != null)
2826 CandidateChanged (this, callback_arg);
2829 Console.Write ("\nPreedit(\"{0}\"/{1}), Produced({2})",
2830 preedit, cursor_pos, produced);
2832 return (! key_unhandled && produced.Length == 0);