2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
12 using Xex = System.Xml.Expression.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (Context ic, MPlist args);
22 public static Callback PreeditStart, PreeditDone, PreeditDraw;
23 public static Callback StatusStart, StatusDone, StatusDraw;
24 public static Callback CandidateStart, CandidateDone, CandidateDraw;
25 public static Callback SetSpot;
26 public static Callback Toggle;
27 public static Callback Reset;
28 public static Callback GetSurroundingText;
29 public static Callback DeleteSurroundingText;
31 internal static Xex.Domain im_domain = new Xex.Domain (null);
32 private static MSymbol Minput_method = "input-method";
33 private static MSymbol Mdescription = "description";
34 private static MSymbol Mvariable = "variable";
35 private static MSymbol Mcommand = "command";
36 private static MSymbol Mmodule = "module";
37 private static MSymbol Mtitle = "title";
38 private static MSymbol Minclude = "include";
39 private static MSymbol Mmacro = "macro";
40 private static MSymbol Mmap = "map";
41 private static MSymbol Mmap_list = "map-list";
42 private static MSymbol Mstate = "state";
43 internal static MSymbol Mcandidates = "candidates";
44 private static MSymbol Minsert = "insert";
45 internal static MSymbol Mselect = "select";
46 private static MSymbol Mdelete = "delete";
47 private static MSymbol Mmove = "move";
48 private static MSymbol Mmark = "mark";
49 private static MSymbol Mmarker = "marker";
50 private static MSymbol Mset = "set";
51 private static MSymbol Madd = "add";
52 private static MSymbol Msub = "sub";
53 private static MSymbol Mmul = "mul";
54 private static MSymbol Mdiv = "div";
55 private static MSymbol Mif = "if";
56 private static MSymbol Mcond = "cond";
57 private static MSymbol Mchar_at = "char-at";
58 private static MSymbol Msurrounding_flag = "surrounding-text-flag";
59 private static MSymbol Mpushback = "pushback";
60 private static MSymbol Mkeyseq = "keyseq";
62 private static MSymbol Mget_surrounding_text = "get-surrounding-text";
63 private static MSymbol Mdel_surrounding_text = "del-surrounding-text";
65 private static MSymbol Mat_minus_zero = "@-0";
67 private static Xex.Symbol Qmap = "map";
68 private static Xex.Symbol Qrule = "rule";
69 private static Xex.Symbol Qkeyseq = "keyseq";
70 private static Xex.Symbol Qprogn = "progn";
71 private static Xex.Symbol Qcatch = "catch";
72 private static Xex.Symbol Qinsert = "insert";
73 private static Xex.Symbol Qinsert_candidates = "insert-candidates";
74 private static Xex.Symbol Qchar_at = "char-at";
75 private static Xex.Symbol Qsname = "sname";
76 private static Xex.Symbol Qmname = "mname";
77 private static Xex.Symbol Qstate_hook = "state-hook";
78 private static Xex.Symbol Qcatch_all_branch = "catch-all-branch";
79 private static Xex.Symbol Qbranch = "branch";
80 private static Xex.Symbol Qstate = "state";
81 private static Xex.Symbol Qtitle = "title";
84 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
85 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
87 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
88 = new Dictionary<MDatabase.Tag, MInputMethod> ();
90 internal static MInputMethod im_global = null;
93 private enum LoadStatus
103 public enum ChangedStatus
109 CandidateList = 0x08,
110 CandidateIndex = 0x10,
111 CandidateShow = 0x20,
114 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
115 | ChangedStatus.CandidateIndex
116 | ChangedStatus.CandidateShow);
118 public enum KeyModifier
121 Shift_L = 0x00400000,
122 Shift_R = 0x00800000,
124 Control_L = 0x01000000,
125 Control_R = 0x02000000,
126 Control = 0x03000000,
141 private static Dictionary<string, uint> keysyms
142 = new Dictionary<string, uint> ();
143 private static Dictionary<string, KeyModifier> keymodifiers
144 = new Dictionary<string, KeyModifier> ();
145 private static uint keysym_base = 0x200000;
146 private static uint char_mask = ~((uint) KeyModifier.All);
147 public static Key Reload;
151 keysyms["bs"] = keysyms["backspace"] = 0x08;
152 keysyms["tab"] = 0x09;
153 keysyms["lf"] = keysyms["linefeed"] = 0x10;
154 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
155 keysyms["esc"] = keysyms["escape"] = 0x1B;
156 keysyms["spc"] = keysyms["space"] = 0x20;
157 keysyms["del"] = keysyms["delete"] = 0x7F;
158 keymodifiers["shift-l"] = KeyModifier.Shift_L;
159 keymodifiers["shift-r"] = KeyModifier.Shift_R;
160 keymodifiers["shift"] = KeyModifier.Shift;
161 keymodifiers["control-l"] = KeyModifier.Control_L;
162 keymodifiers["control-r"] = KeyModifier.Control_R;
163 keymodifiers["control"] = KeyModifier.Control;
164 keymodifiers["alt-l"] = KeyModifier.Alt_L;
165 keymodifiers["alt-r"] = KeyModifier.Alt_R;
166 keymodifiers["alt"] = KeyModifier.Alt;
167 keymodifiers["altgr"] = KeyModifier.AltGr;
168 keymodifiers["super"] = KeyModifier.Super;
169 keymodifiers["hyper"] = KeyModifier.Hyper;
170 Reload = new Key (keysym_base);
171 keysyms["-reload"] = keysym_base++;
174 private static uint decode_keysym (MSymbol keysym)
177 string name = keysym.Name;
179 if (name.Length == 1)
181 name = name.ToLower ();
182 if (! keysyms.TryGetValue (name, out key))
183 keysyms[name] = key = keysym_base++;
187 private static uint combine_modifiers (uint c, KeyModifier modifiers)
189 if (c < 0x7F && c != 0x20)
191 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
192 && Char.IsLower ((char) c))
194 modifiers &= ~KeyModifier.Shift;
195 c = Char.ToUpper ((char) c);
197 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
199 modifiers &= ~KeyModifier.Control;
203 return c | (uint) modifiers;
206 public Key (uint c) { key = c; }
207 public Key (int c) { key = (uint) c; }
209 public Key (uint c, KeyModifier modifiers)
211 key = combine_modifiers (c, modifiers);
214 public Key (MSymbol keysym, KeyModifier modifiers)
216 key = combine_modifiers (decode_keysym (keysym), modifiers);
219 public Key (MSymbol keysym)
221 string str = keysym.Name;
222 int len = str.Length;
224 KeyModifier modifiers = KeyModifier.None;
226 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
229 modifiers |= KeyModifier.Shift;
230 else if (str[i] == 'C')
231 modifiers |= KeyModifier.Control;
232 else if (str[i] == 'A')
233 modifiers |= KeyModifier.Alt;
234 else if (str[i] == 'G')
235 modifiers |= KeyModifier.AltGr;
236 else if (str[i] == 's')
237 modifiers |= KeyModifier.Super;
238 else if (str[i] == 'H')
239 modifiers |= KeyModifier.Hyper;
242 key = combine_modifiers (str[i], modifiers);
244 key = combine_modifiers (decode_keysym (keysym), modifiers);
247 public Key (MPlist plist)
249 KeyModifier modifiers = KeyModifier.None;
252 for (p = plist; ! p.IsEmpty; p = p.next)
256 if (! p.next.IsEmpty)
257 throw new Exception ("Invalid Key: " + plist);
260 else if (! p.IsSymbol)
261 throw new Exception ("Invalid Key: " + plist);
264 string name = p.Symbol.Name.ToLower ();
267 if (! keymodifiers.TryGetValue (name, out m))
272 if (p.IsEmpty || ! p.next.IsEmpty)
273 throw new Exception ("Invalid Key: " + plist);
275 key = combine_modifiers ((uint) p.Integer, modifiers);
277 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
280 public bool HasModifier
282 get { return ((key & (uint) KeyModifier.All) != 0); }
285 public static bool operator== (Key k1, Key k2)
287 return k1.key == k2.key;
290 public static bool operator!= (Key k1, Key k2)
292 return k1.key != k2.key;
295 public override bool Equals (object o) { return key == ((Key) o).key; }
297 public override int GetHashCode () { return (int) key; }
299 public bool Match (Key k)
303 if ((k.key & char_mask) != (key & char_mask))
305 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
306 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
307 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
308 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
309 && (m2 & KeyModifier.Shift) != KeyModifier.None))
310 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
311 || ((m1 & KeyModifier.Control) == KeyModifier.Control
312 && (m2 & KeyModifier.Control) != KeyModifier.None))
313 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
314 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
315 && (m2 & KeyModifier.Alt) != KeyModifier.None))
316 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
321 return (int) (key & 0x1FFFFF);
324 public override string ToString ()
326 MText mt = new MText (ToChar ());
327 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
329 if (m != KeyModifier.None)
331 if ((m & KeyModifier.Shift) != KeyModifier.None)
333 if ((m & KeyModifier.Control) != KeyModifier.None)
335 if ((m & KeyModifier.Alt) != KeyModifier.None)
337 if ((m & KeyModifier.AltGr) != KeyModifier.None)
339 if ((m & KeyModifier.Super) != KeyModifier.None)
341 if ((m & KeyModifier.Hyper) != KeyModifier.None)
348 internal class KeySeq : Xex.TermValue
350 public List<Key> keyseq = new List<Key> ();
352 public override Xex.TermValue Clone ()
354 KeySeq ks = new KeySeq ();
355 ks.keyseq.InsertRange (0, keyseq);
361 public KeySeq (MPlist plist)
363 foreach (MPlist p in plist)
366 keyseq.Add (new Key (p.Symbol));
367 else if (p.IsInteger)
368 keyseq.Add (new Key ((char) p.Integer));
370 keyseq.Add (new Key (p.Plist));
372 throw new Exception ("Invalid Key Sequence: " + plist);
376 public KeySeq (MText mt) : base ()
378 for (int i = 0; i < mt.Length; i++)
379 keyseq.Add (new Key ((uint) mt[i]));
382 public KeySeq (List<Xex.Term> list)
384 int len = list.Count;
386 for (int i = 0; i < len; i++)
389 keyseq.Add (new Key (list[i].Intval));
390 else if (list[i].IsStr)
391 keyseq.Add (new Key (list[i].Strval));
392 else if (list[i].IsSymbol)
393 keyseq.Add (new Key ((string) list[i].Symval));
395 throw new Exception ("Invalid key: " + list[i]);
399 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
401 Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain);
402 return (term.IsStr ? new KeySeq ((MText) term.Strval)
403 : new KeySeq (term.Listval));
406 public override string ToString ()
409 foreach (Key key in keyseq)
413 foreach (Key k in keyseq)
417 mt.Cat (k.ToString ());
419 return (string) mt.Cat (")");
422 foreach (Key k in keyseq)
426 mt.Cat (k.ToString ());
428 return (string) mt.Cat ("\"");
435 public MText description;
436 internal List<KeySeq> keys;
438 public Command (MPlist p)
442 description = parse_description (p);
443 if (description == null)
444 description = "No description";
445 keys = new List<KeySeq> ();
446 for (p = p.next; ! p.IsEmpty; p = p.next)
449 keys.Add (new KeySeq (p.Text));
451 keys.Add (new KeySeq (p.Plist));
455 public Command (XmlNode node)
457 name = node.Attributes[0].Value;
458 keys = new List<KeySeq> ();
459 for (node = node.FirstChild; node != null; node = node.NextSibling)
461 if (node.Name == "description")
462 description = parse_description (node);
463 else if (node.Name == "keyseq")
464 keys.Add ((KeySeq) KeySeq.parser (null, node));
468 public override string ToString ()
470 string str = "(" + name + " \"" + (string) description;
471 foreach (KeySeq keyseq in keys)
477 internal class Plugin
480 private Assembly assembly;
481 private Type plugin_type;
483 public Plugin (string name)
488 public MethodInfo GetMethod (Xex.Symbol name)
490 if (assembly == null)
492 assembly = Assembly.LoadFrom (name + ".dll");
493 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
496 MethodInfo info = plugin_type.GetMethod ((string) name);
498 throw new Exception ("Invalid plugin method: " + name);
502 public override string ToString ()
504 return String.Format ("(module {0}", name);
508 internal class PluginMethod : Xex.Function
510 private Plugin plugin;
511 private MethodInfo method_info;
512 object[] parameters = new object[2];
514 public PluginMethod (Plugin plugin, string name)
515 : base ((Xex.Symbol) name, 0, -1)
517 this.plugin = plugin;
520 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
523 args = (Xex.Term[]) args.Clone ();
524 for (int i = 0; i < args.Length; i++)
526 args[i] = args[i].Eval (domain);
530 if (method_info == null)
531 method_info = plugin.GetMethod (name);
532 parameters[0] = domain.context;
533 parameters[1] = args;
534 return (Xex.Term) method_info.Invoke (null, parameters);
538 internal abstract class Marker : Xex.TermValue
540 private MSymbol name;
542 private Marker (MSymbol name)
547 public abstract int Position (Context ic);
548 public virtual void Mark (Context ic)
550 throw new Exception ("Can't set predefined marker: " + name);
552 public virtual int CharAt (Context ic)
554 return ic.preedit[Position (ic)];
556 public override Xex.TermValue Clone () { return this; }
558 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
560 return Get ((MSymbol) node.InnerText);
563 public class Named : Marker
565 public Named (MSymbol name) : base (name) { }
567 public override int Position (Context ic)
569 MPlist p = ic.markers.Find (name);
570 return (p != null ? p.Integer : 0);
574 public override void Mark (Context ic)
576 ic.markers.Put (name, ic.cursor_pos);
580 public class Predefined : Marker
582 public Predefined (MSymbol name) : base (name) { }
584 public override int Position (Context ic)
586 switch (name.Name[1]) {
588 case '>': return ic.preedit.Length;
589 case '-': return ic.cursor_pos - 1;
590 case '+': return ic.cursor_pos + 1;
592 if (ic.cursor_pos > 0)
594 int pos = ic.cursor_pos;
596 ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to);
601 if (ic.cursor_pos < ic.preedit.Length - 1)
603 int pos = ic.cursor_pos;
605 ic.preedit.FindProp (Mcandidates, pos, out from, out pos);
608 return ic.preedit.Length;
610 return name.Name[1] - '0';
615 public class PredefinedAbsolute : Marker
619 public PredefinedAbsolute (MSymbol name) : base (name)
621 if (! int.TryParse (name.Name.Substring (1), out pos))
622 throw new Exception ("Invalid marker name: " + name);
625 public override int Position (Context ic)
627 return (pos < ic.preedit.Length ? pos : ic.preedit.Length);
631 public class PredefinedSurround : Marker
633 private int distance;
635 public PredefinedSurround (MSymbol name) : base (name)
637 if (! int.TryParse (name.Name.Substring (2), out distance))
638 throw new Exception ("Invalid marker name: " + name);
643 public override int Position (Context ic)
645 return ic.cursor_pos + distance;
648 public override int CharAt (Context ic)
650 int pos = ic.cursor_pos + distance;
652 return ic.GetSurroundingChar (pos);
653 else if (pos >= ic.preedit.Length)
654 return ic.GetSurroundingChar (pos - ic.preedit.Length);
655 return ic.preedit[pos];
659 static internal Dictionary<MSymbol,Predefined> predefined_markers;
663 predefined_markers = new Dictionary<MSymbol,Predefined> ();
664 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]" };
665 foreach (MSymbol s in symlist)
666 predefined_markers[s] = new Predefined (s);
669 public static Marker Get (MSymbol name)
671 if (name.Name[0] == '@')
674 if (predefined_markers.TryGetValue (name, out pred))
676 if (name.Name.Length == 1)
677 throw new Exception ("Invalid marker name: " + name);
678 if (Char.IsDigit (name.Name[1]))
679 return new PredefinedAbsolute (name);
680 if (name.Name.Length == 2 || name == Mat_minus_zero
681 || ! (name.Name[1] == '-' || name.Name[1] == '+'))
682 throw new Exception ("Invalid marker name: " + name);
683 return new PredefinedSurround (name);
685 return new Named (name);
689 internal class Candidates
696 public Block (int index, Xex.Term term)
700 Data = (MText) term.Strval;
703 MPlist plist = new MPlist ();
705 foreach (Xex.Term t in term.Listval)
706 p = p.Add (MSymbol.mtext, (MText) t.Strval);
711 public Block (int index, MPlist plist)
716 else if (plist.IsPlist)
719 throw new Exception ("Invalid candidate: " + plist);
724 get { return (Data is MText
725 ? ((MText) Data).Length
726 : ((MPlist) Data).Count); }
729 public object this[int i]
732 if (Data is MText) return ((MText) Data)[i];
733 return ((MPlist) Data)[i];
738 private Block[] blocks;
740 private int index = 0;
741 public object[] group;
743 private bool IsFixed { get { return group != null; } }
746 Block last = blocks[blocks.Length - 1];
747 return last.Index + last.Count; }
751 get { return (IsFixed ? index % group.Length
752 : index - blocks[row].Index); }
755 public object Group {
756 get { return (IsFixed ? group : blocks[row].Data); }
759 public int GroupLength
764 int nitems = group.Length;
765 int start = index - (index % nitems);
767 return (start + nitems <= total ? nitems : total - start);
769 return blocks[row].Count;
773 public object Current {
775 return (IsFixed ? group[index % group.Length]
776 : blocks[row][index - blocks[row].Index]);
780 public Candidates (MPlist list, int column)
782 int nblocks = list.Count;
784 blocks = new Block[nblocks];
785 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
786 start += (blocks[i] = new Block (index, list)).Count;
788 group = new object[column];
791 public Candidates (List<Xex.Term> list, int column)
793 int nblocks = list.Count;
795 blocks = new Block[nblocks];
796 for (int i = 0, start = 0; i < nblocks; i++)
797 start += (blocks[i] = new Block (index, list[i])).Count;
799 group = new object[column];
802 public static void Detach (Context ic)
804 ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
805 ic.candidates = null;
806 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
810 // Fill the array "group" by candidates stating from INDEX.
811 // INDEX must be a multiple of "column". Set NTIMES to the
812 // number of valid candidates in "group". Update "block" if
813 // necessary. Return "group".
815 private int fill_group (int start)
817 int nitems = group.Length;
822 while (start < b.Index)
825 while (start >= b.Index + b.Count)
831 for (int i = 0; i < nitems; i++, start++)
836 if (r == blocks.Length)
847 // Update "row" to what contains the first candidate of
848 // the previous candidate-group, update "current_index", and
849 // update "group" if necessary. Return the previous
850 // candidate-group. Set NITEMS to the number of valid
851 // candidates contained in that group.
853 public int PrevGroup ()
860 nitems = group.Length;
861 if ((index -= col + nitems) < 0)
862 index = (Total / nitems) * nitems;
863 nitems = fill_group (index);
867 row = row > 0 ? row-- : blocks.Length - 1;
868 nitems = blocks[row].Count;
869 index = blocks[row].Index;
871 index += col < nitems ? col : nitems - 1;
875 public int NextGroup ()
882 nitems = group.Length;
883 if ((index += nitems - col) >= Total)
885 nitems = fill_group (index);
889 row = row < blocks.Length - 1 ? row + 1 : 0;
890 nitems = blocks[row].Count;
891 index = blocks[row].Count;
893 index += col < nitems ? col : nitems - 1;
903 int nitems = PrevGroup ();
904 index += col < nitems - 1 ? col : nitems - 1;
913 int nitems = GroupLength;
915 if (col == nitems - 1)
917 nitems = NextGroup ();
931 index += GroupLength - (Column + 1);
934 public void Select (int col)
936 int maxcol = GroupLength - 1;
939 index = index - Column + col;
943 internal class Selector : Xex.TermValue
945 static new Dictionary<MSymbol, Selector> selectors;
949 selectors = new Dictionary<MSymbol, Selector> ();
950 MSymbol[] symlist = new MSymbol[] { "@<", "@=", "@>", "@-", "@+",
952 foreach (MSymbol s in symlist)
953 selectors[s] = new Selector (s);
954 selectors["@first"] = new Selector ('<');
955 selectors["@current"] = new Selector ('=');
956 selectors["@last"] = new Selector ('>');
957 selectors["@previous"] = new Selector ('-');
958 selectors["@next"] = new Selector ('+');
959 selectors["@previous-candidate-change"] = new Selector ('[');
960 selectors["@next-candidate-change"] = new Selector (']');
965 private Selector (MSymbol sym) { tag = sym.Name[1]; }
967 private Selector (char tag) { this.tag = tag; }
969 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
971 return Get ((MSymbol) node.InnerText);
974 public static Xex.TermValue Get (MSymbol name)
977 if (! selectors.TryGetValue (name, out selector))
978 throw new Exception ("Invalid selector name: " + name);
982 public override Xex.TermValue Clone () { return this; }
984 public void Select (Candidates candidates)
988 case '<': candidates.First (); break;
989 case '>': candidates.Last (); break;
990 case '-': candidates.Prev (); break;
991 case '+': candidates.Next (); break;
992 case '[': candidates.PrevGroup (); break;
993 case ']': candidates.NextGroup (); break;
1001 public MSymbol name;
1002 public List<Entry> entries = new List<Entry> ();
1004 public Map (MSymbol name) { this.name = name; }
1008 public KeySeq keyseq;
1009 public Xex.Term[] actions;
1011 public Entry (Xex.Domain domain, KeySeq keyseq, Xex.Term[] actions)
1013 this.keyseq = keyseq;
1014 this.actions = actions;
1018 public override string ToString () { return (string) name; }
1021 internal class Keymap
1023 public Dictionary<Key, Keymap> submaps;
1024 public Xex.Term[] map_actions, branch_actions;
1026 public Keymap () { }
1028 public void Add (KeySeq keys, int index,
1029 Xex.Term[] map_actions, Xex.Term[] branch_actions)
1031 if (index == keys.keyseq.Count)
1033 this.map_actions = map_actions;
1034 this.branch_actions = branch_actions;
1038 Key key = keys.keyseq[index];
1041 if (submaps == null)
1042 submaps = new Dictionary<Key, Keymap> ();
1044 submaps.TryGetValue (key, out sub);
1046 submaps[key] = sub = new Keymap ();
1047 sub.Add (keys, index + 1, map_actions, branch_actions);
1051 public void AddMap (Map map, Xex.Term[] branch_actions)
1053 foreach (Map.Entry entry in map.entries)
1054 Add (entry.keyseq, 0, entry.actions, branch_actions);
1057 public Keymap Lookup (KeySeq keys, ref int index)
1061 if (index < keys.keyseq.Count
1063 && submaps.TryGetValue (keys.keyseq[index], out sub))
1066 return sub.Lookup (keys, ref index);
1071 private void describe (MText mt, KeySeq keyseq)
1073 if (map_actions != null || branch_actions != null)
1077 mt.Cat ('(').Cat (keyseq.ToString ());
1078 if (map_actions != null)
1079 foreach (Xex.Term term in map_actions)
1080 mt.Cat (' ').Cat (term.ToString ());
1081 if (branch_actions != null)
1082 foreach (Xex.Term term in branch_actions)
1083 mt.Cat (' ').Cat (term.ToString ());
1086 if (submaps != null)
1087 foreach (KeyValuePair<Key, Keymap> kv in submaps)
1089 keyseq.keyseq.Add (kv.Key);
1090 kv.Value.describe (mt, keyseq);
1091 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
1095 public override string ToString ()
1098 KeySeq keyseq = new KeySeq ();
1100 describe (mt, keyseq);
1106 internal class State
1108 public Xex.Symbol name;
1110 public Xex.Term[] enter_actions, fallback_actions;
1111 public Keymap keymap = new Keymap ();
1113 public State (Xex.Symbol name, MText title)
1119 public State (MInputMethod im, XmlNode node)
1121 this.name = node.Attributes[Qsname].Value;
1122 XmlAttribute attr = node.Attributes[Qtitle];
1124 title = (MText) attr.Value;
1125 keymap = new Keymap ();
1126 for (node = node.FirstChild; node != null; node = node.NextSibling)
1128 if (node.Name == Qstate_hook)
1129 enter_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1130 else if (node.Name == Qcatch_all_branch)
1131 fallback_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1132 else if (node.Name == Qbranch)
1134 MSymbol mapname = node.Attributes[Qmname].Value;
1136 if (im.maps.TryGetValue (mapname, out map))
1137 keymap.AddMap (map, Xex.ParseTerms (im.domain,
1140 throw new Exception ("Unknown map: " + mapname);
1145 public State (MInputMethod im, MPlist plist)
1147 if (! plist.IsSymbol)
1148 throw new Exception ("Invalid state: " + plist);
1149 this.name = plist.Symbol.Name;
1153 this.title = plist.Text;
1156 keymap = new Keymap ();
1157 for (; ! plist.IsEmpty; plist = plist.next)
1159 if (! plist.IsPlist)
1160 throw new Exception ("Invalid branch: " + plist);
1161 MPlist p = plist.Plist;
1163 throw new Exception ("Invalid branch: " + p);
1164 MSymbol mapname = p.Symbol;
1165 if (mapname == MSymbol.t)
1166 enter_actions = im.parse_actions (p.next);
1167 else if (mapname == MSymbol.nil)
1168 fallback_actions = im.parse_actions (p.next);
1172 if (im.maps.TryGetValue (mapname, out map))
1173 keymap.AddMap (map, im.parse_actions (p.next));
1175 throw new Exception ("Unknown map: " + mapname);
1180 public override string ToString ()
1182 MText mt = "(" + name;
1185 mt.Cat (" \"" + title + "\"");
1186 mt += keymap.ToString ();
1187 return (string) mt + ")";
1192 internal Xex.Domain domain = new Xex.Domain (im_domain, null);
1194 private LoadStatus load_status = LoadStatus.None;
1195 private MDatabase.Tag tag;
1196 private MDatabase mdb;
1198 private MText description;
1199 internal MText title;
1200 internal Command[] commands;
1201 internal Xex.Symbol[] var_names;
1202 internal Dictionary<MSymbol, Plugin> plugins;
1203 internal Dictionary<MSymbol, Map> maps;
1204 internal Dictionary<Xex.Symbol, State> states;
1205 internal State initial_state;
1207 static MInputMethod ()
1209 im_domain.DefTerm ("keyseq", KeySeq.parser);
1210 im_domain.DefTerm ("marker", Marker.parser);
1211 im_domain.DefTerm ("selector", Selector.parser);
1213 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1214 im_domain.DefSubr (Finsert_candidates, "candidates", false, 1, -1);
1215 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1216 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1217 im_domain.DefSubr (Fshow, "show", false, 0, 0);
1218 im_domain.DefSubr (Fhide, "hide", false, 0, 0);
1219 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1220 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1221 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1222 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1223 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1224 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1225 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1226 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1227 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
1228 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1229 im_domain.DefSubr (Fkey_count, "key-count", false, 1, 1);
1230 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag",
1233 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
1234 List<MDatabase> list = MDatabase.List (tag);
1235 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
1236 foreach (MDatabase mdb in list)
1237 im_table[mdb.tag] = new MInputMethod (mdb.tag);
1241 private MInputMethod (MDatabase.Tag tag)
1246 // Instance Properties
1247 public MSymbol Language { get { return tag[1]; } }
1248 public MSymbol Name { get { return tag[2]; } }
1249 public MSymbol SubName { get { return tag[3]; } }
1251 public bool Info (out MText description,
1253 out Xex.Variable[] variables,
1254 out Command[] commands)
1256 if ((load_status & LoadStatus.Header) != LoadStatus.Header
1257 && ! load_header ())
1265 description = this.description;
1267 if (var_names == null)
1271 variables = new Xex.Variable[var_names.Length];
1273 foreach (Xex.Symbol name in var_names)
1274 variables[i++] = domain.GetVar (name, false);
1276 commands = this.commands;
1280 public static MInputMethod Find (MSymbol language, MSymbol name)
1282 return Find (language, name, MSymbol.nil);
1285 public static MInputMethod Find (MSymbol language, MSymbol name,
1288 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
1292 return (im_table.TryGetValue (tag, out im) ? im : null);
1297 return ((load_status == LoadStatus.Full) || load_body ());
1300 public static MInputMethod[] List ()
1302 MInputMethod[] array = new MInputMethod[im_table.Count];
1305 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
1306 array[i++] = kv.Value;
1310 private bool load_header ()
1312 mdb = MDatabase.Find (tag);
1315 mdb.name_table = Xex.Symbol.Table;
1317 MSymbol format = mdb.Format;
1319 if (format == MSymbol.plist)
1320 load ((MPlist) mdb.Load (Mmap), false);
1323 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
1324 load (doc.DocumentElement, false);
1326 } catch (Exception e) {
1327 Console.WriteLine ("{0}\n", e);
1328 load_status = LoadStatus.Error;
1331 load_status |= LoadStatus.Header;
1335 private bool load_body ()
1337 mdb = MDatabase.Find (tag);
1340 mdb.name_table = Xex.Symbol.Table;
1342 object obj = mdb.Load ();
1344 load ((MPlist) obj, true);
1346 load ((XmlDocument) obj, true);
1347 } catch (Exception e) {
1348 Console.WriteLine (e);
1349 load_status = LoadStatus.Error;
1352 load_status = LoadStatus.Full;
1356 private void add_default_state ()
1358 Xex.Symbol Qinit = "init";
1359 State state = new State (Qinit, null);
1360 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1361 state.keymap.AddMap (kv.Value, null);
1362 states[Qinit] = initial_state = state;
1365 private void load (MPlist plist, bool full)
1367 maps = new Dictionary<MSymbol, Map> ();
1368 states = new Dictionary<Xex.Symbol, State> ();
1370 for (; ! plist.IsEmpty; plist = plist.next)
1373 MPlist pl = plist.Plist;
1376 MSymbol sym = pl.Symbol;
1379 if (sym == Mdescription)
1380 description = parse_description (pl);
1381 else if (sym == Mtitle)
1386 else if (sym == Mvariable)
1387 parse_variables (pl);
1388 else if (sym == Mcommand)
1389 parse_commands (pl);
1394 else if (sym == Minclude)
1396 else if (sym == Mmacro)
1398 else if (sym == Mmap)
1400 else if (sym == Mstate)
1405 if (description == null)
1406 description = (MText) "No description";
1408 title = new MText (tag[2].Name);
1409 if (commands == null)
1410 commands = new Command[0];
1413 if (states.Count == 0)
1414 add_default_state ();
1417 private void load (XmlNode node, bool full)
1419 bool skip_header = load_status == LoadStatus.Header;
1421 maps = new Dictionary<MSymbol, Map> ();
1422 states = new Dictionary<Xex.Symbol, State> ();
1424 if (node.NodeType == XmlNodeType.Document)
1425 node = node.FirstChild;
1426 while (node.NodeType != XmlNodeType.Element)
1427 node = node.NextSibling;
1428 for (node = node.FirstChild; node != null; node = node.NextSibling)
1430 if (node.NodeType != XmlNodeType.Element)
1434 if (node.Name == "description")
1435 description = parse_description (node);
1436 else if (node.Name == "title")
1437 title = parse_title (node);
1438 else if (node.Name == "variable-list")
1439 parse_variables (node);
1440 else if (node.Name == "command-list")
1441 parse_commands (node);
1445 if (node.Name == "module-list")
1446 parse_plugins (node);
1447 else if (node.Name == "macro-list")
1448 parse_macros (node);
1449 else if (node.Name == "map-list")
1451 else if (node.Name == "state-list")
1452 parse_states (node);
1455 if (description == null)
1456 description = (MText) "No description";
1458 title = new MText (tag[2].Name);
1459 if (commands == null)
1460 commands = new Command[0];
1463 if (states.Count == 0)
1464 add_default_state ();
1467 private static void transform (MPlist plist)
1471 for (; ! plist.IsEmpty; plist = plist.next)
1475 MPlist p = new MPlist ();
1476 p.Add (MSymbol.symbol, Minsert);
1477 p.Add (MSymbol.mtext, plist.Text);
1478 plist.Set (MSymbol.plist, p);
1480 else if (plist.IsInteger)
1482 MPlist p = new MPlist ();
1483 p.Add (MSymbol.symbol, Minsert);
1484 p.Add (MSymbol.integer, plist.Integer);
1485 plist.Set (MSymbol.plist, p);
1487 else if (plist.IsPlist)
1489 MPlist pl = plist.Plist;
1493 if (pl.Symbol == Madd)
1494 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1495 else if (pl.Symbol == Msub)
1496 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1497 else if (pl.Symbol == Mmul)
1498 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1499 else if (pl.Symbol == Mdiv)
1500 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1501 else if (pl.Symbol == Minsert)
1503 // (insert (CANDIDATES ...))
1504 // => (candidates CANDIDATES ...)
1505 if (pl.next.IsPlist)
1507 pl.Set (MSymbol.symbol, Mcandidates);
1509 MPlist p = pl.Plist;
1510 pl.Set (p.key, p.val);
1511 for (p = p.next; ! p.IsEmpty; p = p.next);
1512 pl.Add (p.key, p.val);
1515 else if (pl.Symbol == Mif)
1519 transform (pl.next);
1521 else if (pl.Symbol == Mcond)
1523 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1526 MPlist p = pl.Plist;
1534 else if (pl.Symbol == Mdelete
1535 || pl.Symbol == Mmove
1536 || pl.Symbol == Mmark)
1541 MSymbol sym = pl.Symbol;
1542 MPlist p = new MPlist ();
1543 p.Add (MSymbol.symbol, Mmarker);
1544 p.Add (MSymbol.symbol, sym);
1545 pl.Set (MSymbol.plist, p);
1548 else if (pl.Symbol == Mpushback)
1552 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1555 else if (pl.IsMText)
1557 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1558 pl.Push (MSymbol.symbol, Mcandidates);
1561 else if (plist.IsSymbol)
1563 MSymbol sym = plist.Symbol;
1565 if (sym.Name.Length >= 3
1566 && sym.Name[0] == '@'
1567 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1569 int pos = int.Parse (sym.Name.Substring (1));
1570 MPlist p = new MPlist ();
1574 p.Add (MSymbol.symbol, Msurrounding_flag);
1578 if (sym.Name[1] == '+')
1580 p.Add (MSymbol.symbol, Mchar_at);
1581 p.Add (MSymbol.integer, pos);
1583 plist.Set (MSymbol.plist, p);
1590 private static MText parse_description (MPlist plist)
1596 plist = plist.Plist;
1597 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1598 && plist.next.IsMText)
1599 return plist.next.Text;
1604 private static MText parse_description (XmlNode node)
1606 if (node.HasChildNodes)
1607 node = node.FirstChild;
1608 return node.InnerText;
1611 private static MText parse_title (XmlNode node)
1613 return node.InnerText;
1616 private void new_variable (Xex.Symbol name, string desc, int val,
1617 MPlist pl, Xex.Variable vari)
1625 int nrange = pl.Count;
1626 range = new int[nrange * 2];
1627 for (int i = 0; i < nrange; i++)
1631 MPlist p = pl.Plist;
1633 if (! p.IsInteger || ! p.next.IsInteger)
1634 throw new Exception ("Invalid range: " + p);
1635 range[i * 2] = p.Integer;
1636 range[i * 2 + 1] = p.next.Integer;
1638 else if (pl.IsInteger)
1639 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1641 throw new Exception ("Invalid range: " + pl);
1645 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1648 Xex.Term term = new Xex.Term (val);
1650 vari.DefaultValue = term;
1655 private void new_variable (Xex.Symbol name, string desc, MText val,
1656 MPlist pl, Xex.Variable vari)
1664 range = new string[pl.Count * 2];
1665 for (int i = 0; i < range.Length; i++)
1668 range[i] = (string) pl.Text;
1670 throw new Exception ("Invalid range: " + pl);
1674 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1677 Xex.Term term = new Xex.Term ((string) val);
1679 vari.DefaultValue = term;
1684 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1685 MPlist pl, Xex.Variable vari)
1688 Xex.Symbol sym = val.Name;
1694 range = new Xex.Symbol[pl.Count * 2];
1695 for (int i = 0; i < range.Length; i++)
1698 range[i] = pl.Symbol.Name;
1700 throw new Exception ("Invalid range: " + pl);
1704 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1707 Xex.Term term = new Xex.Term (sym);
1709 vari.DefaultValue = term;
1714 private Xex.Variable get_global_var (Xex.Symbol name)
1716 if (im_global == null || this != im_global)
1718 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1720 im_global = im_table[tag];
1721 if (! im_global.Open ())
1722 throw new Exception ("Failed to load global");
1724 return im_global.domain.GetVar (name, false);
1727 private void parse_variables (MPlist plist)
1729 var_names = new Xex.Symbol[plist.Count];
1731 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1733 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1734 throw new Exception ("Invalid variable: " + plist);
1736 MPlist p = plist.Plist;
1737 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1738 var_names[i] = name;
1740 string desc = (string) parse_description (p);
1741 Xex.Variable vari = get_global_var (name);
1743 domain.Defvar (vari);
1749 new_variable (name, desc, p.Integer, p.next, vari);
1751 new_variable (name, desc, p.Text, p.next, vari);
1752 else if (p.IsSymbol)
1753 new_variable (name, desc, p.Symbol, p.next, vari);
1755 throw new Exception ("Invalid variable type: " + p.val);
1760 private void parse_variables (XmlNode node)
1762 XmlNodeList node_list = node.ChildNodes;
1764 var_names = new Xex.Symbol[node_list.Count];
1765 for (int i = 0; i < node_list.Count; i++)
1767 Xex.Symbol name = node_list[i].Attributes[0].Value;
1768 Xex.Variable vari = get_global_var (name);
1770 domain.Defvar (vari);
1771 domain.Defvar (node_list[i]);
1772 var_names[i] = name;
1776 private void parse_commands (MPlist plist)
1778 commands = new Command[plist.Count];
1780 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1781 if (plist.IsPlist && plist.Plist.IsSymbol)
1782 commands[i++] = new Command (plist.Plist);
1785 private void parse_commands (XmlNode node)
1787 XmlNodeList node_list = node.ChildNodes;
1789 commands = new Command[node_list.Count];
1790 for (int i = 0; i < node_list.Count; i++)
1792 if (node_list[i].NodeType == XmlNodeType.Element)
1793 commands[i] = new Command (node_list[i]);
1797 private void parse_plugins (MPlist plist)
1799 plugins = new Dictionary<MSymbol, Plugin> ();
1801 for (; ! plist.IsEmpty; plist = plist.Next)
1803 MPlist p = plist.Plist;
1804 MSymbol sym = p.Symbol;
1805 Plugin plugin = new Plugin (sym.Name);
1807 for (p = p.next; ! p.IsEmpty; p = p.next)
1809 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1810 domain.Defun (func);
1815 private void parse_plugins (XmlNode node)
1817 plugins = new Dictionary<MSymbol, Plugin> ();
1819 foreach (XmlNode n in node.ChildNodes)
1821 Plugin plugin = new Plugin (n.Attributes[0].Value);
1822 foreach (XmlNode nn in n.ChildNodes)
1824 Xex.Function func = new PluginMethod (plugin,
1825 nn.Attributes[0].Value);
1826 domain.Defun (func);
1831 private void parse_macros (XmlNode node)
1833 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1834 if (nn.NodeType == XmlNodeType.Element)
1835 domain.Defun (nn, true);
1836 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1837 if (nn.NodeType == XmlNodeType.Element)
1838 domain.Defun (nn, false);
1841 private void parse_maps (XmlNode node)
1843 for (node = node.FirstChild; node != null; node = node.NextSibling)
1844 if (node.Name == Qmap)
1846 MSymbol name = node.Attributes[0].Value;
1847 Map map = new Map (name);
1849 for (XmlNode nd = node.FirstChild; nd != null; nd = nd.NextSibling)
1850 if (nd.Name == Qrule)
1852 XmlNode n = nd.FirstChild;
1853 if (n.Name != Qkeyseq)
1855 KeySeq keyseq = (KeySeq) KeySeq.parser (domain, n);
1856 Xex.Term[] actions = Xex.ParseTerms (domain, n.NextSibling);
1857 map.entries.Add (new Map.Entry (domain, keyseq, actions));
1862 private void parse_states (MPlist plist)
1864 for (; ! plist.IsEmpty; plist = plist.next)
1867 State state = new State (this, plist.Plist);
1868 states[state.name] = state;
1872 private void parse_states (XmlNode node)
1874 for (node = node.FirstChild; node != null; node = node.NextSibling)
1875 if (node.Name == Qstate)
1877 State state = new State (this, node);
1878 states[state.name] = state;
1882 private void parse_include (MPlist plist)
1884 if (! plist.IsPlist)
1886 MPlist p = plist.Plist;
1887 MSymbol language, name, subname;
1888 language = p.Symbol;
1891 name = subname = MSymbol.nil;
1897 subname = MSymbol.nil;
1902 MInputMethod im = MInputMethod.Find (language, name, subname);
1908 if (! plist.IsSymbol)
1910 MSymbol target_type = plist.Symbol;
1912 MSymbol target_name = MSymbol.nil;
1914 target_name = plist.Symbol;
1915 if (target_type == Mmacro)
1917 if (target_name == MSymbol.nil)
1918 im.domain.CopyFunc (domain);
1920 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1922 else if (target_type == Mmap)
1924 if (target_name == MSymbol.nil)
1926 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1927 maps[kv.Key] = kv.Value;
1932 if (im.maps.TryGetValue (target_name, out map))
1933 maps[target_name] = map;
1936 else if (target_type == Mstate)
1938 if (target_name == MSymbol.nil)
1940 foreach (KeyValuePair<Xex.Symbol, State> kv in im.states)
1941 states[kv.Key] = kv.Value;
1945 Xex.Symbol state_name = target_name.Name;
1947 if (im.states.TryGetValue (state_name, out state))
1948 states[state_name] = state;
1953 private Xex.Term parse_cond (MPlist plist)
1955 Xex.Term[] args = new Xex.Term[plist.Count];
1957 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1959 if (! plist.IsPlist)
1960 throw new Exception ("Invalid cond args: " + plist);
1961 MPlist p = plist.Plist;
1962 List<Xex.Term> arg = new List<Xex.Term> (parse_actions (p));
1963 args[i] = new Xex.Term (arg);
1965 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1968 private Xex.Term parse_insert (MPlist plist)
1973 arg = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1974 else if (plist.IsMText)
1975 arg = new Xex.Term ((string) plist.Text);
1976 else if (plist.IsInteger)
1977 arg = new Xex.Term (plist.Integer);
1978 else if (plist.IsPlist)
1980 MPlist pl = plist.Plist;
1981 args = new Xex.Term[pl.Count];
1983 for (i = 0; ! pl.IsEmpty; i++, pl = pl.next)
1986 args[i] = new Xex.Term ((string) pl.Text);
1987 else if (pl.IsPlist)
1989 List<Xex.Term> list = new List<Xex.Term> ();
1990 for (MPlist p = pl.Plist; ! p.IsEmpty; p = p.next)
1993 list.Add (new Xex.Term ((string) p.Text));
1995 throw new Exception ("Invalid candidates: " + p);
1999 throw new Exception ("Invalid candidates: " + pl);
2001 return new Xex.Term (domain, Qinsert_candidates, args);
2004 throw new Exception ("Invalid arg to insert: " + plist);
2005 args = new Xex.Term[1];
2007 return new Xex.Term (domain, Qinsert, args);
2010 private Xex.Term parse_select (MPlist plist)
2012 Xex.Term[] args = new Xex.Term[1];
2013 if (plist.IsInteger)
2014 args[0] = new Xex.Term (plist.Integer);
2015 else if (! plist.IsSymbol)
2016 throw new Exception ("Invalid arg to select: " + plist);
2017 else if (plist.Symbol.Name[0] == '@')
2018 args[0] = new Xex.Term (Selector.Get (plist.Symbol));
2020 args[0] = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
2021 return new Xex.Term (domain, (Xex.Symbol) Mselect.Name, args);
2024 private Xex.Term parse_funcall_with_marker (MPlist plist, MSymbol func)
2026 Xex.Term[] args = new Xex.Term[1];
2027 if (plist.IsInteger && func != Mmark)
2028 args[0] = new Xex.Term (plist.Integer);
2029 else if (plist.IsSymbol)
2030 args[0] = new Xex.Term (Marker.Get (plist.Symbol));
2032 throw new Exception ("Invalid arg to " + func + ": " + plist);
2033 return new Xex.Term (domain, (Xex.Symbol) func.Name, args);
2036 private Xex.Term parse_char_at (MSymbol name)
2038 Xex.Term[] args = new Xex.Term[1];
2039 args[0] = new Xex.Term (Marker.Get (name));
2040 return new Xex.Term (domain, Qchar_at, args);
2043 private Xex.Term parse_action (MPlist plist)
2047 MPlist p = plist.Plist;
2049 if (p.IsMText || p.IsPlist)
2050 return parse_insert (p);
2052 throw new Exception ("Invalid action: " + p);
2053 MSymbol name = p.Symbol;
2056 return parse_cond (p);
2057 if (name == Minsert)
2058 return parse_insert (p);
2059 if (name == Mselect)
2060 return parse_select (p);
2061 if (name == Mdelete || name == Mmove || name == Mmark)
2062 return parse_funcall_with_marker (p, name);
2063 if (name.Name[0] == '@')
2064 return parse_char_at (name);
2065 if (name == Mset || name == Madd || name == Msub
2066 || name == Mmul || name == Mdiv)
2069 throw new Exception ("Invalid action: " + p);
2070 Xex.Symbol varname = p.Symbol.Name;
2071 return new Xex.Term (domain, (Xex.Symbol) name.Name,
2072 varname, parse_actions (p.next));
2075 return new Xex.Term (domain, (Xex.Symbol) name.Name,
2078 else if (plist.IsMText || plist.IsInteger || plist.IsSymbol)
2079 return parse_insert (plist);
2081 throw new Exception ("Invalid action: " + plist);
2084 private Xex.Term[] parse_actions (MPlist plist)
2086 Xex.Term[] terms = new Xex.Term[plist.Count];
2088 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
2089 terms[i] = parse_action (plist);
2093 private void parse_macros (MPlist plist)
2095 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2098 MPlist p = pl.Plist;
2102 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
2104 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2107 MPlist p = pl.Plist;
2112 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
2113 parse_actions (p.next), false);
2117 private void parse_maps (MPlist plist)
2119 for (; ! plist.IsEmpty; plist = plist.next)
2122 MPlist pl = plist.Plist;
2126 Map map = new Map (pl.Symbol);
2127 maps[pl.Symbol] = map;
2128 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
2132 MPlist p = pl.Plist;
2135 keys = new KeySeq (p.Text);
2137 keys = new KeySeq (p.Plist);
2143 Xex.Term[] actions = parse_actions (p);
2144 map.entries.Add (new Map.Entry (domain, keys, actions));
2149 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
2152 ((Context) domain.context).insert (args[0]);
2156 private static Xex.Term Finsert_candidates (Xex.Domain domain,
2160 ((Context) domain.context).insert_candidates (args[0]);
2164 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
2167 Context ic = (Context) domain.context;
2168 Marker m = (Marker) args[0].Objval;
2170 return new Xex.Term (ic.char_at (m.Position (ic)));
2173 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
2176 ((Context) domain.context).delete ((int) args[0].Intval);
2180 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
2183 Candidates can = ((Context) domain.context).candidates;
2186 ((Selector) args[0].Objval).Select (can);
2190 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
2193 ((Context) domain.context).show ();
2197 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
2200 ((Context) domain.context).hide ();
2204 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
2208 ((Context) domain.context).move (args[0].Intval);
2211 Marker m = (Marker) args[0].Objval;
2212 Context ic = (Context) domain.context;
2213 ((Context) domain.context).move (m.Position (ic));
2218 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
2221 Marker m = (Marker) args[0].Objval;
2222 m.Mark ((Context) domain.context);
2226 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
2229 Context ic = (Context) domain.context;
2232 ic.pushback (args[0].Intval);
2233 else if (args[0].IsStr)
2234 ic.pushback (new KeySeq (args[0].Strval));
2236 ic.pushback ((KeySeq) args[0].Objval);
2240 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
2243 ((Context) domain.context).pop ();
2247 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
2250 int n = args.Length == 0 ? -2 : args[0].Intval;
2251 ((Context) domain.context).undo (n);
2255 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
2258 ((Context) domain.context).commit ();
2262 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
2265 ((Context) domain.context).commit ();
2266 args = new Xex.Term[2];
2267 args[0] = args[1] = Tcatch_tag;
2268 return Xex.Fthrow (domain, vari, args);
2271 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
2274 Context ic = (Context) domain.context;
2276 if (ic.im.states.TryGetValue (args[0].Symval, out state))
2277 ((Context) domain.context).shift (state);
2279 throw new Exception ("Unknown state: " + args[0].Symval);
2283 private static Xex.Term Fshiftback (Xex.Domain domain, Xex.Variable vari,
2286 ((Context) domain.context).shift (null);
2290 private static Xex.Term Fkey_count (Xex.Domain domain, Xex.Variable vari,
2293 return new Xex.Term (((Context) domain.context).key_head);
2296 private static Xex.Term Fsurrounding_flag (Xex.Domain domain,
2300 return new Xex.Term (((Context) domain.context).SurroundingFlag);
2303 public override string ToString ()
2305 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
2306 if (commands != null)
2308 str += " (commands";
2309 foreach (Command cmd in commands)
2313 if (var_names != null)
2315 str += " (variables";
2316 foreach (Xex.Symbol var in var_names)
2320 if (plugins != null)
2323 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
2324 str += " " + kv.Value;
2328 foreach (KeyValuePair<MSymbol, Map> kv in maps)
2329 str += " " + kv.Value;
2331 foreach (KeyValuePair<Xex.Symbol, State> kv in states)
2333 str += " (" + kv.Key + " " + kv.Value.keymap + ")";
2338 public class Context
2340 internal static Xex.Symbol Qcandidates_group_size
2341 = "candidates-group-size";
2342 internal MInputMethod im;
2343 private Dictionary<MSymbol, Callback> callbacks
2344 = new Dictionary<MSymbol, Callback> ();
2346 private MText produced;
2347 private bool active;
2348 private MText status;
2349 internal MText preedit;
2350 internal int cursor_pos;
2351 internal MPlist markers;
2352 internal Candidates candidates;
2353 private int candidate_from, candidate_to;
2354 private bool candidate_show;
2356 private List<State> state_list = new List<State> ();
2357 private Keymap keymap;
2359 // Sequence of input keys.
2360 internal KeySeq keys = new KeySeq ();
2362 // Index into KEYS specifying the next key to handle.
2363 internal int key_head;
2366 private int state_key_head;
2367 private object state_var_values;
2368 private int commit_key_head;
2369 private MText state_preedit;
2370 private int state_pos;
2371 internal MText preceding_text = new MText ();
2372 internal MText following_text = new MText ();
2374 // Set to false before calling the method 'handle_key', and set
2375 // to true when some key is unhandled.
2376 private bool key_unhandled;
2378 // The unhandled key. It has the meaning only when
2379 // 'key_unhandled' is true.
2380 private Key unhandled_key;
2382 internal Xex.Domain domain;
2384 internal ChangedStatus changed;
2386 static MPlist callback_arg = new MPlist ();
2388 static Xex.Term[] catch_args = new Xex.Term[2];
2390 private bool take_action (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);
2400 private bool call_callback (MSymbol name, MPlist arg)
2403 if (! callbacks.TryGetValue (name, out callback))
2405 return callback (this, arg);
2408 private bool get_surrounding_text (int len)
2410 if (len < 0 ? -len <= preceding_text.Length
2411 : len <= following_text.Length)
2413 callback_arg.Set (MSymbol.integer, len);
2414 if (! call_callback (Mget_surrounding_text, callback_arg)
2415 || ! callback_arg.IsMText)
2419 preceding_text = callback_arg.Text;
2420 return (-len <= preceding_text.Length);
2422 following_text = callback_arg.Text;
2423 return (len <= following_text.Length);
2426 internal int SurroundingFlag
2428 get { return (callbacks.ContainsKey (Mget_surrounding_text) ? 1 : 0); }
2431 internal int GetSurroundingChar (int pos)
2433 if (! get_surrounding_text (pos < 0 ? pos : pos + 1))
2436 return preceding_text[preceding_text.Length + pos];
2437 return following_text[pos];
2440 private void adjust_markers (int from, int to, object inserted)
2442 int ins = (inserted == null ? 0
2443 : inserted is int ? 1
2444 : ((MText) inserted).Length);
2445 int diff = ins - (to - from);
2447 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
2449 int pos = plist.Integer;
2453 plist.val = pos + diff;
2458 if (cursor_pos >= to)
2460 else if (cursor_pos > from)
2464 private void preedit_replace (int from, int to, int c)
2466 preedit.Del (from, to);
2467 preedit.Ins (from, c);
2468 adjust_markers (from, to, c);
2471 private void preedit_replace (int from, int to, MText mt)
2473 preedit[from, to] = mt;
2474 adjust_markers (from, to, mt);
2477 internal void insert (Xex.Term arg)
2480 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
2482 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
2483 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2486 private void update_candidate ()
2488 object candidate = candidates.Current;
2490 if (candidate is MText)
2492 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2493 candidate_to = candidate_from + ((MText) candidate).Length;
2497 preedit_replace (candidate_from, candidate_to, (int) candidate);
2498 candidate_to = candidate_from + 1;
2500 preedit.PushProp (candidate_from, candidate_to,
2502 cursor_pos = candidate_from;
2503 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2507 internal void insert_candidates (Xex.Term arg)
2510 Xex.Variable v = domain.GetVar (Qcandidates_group_size, false);
2513 column = v.Value.Intval;
2514 candidates = new Candidates (arg.Listval, column);
2515 candidate_from = candidate_to = cursor_pos;
2516 update_candidate ();
2519 internal void select (int n)
2521 if (candidates != null)
2523 candidates.Select (n);
2524 update_candidate ();
2528 internal int marker (MSymbol sym)
2530 int pos = cursor_pos;
2532 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2534 switch (sym.Name[0])
2536 case '<': pos = 0; break;
2537 case '>': pos = preedit.Length; break;
2538 case '-': pos = cursor_pos - 1; break;
2539 case '+': pos = cursor_pos + 1; break;
2544 preedit.FindProp (Mcandidates, pos - 1,
2551 if (cursor_pos < preedit.Length - 1)
2554 preedit.FindProp (Mcandidates, pos,
2558 pos = preedit.Length;
2561 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2566 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2568 pos = int.Parse (sym.Name.Substring (2));
2572 object val = markers.Get (sym);
2580 internal int char_at (int pos)
2587 if (preceding_text.Length < -pos)
2589 MPlist plist = new MPlist ();
2590 plist.Push (MSymbol.integer, pos);
2591 if (GetSurroundingText != null
2592 && GetSurroundingText (this, plist)
2594 && preceding_text.Length < plist.Text.Length)
2595 preceding_text = plist.Text;
2597 c = (-pos < preceding_text.Length
2598 ? preceding_text[preceding_text.Length + pos] : -1);
2600 else if (pos >= 0 && pos < preedit.Length)
2604 pos -= preedit.Length;
2605 if (pos >= following_text.Length)
2607 MPlist plist = new MPlist ();
2608 plist.Push (MSymbol.integer, pos + 1);
2609 if (GetSurroundingText != null
2610 && GetSurroundingText (this, plist)
2612 && following_text.Length < plist.Text.Length)
2613 following_text = plist.Text;
2615 c = (pos < following_text.Length ? following_text[pos] : -1);
2620 internal void delete (int pos)
2622 if (pos < cursor_pos)
2623 preedit_replace (pos, cursor_pos, null);
2625 preedit_replace (cursor_pos, pos, null);
2626 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2629 internal void show ()
2631 candidate_show = true;
2632 changed |= ChangedStatus.CandidateShow;
2635 internal void hide ()
2637 candidate_show = false;
2638 changed |= ChangedStatus.CandidateShow;
2641 internal void move (int pos)
2645 else if (pos > preedit.Length)
2646 pos = preedit.Length;
2647 if (pos != cursor_pos)
2650 changed |= ChangedStatus.Preedit;
2654 internal void pushback (int n)
2667 if (key_head > keys.keyseq.Count)
2668 key_head = keys.keyseq.Count;
2672 internal void pushback (KeySeq keyseq)
2676 if (key_head < keys.keyseq.Count)
2677 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2678 for (int i = 0; i < keyseq.keyseq.Count; i++)
2679 keys.keyseq.Add (keyseq.keyseq[i]);
2682 internal void pop ()
2684 if (key_head < keys.keyseq.Count)
2685 keys.keyseq.RemoveRange (key_head, 1);
2688 internal void undo (int n)
2691 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2693 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2697 internal void commit ()
2699 produced.Cat (preedit);
2701 changed |= ChangedStatus.Preedit;
2704 internal void shift (State state)
2710 if (state_list.Count > 1)
2711 state_list.RemoveAt (state_list.Count - 1);
2712 state = state_list[state_list.Count - 1];
2717 changed = state != state_list[state_list.Count - 1];
2719 state_list.Add (state);
2721 if (state_list.Count == 1)
2728 state_key_head = key_head;
2729 state_pos = cursor_pos;
2730 state_preedit = preedit.Dup ();
2731 state_var_values = domain.SaveValues ();
2734 status = state.title;
2737 this.changed |= ChangedStatus.StateTitle;
2738 if (state.enter_actions != null)
2739 take_action (state.enter_actions);
2744 internal void reset ()
2747 state_preedit.Del ();
2751 keys.keyseq.Clear ();
2752 key_head = commit_key_head = 0;
2753 state_list.Clear ();
2754 state_list.Add (im.initial_state);
2755 keymap = im.initial_state.keymap;
2756 key_head = state_key_head = 0;
2760 public Context (MInputMethod im)
2763 domain = new Xex.Domain (im.domain, this);
2767 public ChangedStatus Changed { get { return changed; } }
2769 public void AddCallback (MSymbol name, Callback callback)
2771 callbacks[name] = callback;
2774 internal object GetCandidates (out int column)
2777 if (cursor_pos == 0)
2779 Candidates candidates
2780 = (Candidates) preedit.GetProp (cursor_pos - 1, Mcandidates);
2781 if (candidates == null)
2783 column = candidates.Column;
2784 return candidates.Current;
2787 private void restore_state ()
2791 private bool handle_key ()
2793 State state = state_list[state_list.Count - 1];
2794 Keymap sub = keymap.Lookup (keys, ref key_head);
2799 if (keymap.map_actions != null)
2802 if (! take_action (keymap.map_actions))
2805 else if (keymap.submaps != null)
2807 for (int i = state_key_head; i < key_head; i++)
2808 preedit_replace (cursor_pos, cursor_pos,
2809 keys.keyseq[i].ToChar ());
2811 if (keymap.submaps == null)
2813 if (keymap.branch_actions != null)
2815 if (! take_action (keymap.branch_actions))
2818 if (keymap != state.keymap)
2824 if (keymap.branch_actions != null)
2826 if (! take_action (keymap.branch_actions))
2829 if (state == state_list[state_list.Count - 1])
2831 if (state == im.initial_state
2832 && key_head < keys.keyseq.Count)
2834 if (keymap != state.keymap)
2836 else if (keymap.branch_actions == null)
2837 shift (im.initial_state);
2843 public bool Toggle ()
2849 public bool UnhandledKey (out Key key)
2851 key = unhandled_key;
2852 return key_unhandled;
2855 public bool Produced (out MText mt)
2858 return (produced.Length > 0);
2862 // true: All keys are handled and there's no text to commit.
2863 // false: Some key is unhandled or there's a text to commit.
2864 // The caller should use methods UnhandledKey and Produced.
2866 public bool Filter (Key key)
2868 if (key == Key.Reload)
2870 changed = ChangedStatus.None;
2872 preceding_text.Del ();
2873 following_text.Del ();
2875 key_unhandled = false;
2876 keys.keyseq.Add (key);
2878 while (key_head < keys.keyseq.Count)
2880 if (! handle_key ())
2882 unhandled_key = keys.keyseq[key_head++];
2883 key_unhandled = true;
2889 keys.keyseq.RemoveRange (0, key_head);
2890 return (! key_unhandled && produced.Length == 0);