2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
12 using Xex = System.Xml.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (Context ic, MPlist args);
21 internal static Xex.Domain im_domain
22 = new Xex.Domain ("input-method", null);
23 private static MSymbol Minput_method = "input-method";
24 private static MSymbol Mdescription = "description";
25 private static MSymbol Mvariable = "variable";
26 private static MSymbol Mcommand = "command";
27 private static MSymbol Mmodule = "module";
28 private static MSymbol Mtitle = "title";
29 private static MSymbol Minclude = "include";
30 private static MSymbol Mmacro = "macro";
31 private static MSymbol Mmap = "map";
32 private static MSymbol Mmap_list = "map-list";
33 private static MSymbol Mstate = "state";
34 internal static MSymbol Mcandidates = "candidates";
35 private static MSymbol Mat_minus_zero = "@-0";
36 private static MSymbol Matat = "@@";
38 private static Xex.Symbol Qxi_include = "xi:include";
39 private static Xex.Symbol Qmap = "map";
40 private static Xex.Symbol Qrule = "rule";
41 private static Xex.Symbol Qkeyseq = "keyseq";
42 private static Xex.Symbol Qprogn = "progn";
43 private static Xex.Symbol Qcatch = "catch";
44 private static Xex.Symbol Qinsert = "insert";
45 private static Xex.Symbol Qinsert_candidates = "insert-candidates";
46 private static Xex.Symbol Qchar_at = "char-at";
47 private static Xex.Symbol Qselect = "select";
48 private static Xex.Symbol Qdelete = "delete";
49 private static Xex.Symbol Qshift = "shift";
50 private static Xex.Symbol Qmove = "move";
51 private static Xex.Symbol Qmark = "mark";
52 private static Xex.Symbol Qset = "set";
53 private static Xex.Symbol Qadd = "add";
54 private static Xex.Symbol Qsub = "sub";
55 private static Xex.Symbol Qmul = "mul";
56 private static Xex.Symbol Qdiv = "div";
57 private static Xex.Symbol Qcond = "cond";
58 private static Xex.Symbol Qsname = "sname";
59 private static Xex.Symbol Qmname = "mname";
60 private static Xex.Symbol Qstate_hook = "state-hook";
61 private static Xex.Symbol Qcatch_all_branch = "catch-all-branch";
62 private static Xex.Symbol Qbranch = "branch";
63 private static Xex.Symbol Qstate = "state";
64 private static Xex.Symbol Qtitle = "title";
65 private static Xex.Symbol Qeq = "=";
66 private static Xex.Symbol Qeqeq = "==";
67 private static Xex.Symbol Qhide = "hide";
68 private static Xex.Symbol Qhide_candidates = "hide-candidates";
69 private static Xex.Symbol Qshow = "show";
70 private static Xex.Symbol Qshow_candidates = "show-candidates";
71 private static Xex.Symbol Qkey_count = "key-count";
72 private static Xex.Symbol Qsurrounding_text_flag = "surrounding-text-flag";
73 private static Xex.Symbol Qcandidates_group_size = "candidates-group-size";
75 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
76 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
78 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
79 = new Dictionary<MDatabase.Tag, MInputMethod> ();
81 internal static MInputMethod im_global = null;
84 protected enum LoadStatus
94 public enum ChangedStatus
100 CandidateList = 0x08,
101 CandidateIndex = 0x10,
102 CandidateShow = 0x20,
103 Preedit = PreeditText | CursorPos,
104 Candidate = CandidateList | CandidateIndex | CandidateShow,
107 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
108 | ChangedStatus.CandidateIndex
109 | ChangedStatus.CandidateShow);
112 public enum KeyModifier
115 Shift_L = 0x00400000,
116 Shift_R = 0x00800000,
118 Control_L = 0x01000000,
119 Control_R = 0x02000000,
120 Control = 0x03000000,
135 private static Dictionary<string, uint> keysyms
136 = new Dictionary<string, uint> ();
137 private static Dictionary<string, KeyModifier> keymodifiers
138 = new Dictionary<string, KeyModifier> ();
139 private static uint keysym_base = 0x200000;
140 private static uint char_mask = ~((uint) KeyModifier.All);
141 public static readonly Key Reload;
145 keysyms["null"] = 0x00;
146 keysyms["bs"] = keysyms["backspace"] = 0x08;
147 keysyms["tab"] = 0x09;
148 keysyms["lf"] = keysyms["linefeed"] = 0x10;
149 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
150 keysyms["esc"] = keysyms["escape"] = 0x1B;
151 keysyms["spc"] = keysyms["space"] = 0x20;
152 keysyms["del"] = keysyms["delete"] = 0x7F;
153 keymodifiers["shift-l"] = KeyModifier.Shift_L;
154 keymodifiers["shift-r"] = KeyModifier.Shift_R;
155 keymodifiers["shift"] = KeyModifier.Shift;
156 keymodifiers["control-l"] = KeyModifier.Control_L;
157 keymodifiers["control-r"] = KeyModifier.Control_R;
158 keymodifiers["control"] = KeyModifier.Control;
159 keymodifiers["alt-l"] = KeyModifier.Alt_L;
160 keymodifiers["alt-r"] = KeyModifier.Alt_R;
161 keymodifiers["alt"] = KeyModifier.Alt;
162 keymodifiers["altgr"] = KeyModifier.AltGr;
163 keymodifiers["super"] = KeyModifier.Super;
164 keymodifiers["hyper"] = KeyModifier.Hyper;
165 Reload = new Key ((MSymbol) "-reload");
168 private static uint decode_keysym (MSymbol keysym)
171 string name = keysym.Name;
173 if (name.Length == 1)
175 name = name.ToLower ();
176 if (! keysyms.TryGetValue (name, out key))
177 keysyms[name] = key = keysym_base++;
181 private static uint combine_modifiers (uint c, KeyModifier modifiers)
183 if (c < 0x7F && c != 0x20)
185 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
186 && Char.IsLower ((char) c))
188 modifiers &= ~KeyModifier.Shift;
189 c = Char.ToUpper ((char) c);
191 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
193 modifiers &= ~KeyModifier.Control;
197 return c | (uint) modifiers;
200 public Key (uint c) { key = c; }
201 public Key (int c) { key = (uint) c; }
203 public Key (uint c, KeyModifier modifiers)
205 key = combine_modifiers (c, modifiers);
208 public Key (MSymbol keysym, KeyModifier modifiers)
210 key = combine_modifiers (decode_keysym (keysym), modifiers);
213 public Key (MSymbol keysym)
215 string str = keysym.Name;
216 int len = str.Length;
218 KeyModifier modifiers = KeyModifier.None;
220 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
223 modifiers |= KeyModifier.Shift;
224 else if (str[i] == 'C')
225 modifiers |= KeyModifier.Control;
226 else if (str[i] == 'A')
227 modifiers |= KeyModifier.Alt;
228 else if (str[i] == 'G')
229 modifiers |= KeyModifier.AltGr;
230 else if (str[i] == 's')
231 modifiers |= KeyModifier.Super;
232 else if (str[i] == 'H')
233 modifiers |= KeyModifier.Hyper;
236 key = combine_modifiers (str[i], modifiers);
238 key = combine_modifiers (decode_keysym (keysym), modifiers);
241 public Key (MPlist plist)
243 KeyModifier modifiers = KeyModifier.None;
246 for (p = plist; ! p.IsEmpty; p = p.next)
250 if (! p.next.IsEmpty)
251 throw new Exception ("Invalid Key: " + plist);
254 else if (! p.IsSymbol)
255 throw new Exception ("Invalid Key: " + plist);
258 string name = p.Symbol.Name.ToLower ();
261 if (! keymodifiers.TryGetValue (name, out m))
266 if (p.IsEmpty || ! p.next.IsEmpty)
267 throw new Exception ("Invalid Key: " + plist);
269 key = combine_modifiers ((uint) p.Integer, modifiers);
271 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
274 public bool HasModifier
276 get { return ((key & (uint) KeyModifier.All) != 0); }
279 public static bool operator== (Key k1, Key k2)
281 return k1.key == k2.key;
284 public static bool operator!= (Key k1, Key k2)
286 return k1.key != k2.key;
289 public override bool Equals (object o) { return key == ((Key) o).key; }
291 public override int GetHashCode () { return (int) key; }
293 public bool Match (Key k)
297 if ((k.key & char_mask) != (key & char_mask))
299 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
300 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
301 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
302 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
303 && (m2 & KeyModifier.Shift) != KeyModifier.None))
304 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
305 || ((m1 & KeyModifier.Control) == KeyModifier.Control
306 && (m2 & KeyModifier.Control) != KeyModifier.None))
307 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
308 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
309 && (m2 & KeyModifier.Alt) != KeyModifier.None))
310 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
315 return (key & 0x3FFFFF) <= 0x1FFFFF ? (int) (key & 0x1FFFFF) : -1;
318 public override string ToString ()
323 foreach (KeyValuePair<string, uint> kv in keysyms)
324 if ((key & 0x3FFFFF) == kv.Value)
332 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
334 if (m != KeyModifier.None)
336 if ((m & KeyModifier.Shift) != KeyModifier.None)
338 if ((m & KeyModifier.Control) != KeyModifier.None)
340 if ((m & KeyModifier.Alt) != KeyModifier.None)
342 if ((m & KeyModifier.AltGr) != KeyModifier.None)
344 if ((m & KeyModifier.Super) != KeyModifier.None)
346 if ((m & KeyModifier.Hyper) != KeyModifier.None)
353 internal class KeySeq : Xex.TermValue
355 public List<Key> keyseq = new List<Key> ();
357 public override Xex.TermValue Clone ()
359 KeySeq ks = new KeySeq ();
360 ks.keyseq.InsertRange (0, keyseq);
366 public KeySeq (MPlist plist)
368 foreach (MPlist p in plist)
371 keyseq.Add (new Key (p.Symbol));
372 else if (p.IsInteger)
373 keyseq.Add (new Key ((char) p.Integer));
375 keyseq.Add (new Key (p.Plist));
377 throw new Exception ("Invalid Key Sequence: " + plist);
381 public KeySeq (MText mt) : base ()
383 for (int i = 0; i < mt.Length; i++)
384 keyseq.Add (new Key ((uint) mt[i]));
387 public KeySeq (List<Xex.Term> list)
389 int len = list.Count;
391 for (int i = 0; i < len; i++)
394 keyseq.Add (new Key (list[i].Intval));
395 else if (list[i].IsStr)
396 keyseq.Add (new Key (list[i].Strval));
397 else if (list[i].IsSymbol)
398 keyseq.Add (new Key ((string) list[i].Symval));
400 throw new Exception ("Invalid key: " + list[i]);
404 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
406 Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain);
407 return (term.IsStr ? new KeySeq ((MText) term.Strval)
408 : new KeySeq (term.Listval));
411 public override string ToString ()
414 foreach (Key key in keyseq)
415 if (key.HasModifier || key.ToChar () < 0x20)
418 foreach (Key k in keyseq)
422 mt.Cat (k.ToString ());
424 return (string) mt.Cat (")");
427 foreach (Key k in keyseq)
431 if (c == '\\' || c == '"')
435 return (string) mt.Cat ("\"");
442 public MText description;
443 internal List<KeySeq> keys;
445 public Command (MPlist p)
449 description = parse_description (p);
450 if (description == null)
451 description = "No description";
452 keys = new List<KeySeq> ();
453 for (p = p.next; ! p.IsEmpty; p = p.next)
456 keys.Add (new KeySeq (p.Text));
458 keys.Add (new KeySeq (p.Plist));
462 public Command (XmlNode node)
464 name = node.Attributes[0].Value;
465 keys = new List<KeySeq> ();
466 for (node = node.FirstChild; node != null; node = node.NextSibling)
468 if (node.Name == "description")
469 description = parse_description (node);
470 else if (node.Name == "keyseq")
471 keys.Add ((KeySeq) KeySeq.parser (null, node));
475 public override string ToString ()
477 string str = "(" + name + " \"" + (string) description;
478 foreach (KeySeq keyseq in keys)
484 internal class Plugin
487 private Assembly assembly;
488 private Type plugin_type;
490 public Plugin (string name)
495 public MethodInfo GetMethod (Xex.Symbol name)
497 if (assembly == null)
499 assembly = Assembly.LoadFrom (name + ".dll");
500 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
503 MethodInfo info = plugin_type.GetMethod ((string) name);
505 throw new Exception ("Invalid plugin method: " + name);
509 public override string ToString ()
511 return String.Format ("(module {0}", name);
515 internal class PluginMethod : Xex.Function
517 private Plugin plugin;
518 private MethodInfo method_info;
519 object[] parameters = new object[2];
521 public PluginMethod (Plugin plugin, string name)
522 : base ((Xex.Symbol) name, 0, -1)
524 this.plugin = plugin;
527 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
530 args = (Xex.Term[]) args.Clone ();
531 for (int i = 0; i < args.Length; i++)
533 args[i] = args[i].Eval (domain);
537 if (method_info == null)
538 method_info = plugin.GetMethod (name);
539 parameters[0] = domain.context;
540 parameters[1] = args;
541 return (Xex.Term) method_info.Invoke (null, parameters);
545 internal abstract class Marker : Xex.TermValue
547 private MSymbol name;
549 private Marker (MSymbol name)
554 public abstract int Position (Context ic);
556 public virtual void Mark (Context ic)
558 throw new Exception ("Can't set predefined marker: " + name);
560 public virtual int CharAt (Context ic)
562 int pos = Position (ic);
564 return ((pos >= 0 && pos < ic.preedit.Length) ? ic.preedit[pos]
568 public override string ToString ()
570 return "<marker>" + name.Name + "</marker>";
573 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
575 return Get ((MSymbol) node.InnerText);
578 public class Named : Marker
580 public Named (MSymbol name) : base (name) { }
582 public override int Position (Context ic)
584 MPlist p = ic.marker_positions.Find (name);
585 return (p == null ? 0 : p.Integer);
588 public override void Mark (Context ic)
590 ic.marker_positions.Put (name, ic.cursor_pos);
594 public class Predefined : Marker
598 public Predefined (char tag) : base ("@" + tag) { this.tag = tag; }
600 public override int Position (Context ic)
604 case '>': return ic.preedit.Length;
605 case '-': return ic.cursor_pos - 1;
606 case '+': return ic.cursor_pos + 1;
608 if (ic.cursor_pos > 0)
610 int pos = ic.cursor_pos;
612 ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to);
617 if (ic.cursor_pos < ic.preedit.Length - 1)
619 int pos = ic.cursor_pos;
621 ic.preedit.FindProp (Mcandidates, pos, out from, out pos);
624 return ic.preedit.Length;
631 public class PredefinedAbsolute : Marker
635 public PredefinedAbsolute (MSymbol name) : base (name)
637 if (! int.TryParse (((string) name).Substring (1), out pos))
638 throw new Exception ("Invalid marker name: " + name);
641 public override int Position (Context ic)
643 return (pos < ic.preedit.Length ? pos : ic.preedit.Length);
647 public class PredefinedSurround : Marker
649 private int distance;
651 public PredefinedSurround (MSymbol name) : base (name)
653 if (! int.TryParse (((string) name).Substring (1), out distance))
654 throw new Exception ("Invalid marker name: " + name);
659 public override int Position (Context ic)
661 return ic.cursor_pos + distance;
664 public override int CharAt (Context ic)
666 int pos = ic.cursor_pos + distance;
668 return ic.GetSurroundingChar (pos);
669 else if (pos >= ic.preedit.Length)
670 return ic.GetSurroundingChar (pos - ic.preedit.Length);
671 return ic.preedit[pos];
675 static internal Dictionary<MSymbol,Predefined> predefineds;
679 predefineds = new Dictionary<MSymbol, Predefined> ();
680 predefineds ["@<"] = predefineds["@first"] = new Predefined ('<');
681 predefineds ["@>"] = predefineds["@last"] = new Predefined ('>');
682 predefineds ["@-"] = predefineds["@previous"] = new Predefined ('-');
683 predefineds ["@+"] = predefineds["@next"] = new Predefined ('+');
684 predefineds ["@["] = predefineds["@previous-candidate-change"]
685 = new Predefined ('[');
686 predefineds ["@]"] = predefineds["@next-candidate-change"]
687 = new Predefined (']');
690 public static Marker Get (MSymbol name)
692 string str = name.Name;
696 if (predefineds.TryGetValue (name, out pred))
699 throw new Exception ("Invalid marker name: " + name);
700 if (Char.IsDigit (str[1]))
701 return new PredefinedAbsolute (name);
702 if (str.Length == 2 || name == Mat_minus_zero
703 || ! (str[1] == '-' || str[1] == '+'))
704 throw new Exception ("Invalid marker name: " + name);
705 return new PredefinedSurround (name);
707 return new Named (name);
711 internal class Candidates
718 public Block (int index, Xex.Term term)
722 Data = (MText) term.Strval;
725 MPlist plist = new MPlist ();
727 foreach (Xex.Term t in term.Listval)
728 p = p.Add (MSymbol.mtext, (MText) t.Strval);
733 public Block (int index, MPlist plist)
738 else if (plist.IsPlist)
741 throw new Exception ("Invalid candidate: " + plist);
746 get { return (Data is MText
747 ? ((MText) Data).Length
748 : ((MPlist) Data).Count); }
751 public object this[int i]
754 if (Data is MText) return ((MText) Data)[i];
755 return ((MPlist) Data)[i];
760 private Block[] blocks;
762 private int index = 0;
763 public object[] group;
765 private bool IsFixed { get { return group != null; } }
768 Block last = blocks[blocks.Length - 1];
769 return last.Index + last.Count; }
773 get { return (IsFixed ? index % group.Length
774 : index - blocks[row].Index); }
777 public object Group {
778 get { return (IsFixed ? group : blocks[row].Data); }
781 public int GroupLength
786 int nitems = group.Length;
787 int start = index - (index % nitems);
789 return (start + nitems <= total ? nitems : total - start);
791 return blocks[row].Count;
795 public object Current {
797 return (IsFixed ? group[index % group.Length]
798 : blocks[row][index - blocks[row].Index]);
802 public Candidates (MPlist list, int column)
804 int nblocks = list.Count;
806 blocks = new Block[nblocks];
807 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
808 start += (blocks[i] = new Block (index, list)).Count;
811 group = new object[column];
816 public Candidates (Xex.Term[] candidates, int column)
818 int nblocks = candidates.Length;
820 blocks = new Block[nblocks];
821 for (int i = 0, start = 0; i < nblocks; i++)
822 start += (blocks[i] = new Block (index, candidates[i])).Count;
825 group = new object[column];
830 public static void Detach (Context ic)
832 ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
833 ic.candidates = null;
834 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
838 // Fill the array "group" by candidates stating from START.
839 // START must be a multiple of "column". Return the number of
840 // valid candidates in "group".
842 private int fill_group (int start)
844 int nitems = group.Length;
849 while (start < b.Index)
852 while (start >= b.Index + b.Count)
858 for (int i = 0; i < nitems; i++, start++)
863 if (r == blocks.Length)
874 // Update "row" to what contains the first candidate of
875 // the previous candidate-group, update "current_index", and
876 // update "group" if necessary. Return the previous
877 // candidate-group. Set NITEMS to the number of valid
878 // candidates contained in that group.
880 public int PrevGroup ()
887 nitems = group.Length;
888 if ((index -= col + nitems) < 0)
889 index = (Total / nitems) * nitems;
890 nitems = fill_group (index);
894 row = row > 0 ? row-- : blocks.Length - 1;
895 nitems = blocks[row].Count;
896 index = blocks[row].Index;
898 index += col < nitems ? col : nitems - 1;
902 public int NextGroup ()
909 nitems = group.Length;
910 if ((index += nitems - col) >= Total)
912 nitems = fill_group (index);
916 row = row < blocks.Length - 1 ? row + 1 : 0;
917 nitems = blocks[row].Count;
918 index = blocks[row].Count;
920 index += col < nitems ? col : nitems - 1;
930 int nitems = PrevGroup ();
931 index += col < nitems - 1 ? col : nitems - 1;
940 int nitems = GroupLength;
942 if (col == nitems - 1)
944 nitems = NextGroup ();
958 index += GroupLength - (Column + 1);
961 public object Select (int col)
963 int maxcol = GroupLength - 1;
966 index = index - Column + col;
970 public object Select (Selector selector)
972 switch (selector.Tag)
974 case '<': First (); break;
975 case '>': Last (); break;
976 case '-': Prev (); break;
977 case '+': Next (); break;
978 case '[': PrevGroup (); break;
979 case ']': NextGroup (); break;
985 public override string ToString ()
987 return (String.Format ("<candidates row={0} col={1}>", row, index)
993 internal class Selector : Xex.TermValue
995 static new Dictionary<MSymbol, Selector> selectors;
999 selectors = new Dictionary<MSymbol, Selector> ();
1000 selectors ["@<"] = selectors["@first"] = new Selector ('<');
1001 selectors ["@="] = selectors["@current"] = new Selector ('=');
1002 selectors ["@>"] = selectors["@last"] = new Selector ('>');
1003 selectors ["@-"] = selectors["@previous"] = new Selector ('-');
1004 selectors ["@+"] = selectors["@next"] = new Selector ('+');
1005 selectors ["@["] = selectors["@previous-candidate-change"]
1006 = new Selector ('[');
1007 selectors ["@]"] = selectors["@next-candidate-change"]
1008 = new Selector (']');
1011 private readonly char tag;
1013 public char Tag { get { return tag; } }
1015 private Selector (char tag) { this.tag = tag; }
1017 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
1019 return Get ((MSymbol) node.InnerText);
1022 public static Xex.TermValue Get (MSymbol name)
1025 if (! selectors.TryGetValue (name, out selector))
1026 throw new Exception ("Invalid selector name: " + name);
1033 public MSymbol name;
1034 public List<Entry> entries = new List<Entry> ();
1036 public Map (MSymbol name) { this.name = name; }
1040 public KeySeq keyseq;
1041 public Xex.Term[] actions;
1043 public Entry (Xex.Domain domain, KeySeq keyseq, Xex.Term[] actions)
1045 this.keyseq = keyseq;
1046 this.actions = actions;
1050 public override string ToString ()
1052 string str = "(" + name;
1053 foreach (Entry e in entries)
1054 str += " " + e.keyseq.ToString ();
1059 internal class Keymap
1061 public Dictionary<Key, Keymap> submaps;
1062 public Xex.Term[] map_actions, branch_actions;
1064 public Keymap () { }
1066 public void Add (KeySeq keys, int index,
1067 Xex.Term[] map_actions, Xex.Term[] branch_actions)
1069 if (index == keys.keyseq.Count)
1071 this.map_actions = map_actions;
1072 this.branch_actions = branch_actions;
1076 Key key = keys.keyseq[index];
1079 if (submaps == null)
1080 submaps = new Dictionary<Key, Keymap> ();
1082 submaps.TryGetValue (key, out sub);
1084 submaps[key] = sub = new Keymap ();
1085 sub.Add (keys, index + 1, map_actions, branch_actions);
1089 public void AddMap (Map map, Xex.Term[] branch_actions)
1091 foreach (Map.Entry entry in map.entries)
1092 Add (entry.keyseq, 0, entry.actions, branch_actions);
1095 public Keymap Lookup (KeySeq keys, ref int index)
1099 if (index < keys.keyseq.Count
1101 && submaps.TryGetValue (keys.keyseq[index], out sub))
1104 return sub.Lookup (keys, ref index);
1109 private void describe (MText mt, KeySeq keyseq)
1111 if (map_actions != null || branch_actions != null)
1115 mt.Cat ('(').Cat (keyseq.ToString ());
1116 if (map_actions != null)
1117 foreach (Xex.Term term in map_actions)
1118 mt.Cat (' ').Cat (term.ToString ());
1119 if (branch_actions != null)
1120 foreach (Xex.Term term in branch_actions)
1121 mt.Cat (' ').Cat (term.ToString ());
1124 if (submaps != null)
1125 foreach (KeyValuePair<Key, Keymap> kv in submaps)
1127 keyseq.keyseq.Add (kv.Key);
1128 kv.Value.describe (mt, keyseq);
1129 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
1133 public override string ToString ()
1136 KeySeq keyseq = new KeySeq ();
1138 describe (mt, keyseq);
1143 internal class State
1145 public MSymbol name;
1147 public Xex.Term[] enter_actions, fallback_actions;
1148 public Keymap keymap = new Keymap ();
1150 public State (MSymbol name, MText title)
1156 public State (MInputMethod im, XmlNode node)
1158 this.name = node.Attributes[Qsname].Value;
1159 XmlAttribute attr = node.Attributes[Qtitle];
1161 title = (MText) attr.Value;
1164 keymap = new Keymap ();
1165 for (node = node.FirstChild; node != null; node = node.NextSibling)
1167 if (node.Name == Qstate_hook)
1168 enter_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1169 else if (node.Name == Qcatch_all_branch)
1170 fallback_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1171 else if (node.Name == Qbranch)
1173 MSymbol mapname = node.Attributes[Qmname].Value;
1175 if (im.maps.TryGetValue (mapname, out map))
1176 keymap.AddMap (map, Xex.ParseTerms (im.domain,
1179 throw new Exception ("Unknown map: " + mapname);
1184 public State (MInputMethod im, MPlist plist)
1186 if (! plist.IsSymbol)
1187 throw new Exception ("Invalid state: " + plist);
1188 this.name = plist.Symbol;
1197 keymap = new Keymap ();
1198 for (; ! plist.IsEmpty; plist = plist.next)
1200 if (! plist.IsPlist)
1201 throw new Exception ("Invalid branch: " + plist);
1202 MPlist p = plist.Plist;
1204 throw new Exception ("Invalid branch: " + p);
1205 MSymbol mapname = p.Symbol;
1206 if (mapname == MSymbol.t)
1207 enter_actions = im.parse_actions (p.next, false);
1208 else if (mapname == MSymbol.nil)
1209 fallback_actions = im.parse_actions (p.next, false);
1213 if (im.maps.TryGetValue (mapname, out map))
1214 keymap.AddMap (map, im.parse_actions (p.next, false));
1216 throw new Exception ("Unknown map: " + mapname);
1221 public override string ToString ()
1223 MText mt = "(" + name;
1226 mt.Cat (" \"" + title + "\"");
1227 mt.Cat (keymap.ToString ());
1228 return (string) mt + ")";
1233 internal Xex.Domain domain;
1235 protected LoadStatus load_status = LoadStatus.None;
1236 protected MDatabase.Tag tag;
1237 private MDatabase mdb;
1239 private MText description;
1240 internal MText title;
1241 internal Command[] commands;
1242 internal Xex.Symbol[] var_names;
1243 internal Dictionary<MSymbol, Plugin> plugins;
1244 internal Dictionary<MSymbol, Map> maps;
1245 internal MPlist states;
1247 static MInputMethod ()
1249 im_domain.DefTerm ("keyseq", KeySeq.parser);
1250 im_domain.DefTerm ("marker", Marker.parser);
1251 im_domain.DefTerm ("selector", Selector.parser);
1253 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1254 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, -1);
1255 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1256 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1257 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
1258 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
1259 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1260 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1261 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1262 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1263 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1264 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1265 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1266 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1267 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
1268 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1269 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
1270 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag",
1273 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
1274 List<MDatabase> list = MDatabase.List (tag);
1275 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
1276 foreach (MDatabase mdb in list)
1277 im_table[mdb.tag] = new MInputMethod (mdb.tag);
1281 private MInputMethod (MDatabase.Tag tag)
1284 domain = new Xex.Domain (tag[1].Name, im_domain, null);
1287 // Instance Properties
1288 public MSymbol Language { get { return tag[1]; } }
1289 public MSymbol Name { get { return tag[2]; } }
1290 public MSymbol SubName { get { return tag[3]; } }
1292 public bool Info (out MText description,
1294 out Xex.Variable[] variables,
1295 out Command[] commands)
1297 if ((load_status & LoadStatus.Header) != LoadStatus.Header
1298 && ! load_header ())
1306 description = this.description;
1308 if (var_names == null)
1312 variables = new Xex.Variable[var_names.Length];
1314 foreach (Xex.Symbol name in var_names)
1315 variables[i++] = domain.GetVar (name, false);
1317 commands = this.commands;
1321 public static MInputMethod Find (MSymbol language, MSymbol name)
1323 return Find (language, name, MSymbol.nil);
1326 public static MInputMethod Find (MSymbol language, MSymbol name,
1329 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
1333 return (im_table.TryGetValue (tag, out im) ? im : null);
1336 private bool Open ()
1338 return ((load_status == LoadStatus.Full) || load_body ());
1341 public static MInputMethod[] List ()
1343 MInputMethod[] array = new MInputMethod[im_table.Count];
1346 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
1347 array[i++] = kv.Value;
1351 private bool load_header ()
1353 mdb = MDatabase.Find (tag);
1357 if (mdb.Format == MSymbol.plist)
1358 load (mdb.Load (Mmap), false);
1361 XmlDocument doc = new XmlDocument (Xex.Symbol.NameTable);
1362 if (! mdb.Load (doc, Mmap_list))
1363 throw new Exception ("Load error" + mdb.tag);
1364 load (doc.DocumentElement, false);
1366 } catch (Exception e) {
1367 Console.WriteLine ("{0}\n", e);
1368 load_status = LoadStatus.Error;
1371 load_status |= LoadStatus.Header;
1375 private bool load_body ()
1377 mdb = MDatabase.Find (tag);
1381 if (mdb.Format == MSymbol.plist)
1382 load (mdb.Load (), true);
1385 XmlDocument doc = new XmlDocument (Xex.Symbol.NameTable);
1386 if (! mdb.Load (doc))
1387 throw new Exception ("Load error" + mdb.tag);
1388 load (doc.DocumentElement, true);
1390 } catch (Exception e) {
1391 Console.WriteLine (e);
1392 load_status = LoadStatus.Error;
1395 load_status = LoadStatus.Full;
1399 private void add_default_state ()
1401 MSymbol Qinit = "init";
1402 State state = new State (Qinit, title);
1403 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1404 state.keymap.AddMap (kv.Value, null);
1405 states.Add (Qinit, state);
1408 private void load (MPlist plist, bool full)
1410 maps = new Dictionary<MSymbol, Map> ();
1411 states = new MPlist ();
1413 for (; ! plist.IsEmpty; plist = plist.next)
1416 MPlist pl = plist.Plist;
1419 MSymbol sym = pl.Symbol;
1422 if (sym == Mdescription)
1423 description = parse_description (pl);
1424 else if (sym == Mtitle)
1429 else if (sym == Mvariable)
1430 parse_variables (pl);
1431 else if (sym == Mcommand)
1432 parse_commands (pl);
1437 else if (sym == Minclude)
1439 else if (sym == Mmacro)
1441 else if (sym == Mmap)
1443 else if (sym == Mstate)
1448 if (description == null)
1449 description = (MText) "No description";
1451 title = new MText (tag[2].Name);
1452 if (commands == null)
1453 commands = new Command[0];
1457 add_default_state ();
1460 private void load (XmlNode node, bool full)
1462 bool skip_header = load_status == LoadStatus.Header;
1464 maps = new Dictionary<MSymbol, Map> ();
1465 states = new MPlist ();
1467 if (node.NodeType == XmlNodeType.Document)
1468 node = node.FirstChild;
1469 while (node.NodeType != XmlNodeType.Element)
1470 node = node.NextSibling;
1471 for (node = node.FirstChild; node != null; node = node.NextSibling)
1473 if (node.NodeType != XmlNodeType.Element)
1477 if (node.Name == "description")
1478 description = parse_description (node);
1479 else if (node.Name == "title")
1480 title = parse_title (node);
1481 else if (node.Name == "variable-list")
1482 parse_variables (node);
1483 else if (node.Name == "command-list")
1484 parse_commands (node);
1488 if (node.Name == "module-list")
1489 parse_plugins (node);
1490 else if (node.Name == "macro-list")
1491 parse_macros (node);
1492 else if (node.Name == "map-list")
1494 else if (node.Name == "state-list")
1495 parse_states (node);
1498 if (description == null)
1499 description = (MText) "No description";
1501 title = new MText (tag[2].Name);
1502 if (commands == null)
1503 commands = new Command[0];
1507 add_default_state ();
1510 private static MText parse_description (MPlist plist)
1516 plist = plist.Plist;
1517 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1518 && plist.next.IsMText)
1519 return plist.next.Text;
1524 private static MText parse_description (XmlNode node)
1526 if (node.HasChildNodes)
1527 node = node.FirstChild;
1528 return node.InnerText;
1531 private static MText parse_title (XmlNode node)
1533 return node.InnerText;
1536 private Xex.Variable get_global_var (Xex.Symbol name)
1538 if (im_global == null || this != im_global)
1541 new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil, "global");
1542 im_global = im_table[tag];
1543 if (! im_global.Open ())
1544 throw new Exception ("Failed to load global");
1546 return im_global.domain.GetVar (name, false);
1549 private void parse_variables (MPlist plist)
1551 var_names = new Xex.Symbol[plist.Count];
1553 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1555 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1556 throw new Exception ("Invalid variable: " + plist);
1558 MPlist p = plist.Plist;
1559 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1560 var_names[i] = name;
1562 MText mt = parse_description (p);
1563 string desc = mt == null ? null : (string) mt;
1566 Xex.Variable vari = get_global_var (name);
1568 domain.Defvar (vari);
1577 int nrange = p.Count;
1578 range = new int[nrange * 2];
1579 for (int j = 0; j < nrange; j++)
1583 MPlist p0 = p.Plist;
1585 if (! p0.IsInteger || ! p0.next.IsInteger)
1586 throw new Exception ("Invalid range: " + p0);
1587 range[j * 2] = p0.Integer;
1588 range[j * 2 + 1] = p0.next.Integer;
1590 else if (p.IsInteger)
1591 range[j * 2] = range[j * 2 + 1] = p.Integer;
1593 throw new Exception ("Invalid range: " + p);
1596 domain.DefvarInt (name, n, desc, range);
1600 string str = (string) p.Text;
1601 string[] range = null;
1606 range = new string[p.Count];
1607 for (int j = 0; j < range.Length; j++)
1610 range[j] = (string) p.Text;
1612 throw new Exception ("Invalid range: " + p);
1615 domain.DefvarStr (name, str, desc, range);
1617 else if (p.IsSymbol)
1619 Xex.Symbol sym = p.Symbol.Name;
1627 range = new Xex.Symbol[p.Count];
1628 for (int j = 0; j < range.Length; j++)
1631 range[j] = p.Symbol.Name;
1633 throw new Exception ("Invalid range: " + p);
1636 domain.DefvarSym (name, sym, desc, range);
1638 else if (! p.IsEmpty)
1639 throw new Exception ("Invalid variable type: " + p.val);
1643 private void parse_variables (XmlNode node)
1645 XmlNodeList node_list = node.ChildNodes;
1647 var_names = new Xex.Symbol[node_list.Count];
1648 for (int i = 0; i < node_list.Count; i++)
1650 Xex.Symbol name = node_list[i].Attributes[0].Value;
1651 Xex.Variable vari = get_global_var (name);
1653 domain.Defvar (vari);
1654 domain.Defvar (node_list[i]);
1655 var_names[i] = name;
1659 private void parse_commands (MPlist plist)
1661 commands = new Command[plist.Count];
1663 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1664 if (plist.IsPlist && plist.Plist.IsSymbol)
1665 commands[i++] = new Command (plist.Plist);
1668 private void parse_commands (XmlNode node)
1670 XmlNodeList node_list = node.ChildNodes;
1672 commands = new Command[node_list.Count];
1673 for (int i = 0; i < node_list.Count; i++)
1675 if (node_list[i].NodeType == XmlNodeType.Element)
1676 commands[i] = new Command (node_list[i]);
1680 private void parse_plugins (MPlist plist)
1682 plugins = new Dictionary<MSymbol, Plugin> ();
1684 for (; ! plist.IsEmpty; plist = plist.Next)
1686 MPlist p = plist.Plist;
1687 MSymbol sym = p.Symbol;
1688 Plugin plugin = new Plugin (sym.Name);
1690 for (p = p.next; ! p.IsEmpty; p = p.next)
1692 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1693 domain.Defun (func);
1698 private void parse_plugins (XmlNode node)
1700 plugins = new Dictionary<MSymbol, Plugin> ();
1702 foreach (XmlNode n in node.ChildNodes)
1704 Plugin plugin = new Plugin (n.Attributes[0].Value);
1705 foreach (XmlNode nn in n.ChildNodes)
1707 Xex.Function func = new PluginMethod (plugin,
1708 nn.Attributes[0].Value);
1709 domain.Defun (func);
1714 private void parse_include (XmlNode node)
1717 MSymbol language, name, subname;
1718 MSymbol part, section;
1719 node = node.FirstChild;
1720 n = node.FirstChild;
1721 language = n.InnerText;
1726 subname = n.InnerText;
1728 subname = MSymbol.nil;
1729 node = node.NextSibling;
1730 part = node.InnerText;
1731 node = node.NextSibling;
1733 section = node.InnerText;
1735 section = MSymbol.nil;
1736 include_part (language, name, subname, part, section);
1739 private void parse_macros (XmlNode node)
1741 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1742 if (nn.NodeType == XmlNodeType.Element)
1744 if (nn.Name == Xex.Qdefun)
1745 domain.Defun (nn, true);
1746 else if (nn.Name == Qxi_include)
1749 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1750 if (nn.NodeType == XmlNodeType.Element
1751 && nn.Name == Xex.Qdefun)
1752 domain.Defun (nn, false);
1755 private void parse_maps (XmlNode node)
1757 for (node = node.FirstChild; node != null; node = node.NextSibling)
1759 if (node.Name == Qmap)
1761 MSymbol name = node.Attributes[0].Value;
1762 Map map = new Map (name);
1764 for (XmlNode nd = node.FirstChild; nd != null;
1765 nd = nd.NextSibling)
1766 if (nd.Name == Qrule)
1768 XmlNode n = nd.FirstChild;
1769 if (n.Name != Qkeyseq)
1771 KeySeq keyseq = (KeySeq) KeySeq.parser (domain, n);
1772 Xex.Term[] actions = Xex.ParseTerms (domain, n.NextSibling);
1773 map.entries.Add (new Map.Entry (domain, keyseq, actions));
1776 else if (node.Name == Qxi_include)
1777 parse_include (node);
1781 private void parse_states (MPlist plist)
1783 for (; ! plist.IsEmpty; plist = plist.next)
1786 State state = new State (this, plist.Plist);
1787 states.Add (state.name, state);
1791 private void parse_states (XmlNode node)
1793 for (node = node.FirstChild; node != null; node = node.NextSibling)
1795 if (node.Name == Qstate)
1797 State state = new State (this, node);
1798 states.Add (state.name, state);
1800 else if (node.Name == Qxi_include)
1801 parse_include (node);
1805 private void include_part (MSymbol language, MSymbol name, MSymbol subname,
1806 MSymbol part, MSymbol section)
1808 MInputMethod im = MInputMethod.Find (language, name, subname);
1815 if (section == MSymbol.nil)
1816 im.domain.CopyFunc (domain);
1818 im.domain.CopyFunc (domain, (Xex.Symbol) section.Name);
1820 else if (part == Mmap)
1822 if (section == MSymbol.nil)
1824 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1825 maps[kv.Key] = kv.Value;
1830 if (im.maps.TryGetValue (section, out map))
1831 maps[section] = map;
1834 else if (part == Mstate)
1836 if (section == MSymbol.nil)
1838 for (MPlist p = im.states; ! p.IsEmpty; p = p.next)
1839 states.Add (p.Key, p.Val);
1843 MSymbol state_name = (string) section.Name;
1844 State state = (State) im.states.Get (state_name);
1846 states.Add (state.name, state);
1851 private void parse_include (MPlist plist)
1853 if (! plist.IsPlist)
1855 MPlist p = plist.Plist;
1856 MSymbol language, name, subname;
1857 language = p.Symbol;
1860 name = subname = MSymbol.nil;
1866 subname = MSymbol.nil;
1871 if (! plist.IsSymbol)
1873 MSymbol part = plist.Symbol;
1875 MSymbol section = MSymbol.nil;
1877 section = plist.Symbol;
1878 include_part (language, name, subname, part, section);
1881 private Xex.Term parse_cond (MPlist plist)
1883 Xex.Term[] args = new Xex.Term[plist.Count];
1885 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1887 if (! plist.IsPlist)
1888 throw new Exception ("Invalid cond args: " + plist);
1889 MPlist p = plist.Plist;
1890 List<Xex.Term> arg = new List<Xex.Term> ();
1891 arg.Add (parse_action (p, true));
1892 for (p = p.next; ! p.IsEmpty; p = p.next)
1893 arg.Add (parse_action (p, false));
1894 args[i] = new Xex.Term (arg);
1896 return new Xex.Term (domain, Qcond, args);
1899 private Xex.Term parse_insert (MPlist plist)
1904 arg = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1905 else if (plist.IsMText)
1906 arg = new Xex.Term ((string) plist.Text);
1907 else if (plist.IsInteger)
1908 arg = new Xex.Term (plist.Integer);
1909 else if (plist.IsPlist)
1911 MPlist pl = plist.Plist;
1913 args = new Xex.Term[pl.Count];
1915 for (i = 0; ! pl.IsEmpty; i++, pl = pl.next)
1918 args[i] = new Xex.Term ((string) pl.Text);
1919 else if (pl.IsPlist)
1921 List<Xex.Term> list = new List<Xex.Term> ();
1922 for (MPlist p = pl.Plist; ! p.IsEmpty; p = p.next)
1925 list.Add (new Xex.Term ((string) p.Text));
1927 throw new Exception ("Invalid candidates: " + p);
1931 throw new Exception ("Invalid candidates: " + pl);
1933 return new Xex.Term (domain, Qinsert_candidates, args);
1936 throw new Exception ("Invalid arg to insert: " + plist);
1937 args = new Xex.Term[1];
1939 return new Xex.Term (domain, Qinsert, args);
1942 private Xex.Term parse_select (MPlist plist)
1944 Xex.Term[] args = new Xex.Term[1];
1945 if (plist.IsInteger)
1946 args[0] = new Xex.Term (plist.Integer);
1947 else if (! plist.IsSymbol)
1948 throw new Exception ("Invalid arg to select: " + plist);
1949 else if (plist.Symbol.Name[0] == '@')
1950 args[0] = new Xex.Term (Selector.Get (plist.Symbol));
1952 args[0] = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1953 return new Xex.Term (domain, Qselect, args);
1956 private Xex.Term parse_funcall_with_marker (MPlist plist, Xex.Symbol func)
1958 Xex.Term[] args = new Xex.Term[1];
1959 if (plist.IsInteger && func != Qmark)
1960 args[0] = new Xex.Term (plist.Integer);
1961 else if (plist.IsSymbol)
1962 args[0] = new Xex.Term (Marker.Get (plist.Symbol));
1964 throw new Exception ("Invalid arg to " + func + ": " + plist);
1965 return new Xex.Term (domain, func, args);
1968 private Xex.Term parse_char_at (MSymbol name)
1970 Xex.Term[] args = new Xex.Term[1];
1971 args[0] = new Xex.Term (Marker.Get (name));
1972 return new Xex.Term (domain, Qchar_at, args);
1975 private Xex.Term parse_shift (MPlist plist)
1977 Xex.Term[] args = new Xex.Term[1];
1978 if (! plist.IsSymbol)
1979 throw new Exception ("Invalid arg to shift: " + plist);
1980 args[0] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1981 return new Xex.Term (domain, Qshift, args);
1984 private Xex.Term parse_action (MPlist plist, bool as_funarg)
1988 MPlist p = plist.Plist;
1990 if (p.IsMText || p.IsPlist)
1991 return parse_insert (plist);
1993 throw new Exception ("Invalid action: " + p);
1994 MSymbol sym = p.Symbol;
1995 Xex.Symbol name = sym.Name;
1998 return parse_cond (p);
1999 if (name == Qinsert)
2000 return parse_insert (p);
2001 if (name == Qselect)
2002 return parse_select (p);
2003 if (name == Qdelete || name == Qmove || name == Qmark)
2004 return parse_funcall_with_marker (p, name);
2006 return parse_shift (p);
2007 if (name == Qset || name == Qadd || name == Qsub
2008 || name == Qmul || name == Qdiv)
2011 throw new Exception ("Invalid action: " + p);
2012 Xex.Symbol varname = p.Symbol.Name;
2013 Xex.Term[] args = new Xex.Term[1];
2014 args[0] = parse_action (p.next, true);
2015 return new Xex.Term (domain, name, varname, args);
2021 else if (name == Qhide)
2022 name = Qhide_candidates;
2023 else if (name == Qshow)
2024 name = Qshow_candidates;
2026 return new Xex.Term (domain, name, null);
2028 return new Xex.Term (domain, name, parse_actions (p, true));
2031 else if (plist.IsSymbol)
2033 if (plist.Symbol == Matat)
2034 return new Xex.Term (domain, Qkey_count, null);
2035 if (plist.Symbol == Mat_minus_zero)
2036 return new Xex.Term (domain, Qsurrounding_text_flag, null);
2037 if (plist.Symbol.Name[0] == '@')
2038 return parse_char_at (plist.Symbol);
2039 return new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
2041 else if (plist.IsMText)
2042 return (as_funarg ? new Xex.Term ((string) plist.Text)
2043 : parse_insert (plist));
2044 else if (plist.IsInteger)
2045 return (as_funarg ? new Xex.Term (plist.Integer)
2046 : parse_insert (plist));
2048 throw new Exception ("Invalid action: " + plist);
2051 private Xex.Term[] parse_actions (MPlist plist, bool as_funarg)
2053 Xex.Term[] terms = new Xex.Term[plist.Count];
2055 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
2056 terms[i] = parse_action (plist, as_funarg);
2060 private void parse_macros (MPlist plist)
2062 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2065 MPlist p = pl.Plist;
2068 domain.Defun ((Xex.Symbol) p.Symbol.Name, null, null, true);
2070 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2073 MPlist p = pl.Plist;
2077 domain.Defun ((Xex.Symbol) p.Symbol.Name, null,
2078 parse_actions (p.next, false), false);
2082 private void parse_maps (MPlist plist)
2084 for (; ! plist.IsEmpty; plist = plist.next)
2087 MPlist pl = plist.Plist;
2091 Map map = new Map (pl.Symbol);
2092 maps[pl.Symbol] = map;
2093 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
2097 MPlist p = pl.Plist;
2100 keys = new KeySeq (p.Text);
2102 keys = new KeySeq (p.Plist);
2107 = p.IsEmpty ? null : parse_actions (p, false);
2108 map.entries.Add (new Map.Entry (domain, keys, actions));
2113 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
2117 ((Context) domain.context).insert (args[0].Intval, null);
2119 ((Context) domain.context).insert ((MText) args[0].Strval, null);
2123 private static Xex.Term Finsert_candidates (Xex.Domain domain,
2127 Context ic = (Context) domain.context;
2128 Xex.Variable v = ic.domain.GetVar (Qcandidates_group_size, false);
2129 int column = (v == null ? 0 : v.Value.Intval);
2130 Candidates candidates = new Candidates (args, column);
2131 object candidate = candidates.Current;
2133 if (candidate is MText)
2134 ic.insert ((MText) candidate, candidates);
2136 ic.insert ((int) candidate, candidates);
2140 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
2143 Context ic = (Context) domain.context;
2144 Marker m = (Marker) args[0].Objval;
2146 return new Xex.Term (m.CharAt (ic));
2149 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
2152 Context ic = (Context) domain.context;
2156 pos = args[0].Intval;
2159 Marker m = (Marker) args[0].Objval;
2160 pos = m.Position (ic);
2162 return new Xex.Term (ic.delete (pos));
2165 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
2168 Context ic = (Context) domain.context;
2169 Candidates can = ic.candidates;
2173 object candidate = can.Current;
2175 if (candidate is MText)
2176 ic.delete (ic.cursor_pos - ((MText) candidate).Length);
2178 ic.delete (ic.cursor_pos - 1);
2180 candidate = can.Select (args[0].Intval);
2182 candidate = can.Select ((Selector) args[0].Objval);
2183 if (candidate is MText)
2184 ic.insert ((MText) candidate, can);
2186 ic.insert ((int) candidate, can);
2191 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
2194 ((Context) domain.context).show ();
2198 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
2201 ((Context) domain.context).hide ();
2205 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
2208 Context ic = (Context) domain.context;
2209 int pos = (args[0].IsInt ? args[0].Intval
2210 : ((Marker) args[0].Objval).Position (ic));
2215 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
2218 Marker m = (Marker) args[0].Objval;
2219 m.Mark ((Context) domain.context);
2223 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
2226 Context ic = (Context) domain.context;
2229 ic.pushback (args[0].Intval);
2230 else if (args[0].IsStr)
2231 ic.pushback (new KeySeq (args[0].Strval));
2233 ic.pushback ((KeySeq) args[0].Objval);
2237 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
2240 ((Context) domain.context).pop ();
2244 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
2247 int n = args.Length == 0 ? -2 : args[0].Intval;
2248 ((Context) domain.context).undo (n);
2252 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
2255 ((Context) domain.context).commit ();
2259 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
2262 ((Context) domain.context).commit ();
2263 args = new Xex.Term[2];
2264 args[0] = args[1] = Tcatch_tag;
2265 return Xex.Fthrow (domain, vari, args);
2268 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
2271 Context ic = (Context) domain.context;
2272 MSymbol state_name = (string) args[0].Symval;
2273 State state = (State) ic.im.states.Get (state_name);
2275 throw new Exception ("Unknown state: " + state_name);
2276 ((Context) domain.context).shift (state);
2280 private static Xex.Term Fshiftback (Xex.Domain domain, Xex.Variable vari,
2283 ((Context) domain.context).shift (null);
2287 private static Xex.Term Fkey_count (Xex.Domain domain, Xex.Variable vari,
2290 return new Xex.Term (((Context) domain.context).key_head);
2293 private static Xex.Term Fsurrounding_flag (Xex.Domain domain,
2297 return new Xex.Term (((Context) domain.context).GetSurroundingText == null
2301 public override string ToString ()
2304 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
2305 if (commands != null)
2307 str += " (commands";
2308 foreach (Command cmd in commands)
2312 if (var_names != null)
2314 str += " (variables";
2315 foreach (Xex.Symbol var in var_names)
2319 if (plugins != null)
2322 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
2323 str += " " + kv.Value;
2327 foreach (KeyValuePair<MSymbol, Map> kv in maps)
2328 str += " " + kv.Value;
2330 for (MPlist p = states; ! p.IsEmpty; p = p.next)
2331 str += " (" + p.Key + " " + ((State) p.Val).keymap + ")";
2335 public class Context
2337 internal MInputMethod im;
2338 internal Xex.Domain domain;
2339 private bool active;
2341 public Callback PreeditChanged;
2342 public Callback StatusChanged;
2343 public Callback CandidateChanged;
2344 public Callback GetSurroundingText;
2345 public Callback DelSurroundingText;
2347 private MText status;
2348 private MText produced = new MText ();
2349 internal MText preedit = new MText ();
2350 internal int cursor_pos;
2351 internal MPlist marker_positions = new MPlist ();
2353 internal Candidates candidates;
2354 private bool candidate_show;
2355 public bool CandidateShow { get { return candidate_show; } }
2357 private State initial_state, state, prev_state;
2358 private MText state_preedit = new MText ();
2359 private int state_key_head;
2360 private object state_var_values, state_initial_var_values;
2361 private int state_pos;
2363 private Keymap keymap;
2364 // Sequence of input keys.
2365 internal KeySeq keys = new KeySeq ();
2366 // Index into KEYS specifying the next key to handle.
2367 internal int key_head;
2369 internal MText preceding_text = new MText ();
2370 internal MText following_text = new MText ();
2372 // Set to false before calling the method 'handle_key', and set
2373 // to true when some key is unhandled.
2374 private bool key_unhandled;
2376 // The unhandled key. It has the meaning only when
2377 // 'key_unhandled' is true.
2378 private Key unhandled_key;
2380 internal ChangedStatus changed;
2382 private void set_cursor (string prefix, int pos)
2386 candidates = (Candidates) preedit.GetProp (cursor_pos - 1,
2392 internal void reset ()
2394 status = initial_state.title;
2398 set_cursor ("reset", 0);
2399 marker_positions.Clear ();
2401 candidate_show = false;
2403 state = prev_state = null;
2404 state_preedit.Del ();
2405 state_var_values = state_initial_var_values;
2407 shift (initial_state);
2409 preceding_text.Del ();
2410 following_text.Del ();
2412 changed = ChangedStatus.None;
2415 static Xex.Term[] catch_args = new Xex.Term[2];
2417 private bool take_actions (Xex.Term[] actions)
2419 catch_args[0] = Tcatch_tag;
2420 catch_args[1]= new Xex.Term (domain, Qprogn, actions);
2421 Xex.Term term = new Xex.Term (domain, Qcatch, catch_args);
2422 term = term.Eval (domain);
2423 return (! term.IsSymbol || term.Symval != Tcatch_tag.Symval);
2426 static MPlist callback_arg = new MPlist ();
2428 private bool get_surrounding_text (int len)
2430 if (len < 0 ? -len <= preceding_text.Length
2431 : len <= following_text.Length)
2433 if (GetSurroundingText == null)
2435 callback_arg.Set (MSymbol.integer, len);
2436 if (! GetSurroundingText (this, callback_arg)
2437 || ! callback_arg.IsMText)
2441 preceding_text = callback_arg.Text;
2442 return (-len <= preceding_text.Length);
2444 following_text = callback_arg.Text;
2445 return (len <= following_text.Length);
2448 internal int GetSurroundingChar (int pos)
2450 if (! get_surrounding_text (pos < 0 ? pos : pos + 1))
2453 return preceding_text[preceding_text.Length + pos];
2454 return following_text[pos];
2457 private void adjust_markers (int from, int to, int inserted)
2459 int diff = inserted - (to - from);
2461 for (MPlist p = marker_positions; ! p.IsEmpty; p = p.next)
2463 int pos = p.Integer;
2465 p.Set (p.Key, pos >= to ? pos + diff : from);
2467 if (cursor_pos >= to)
2468 set_cursor ("adjust", cursor_pos + diff);
2469 else if (cursor_pos > from)
2470 set_cursor ("adjust", from);
2473 private void preedit_replace (int from, int to, int c,
2474 Candidates candidates)
2476 preedit.Del (from, to);
2477 preedit.Ins (from, c);
2478 if (candidates != null)
2480 preedit.PushProp (from, from + 1, Mcandidates, candidates);
2481 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2484 adjust_markers (from, to, 1);
2487 private void preedit_replace (int from, int to, MText mt,
2488 Candidates candidates)
2490 preedit[from, to] = mt;
2491 if (candidates != null)
2493 preedit.PushProp (from, from + mt.Length, Mcandidates, candidates);
2494 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2497 adjust_markers (from, to, mt == null ? 0 : mt.Length);
2500 internal void insert (int c, Candidates candidates)
2502 preedit_replace (cursor_pos, cursor_pos, c, candidates);
2503 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2506 internal void insert (MText mt, Candidates candidates)
2508 preedit_replace (cursor_pos, cursor_pos, mt, candidates);
2509 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2512 internal int delete (int pos)
2514 int deleted = pos - cursor_pos;
2516 if (pos < cursor_pos)
2520 if (DelSurroundingText != null)
2522 callback_arg.Set (MSymbol.integer, pos);
2523 if (DelSurroundingText (this, callback_arg))
2525 if (callback_arg.IsInteger)
2526 deleted = callback_arg.Integer - cursor_pos;
2527 preceding_text.Del ();
2530 deleted = - cursor_pos;
2534 if (pos < cursor_pos)
2535 preedit_replace (pos, cursor_pos, null, null);
2539 if (pos > preedit.Length)
2541 if (DelSurroundingText != null)
2543 callback_arg.Set (MSymbol.integer, pos - preedit.Length);
2544 if (DelSurroundingText (this, callback_arg))
2546 if (callback_arg.IsInteger)
2547 deleted = callback_arg.Integer - cursor_pos;
2548 preceding_text.Del ();
2551 deleted = preedit.Length - cursor_pos;
2553 pos = preedit.Length;
2555 if (pos > cursor_pos)
2556 preedit_replace (cursor_pos, pos, null, null);
2559 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2563 internal void show ()
2565 candidate_show = true;
2566 changed |= ChangedStatus.CandidateShow;
2569 internal void hide ()
2571 candidate_show = false;
2572 changed |= ChangedStatus.CandidateShow;
2575 internal void move (int pos)
2579 else if (pos > preedit.Length)
2580 pos = preedit.Length;
2581 if (pos != cursor_pos)
2583 set_cursor ("move", pos);
2584 changed |= ChangedStatus.Preedit;
2588 internal void pushback (int n)
2601 if (key_head > keys.keyseq.Count)
2602 key_head = keys.keyseq.Count;
2606 internal void pushback (KeySeq keyseq)
2610 if (key_head < keys.keyseq.Count)
2611 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2612 for (int i = 0; i < keyseq.keyseq.Count; i++)
2613 keys.keyseq.Add (keyseq.keyseq[i]);
2616 internal void pop ()
2618 if (key_head < keys.keyseq.Count)
2619 keys.keyseq.RemoveRange (key_head, 1);
2622 internal void undo (int n)
2625 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2627 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2631 internal void commit ()
2633 if (preedit.Length > 0)
2635 Candidates.Detach (this);
2636 produced.Cat (preedit);
2637 preedit_replace (0, preedit.Length, null, null);
2641 internal void shift (State state)
2645 if (prev_state == null)
2650 if (state == initial_state)
2653 keys.keyseq.RemoveRange (0, key_head);
2655 if (state != this.state)
2657 domain.RestoreValues (state_initial_var_values);
2658 if (state.enter_actions != null)
2659 take_actions (state.enter_actions);
2665 if (state != this.state && state.enter_actions != null)
2666 take_actions (state.enter_actions);
2667 prev_state = this.state;
2670 if (this.state == null || this.state.title != state.title)
2671 this.changed |= ChangedStatus.StateTitle;
2673 keymap = state.keymap;
2676 public Context (MInputMethod im)
2678 if (im.load_status != LoadStatus.Full
2680 throw new Exception ("Openging " + im.tag + " failed");
2682 domain = new Xex.Domain ("context", im.domain, this);
2683 initial_state = (State) im.states.Val;
2684 state_initial_var_values = domain.SaveValues ();
2687 if (PreeditChanged != null)
2689 callback_arg.Set (MSymbol.mtext, preedit);
2690 PreeditChanged (this, callback_arg);
2692 if (StatusChanged != null)
2694 callback_arg.Set (MSymbol.mtext, status);
2695 StatusChanged (this, callback_arg);
2699 public ChangedStatus Changed { get { return changed; } }
2701 internal object GetCandidates (out int column)
2704 if (cursor_pos == 0)
2706 Candidates candidates
2707 = (Candidates) preedit.GetProp (cursor_pos - 1, Mcandidates);
2708 if (candidates == null)
2710 column = candidates.Column;
2711 return candidates.Current;
2714 private void save_state ()
2716 state_var_values = domain.SaveValues ();
2717 state_preedit.Del ();
2718 state_preedit.Ins (0, preedit);
2719 state_key_head = key_head;
2720 state_pos = cursor_pos;
2723 private void restore_state ()
2725 domain.RestoreValues (state_var_values);
2727 preedit.Ins (0, state_preedit);
2728 set_cursor ("restore", state_pos);
2731 private bool handle_key ()
2733 Console.WriteLine ("{0}:key='{1}'", state.name, keys.keyseq[key_head]);
2734 Keymap sub = keymap.Lookup (keys, ref key_head);
2740 if (keymap.map_actions != null)
2742 if (! take_actions (keymap.map_actions))
2745 else if (keymap.submaps != null)
2747 for (int i = state_key_head; i < key_head; i++)
2748 preedit_replace (cursor_pos, cursor_pos,
2749 keys.keyseq[i].ToChar (), null);
2751 if (keymap.submaps == null)
2753 if (keymap.branch_actions != null)
2755 if (! take_actions (keymap.branch_actions))
2758 if (keymap != state.keymap)
2764 State current_state = state;
2766 if (keymap.branch_actions != null)
2768 if (! take_actions (keymap.branch_actions))
2771 if (state == current_state)
2773 if (state == initial_state
2774 && key_head < keys.keyseq.Count)
2776 if (keymap != state.keymap)
2778 else if (keymap.branch_actions == null)
2779 shift (initial_state);
2785 public bool Toggle ()
2791 public bool UnhandledKey (out Key key)
2793 key = unhandled_key;
2794 return key_unhandled;
2797 public MText Preedit { get { return preedit; } }
2798 public MText Produced { get { return produced; } }
2801 // true: All keys are handled and there's no text to commit.
2802 // false: Some key is left unhandled or there's a text to
2803 // commit. The caller should refer to UnhandledKey and
2806 public bool Filter (Key key)
2810 key_unhandled = true;
2811 unhandled_key = key;
2814 if (key == Key.Reload)
2816 changed = ChangedStatus.None;
2818 preceding_text.Del ();
2819 following_text.Del ();
2821 key_unhandled = false;
2822 keys.keyseq.Add (key);
2824 while (key_head < keys.keyseq.Count)
2826 if (! handle_key ())
2828 unhandled_key = keys.keyseq[key_head++];
2829 key_unhandled = true;
2835 keys.keyseq.RemoveRange (0, key_head);
2838 if ((changed & ChangedStatus.Preedit) != ChangedStatus.None
2839 && PreeditChanged != null)
2841 callback_arg.Set (MSymbol.mtext, preedit);
2842 PreeditChanged (this, callback_arg);
2844 if ((changed & ChangedStatus.StateTitle) != ChangedStatus.None
2845 && StatusChanged != null)
2847 callback_arg.Set (MSymbol.mtext, status);
2848 StatusChanged (this, callback_arg);
2850 if ((changed & ChangedStatus.Candidate) != ChangedStatus.None
2851 && CandidateChanged != null)
2853 CandidateChanged (this, callback_arg);
2855 return (! key_unhandled && produced.Length == 0);
2858 public bool Filter ()
2860 changed = ChangedStatus.None;
2862 preceding_text.Del ();
2863 following_text.Del ();
2866 if ((changed & ChangedStatus.Preedit) != ChangedStatus.None
2867 && PreeditChanged != null)
2869 callback_arg.Set (MSymbol.mtext, preedit);
2870 PreeditChanged (this, callback_arg);
2872 if ((changed & ChangedStatus.StateTitle) != ChangedStatus.None
2873 && StatusChanged != null)
2875 callback_arg.Set (MSymbol.mtext, status);
2876 StatusChanged (this, callback_arg);
2878 if ((changed & ChangedStatus.Candidate) != ChangedStatus.None
2879 && CandidateChanged != null)
2881 CandidateChanged (this, callback_arg);
2883 return (produced.Length == 0);
2887 public class Session
2893 public Session (MInputMethod im, MText mt, int pos)
2895 ic = new Context (im);
2898 ic.GetSurroundingText = get_surrounding_text;
2899 ic.DelSurroundingText = del_surrounding_text;
2902 private bool get_surrounding_text (Context ic, MPlist args)
2904 int len = args.Integer;
2906 args.Set (MSymbol.mtext, mt[0, pos]);
2908 args.Set (MSymbol.mtext, mt[pos, mt.Length]);
2912 private bool del_surrounding_text (Context ic, MPlist args)
2914 int pos = this.pos + args.Integer;
2917 mt.Del (pos, this.pos);
2921 mt.Del (this.pos, pos);
2925 public bool HandleKey (ref Key key)
2927 if (! ic.Filter (key))
2929 MText produced = ic.Produced;
2930 mt.Ins (pos, produced);
2931 pos += produced.Length;
2933 if (ic.UnhandledKey (out unhandled))
2942 public bool Close ()
2944 bool result = ic.Filter ();
2947 mt.Ins (pos, ic.Produced);
2948 pos += ic.Produced.Length;
2955 public int CurrentPos { get { return pos; } set { pos = value; } }
2956 public MText Preedit { get { return ic.Preedit; } }