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 private static Xex.Symbol name = "keyseq";
356 public static Xex.Symbol Name { get { return name; } }
358 public List<Key> keyseq = new List<Key> ();
360 public override Xex.TermValue Clone ()
362 KeySeq ks = new KeySeq ();
363 ks.keyseq.InsertRange (0, keyseq);
369 public KeySeq (MPlist plist)
371 foreach (MPlist p in plist)
374 keyseq.Add (new Key (p.Symbol));
375 else if (p.IsInteger)
376 keyseq.Add (new Key ((char) p.Integer));
378 keyseq.Add (new Key (p.Plist));
380 throw new Exception ("Invalid Key Sequence: " + plist);
384 public KeySeq (MText mt) : base ()
386 for (int i = 0; i < mt.Length; i++)
387 keyseq.Add (new Key ((uint) mt[i]));
390 public KeySeq (List<Xex.Term> list)
392 int len = list.Count;
394 for (int i = 0; i < len; i++)
397 keyseq.Add (new Key (list[i].Intval));
398 else if (list[i].IsStr)
399 keyseq.Add (new Key (list[i].Strval));
400 else if (list[i].IsSymbol)
401 keyseq.Add (new Key ((string) list[i].Symval));
403 throw new Exception ("Invalid key: " + list[i]);
407 public static Xex.TermValue Parser (Xex.Domain domain, XmlNode node)
409 Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain);
410 return (term.IsStr ? new KeySeq ((MText) term.Strval)
411 : new KeySeq (term.Listval));
414 public override string ToString ()
417 foreach (Key key in keyseq)
418 if (key.HasModifier || key.ToChar () < 0x20)
421 foreach (Key k in keyseq)
425 mt.Cat (k.ToString ());
427 return (string) mt.Cat (")");
430 foreach (Key k in keyseq)
434 if (c == '\\' || c == '"')
438 return (string) mt.Cat ("\"");
445 public MText description;
446 internal List<KeySeq> keys;
448 public Command (MPlist p)
452 description = parse_description (p);
453 if (description == null)
454 description = "No description";
455 keys = new List<KeySeq> ();
456 for (p = p.next; ! p.IsEmpty; p = p.next)
459 keys.Add (new KeySeq (p.Text));
461 keys.Add (new KeySeq (p.Plist));
465 public Command (XmlNode node)
467 name = node.Attributes[0].Value;
468 keys = new List<KeySeq> ();
469 for (node = node.FirstChild; node != null; node = node.NextSibling)
471 if (node.Name == "description")
472 description = parse_description (node);
473 else if (node.Name == "keyseq")
474 keys.Add ((KeySeq) KeySeq.Parser (null, node));
478 public override string ToString ()
480 string str = "(" + name + " \"" + (string) description;
481 foreach (KeySeq keyseq in keys)
487 internal class Plugin
490 private Assembly assembly;
491 private Type plugin_type;
493 public Plugin (string name)
498 public MethodInfo GetMethod (Xex.Symbol name)
500 if (assembly == null)
502 assembly = Assembly.LoadFrom (name + ".dll");
503 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
506 MethodInfo info = plugin_type.GetMethod ((string) name);
508 throw new Exception ("Invalid plugin method: " + name);
512 public override string ToString ()
514 return String.Format ("(module {0}", name);
518 internal class PluginMethod : Xex.Function
520 private Plugin plugin;
521 private MethodInfo method_info;
522 object[] parameters = new object[2];
524 public PluginMethod (Plugin plugin, string name)
525 : base ((Xex.Symbol) name, 0, -1)
527 this.plugin = plugin;
530 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
533 args = (Xex.Term[]) args.Clone ();
534 for (int i = 0; i < args.Length; i++)
536 args[i] = args[i].Eval (domain);
540 if (method_info == null)
541 method_info = plugin.GetMethod (name);
542 parameters[0] = domain.context;
543 parameters[1] = args;
544 return (Xex.Term) method_info.Invoke (null, parameters);
548 internal abstract class Marker : Xex.TermValue
550 private static Xex.Symbol name = "marker";
551 public static Xex.Symbol Name { get { return name; } }
553 private MSymbol mname;
555 private Marker (MSymbol mname)
560 public abstract int Position (Context ic);
562 public virtual void Mark (Context ic)
564 throw new Exception ("Can't set predefined marker: " + mname);
566 public virtual int CharAt (Context ic)
568 int pos = Position (ic);
570 return ((pos >= 0 && pos < ic.preedit.Length) ? ic.preedit[pos]
574 public override string ToString ()
576 return "<marker>" + mname + "</marker>";
579 public static Xex.TermValue Parser (Xex.Domain domain, XmlNode node)
581 return Get ((MSymbol) node.InnerText);
584 public class Named : Marker
586 public Named (MSymbol mname) : base (mname) { }
588 public override int Position (Context ic)
590 MPlist p = ic.marker_positions.Find (mname);
591 return (p == null ? 0 : p.Integer);
594 public override void Mark (Context ic)
596 ic.marker_positions.Put (mname, ic.cursor_pos);
600 public class Predefined : Marker
604 public Predefined (char tag) : base ("@" + tag) { this.tag = tag; }
606 public override int Position (Context ic)
610 case '>': return ic.preedit.Length;
611 case '-': return ic.cursor_pos - 1;
612 case '+': return ic.cursor_pos + 1;
614 if (ic.cursor_pos > 0)
616 int pos = ic.cursor_pos;
618 ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to);
623 if (ic.cursor_pos < ic.preedit.Length - 1)
625 int pos = ic.cursor_pos;
627 ic.preedit.FindProp (Mcandidates, pos, out from, out pos);
630 return ic.preedit.Length;
637 public class PredefinedAbsolute : Marker
641 public PredefinedAbsolute (MSymbol mname) : base (mname)
643 if (! int.TryParse (((string) mname).Substring (1), out pos))
644 throw new Exception ("Invalid marker name: " + mname);
647 public override int Position (Context ic)
649 return (pos < ic.preedit.Length ? pos : ic.preedit.Length);
653 public class PredefinedSurround : Marker
655 private int distance;
657 public PredefinedSurround (MSymbol mname) : base (mname)
659 if (! int.TryParse (((string) name).Substring (1), out distance))
660 throw new Exception ("Invalid marker name: " + mname);
665 public override int Position (Context ic)
667 return ic.cursor_pos + distance;
670 public override int CharAt (Context ic)
672 int pos = ic.cursor_pos + distance;
674 return ic.GetSurroundingChar (pos);
675 else if (pos >= ic.preedit.Length)
676 return ic.GetSurroundingChar (pos - ic.preedit.Length);
677 return ic.preedit[pos];
681 static internal Dictionary<MSymbol,Predefined> predefineds;
685 predefineds = new Dictionary<MSymbol, Predefined> ();
686 predefineds ["@<"] = predefineds["@first"] = new Predefined ('<');
687 predefineds ["@>"] = predefineds["@last"] = new Predefined ('>');
688 predefineds ["@-"] = predefineds["@previous"] = new Predefined ('-');
689 predefineds ["@+"] = predefineds["@next"] = new Predefined ('+');
690 predefineds ["@["] = predefineds["@previous-candidate-change"]
691 = new Predefined ('[');
692 predefineds ["@]"] = predefineds["@next-candidate-change"]
693 = new Predefined (']');
696 public static Marker Get (MSymbol mname)
698 string str = mname.Name;
702 if (predefineds.TryGetValue (mname, out pred))
705 throw new Exception ("Invalid marker name: " + mname);
706 if (Char.IsDigit (str[1]))
707 return new PredefinedAbsolute (mname);
708 if (str.Length == 2 || mname == Mat_minus_zero
709 || ! (str[1] == '-' || str[1] == '+'))
710 throw new Exception ("Invalid marker name: " + mname);
711 return new PredefinedSurround (mname);
713 return new Named (mname);
717 internal class Candidates
724 public Block (int index, Xex.Term term)
728 Data = (MText) term.Strval;
731 MPlist plist = new MPlist ();
733 foreach (Xex.Term t in term.Listval)
734 p = p.Add (MSymbol.mtext, (MText) t.Strval);
739 public Block (int index, MPlist plist)
744 else if (plist.IsPlist)
747 throw new Exception ("Invalid candidate: " + plist);
752 get { return (Data is MText
753 ? ((MText) Data).Length
754 : ((MPlist) Data).Count); }
757 public object this[int i]
760 if (Data is MText) return ((MText) Data)[i];
761 return ((MPlist) Data)[i];
766 private Block[] blocks;
768 private int index = 0;
769 public object[] group;
771 private bool IsFixed { get { return group != null; } }
774 Block last = blocks[blocks.Length - 1];
775 return last.Index + last.Count; }
779 get { return (IsFixed ? index % group.Length
780 : index - blocks[row].Index); }
783 public object Group {
784 get { return (IsFixed ? group : blocks[row].Data); }
787 public int GroupLength
792 int nitems = group.Length;
793 int start = index - (index % nitems);
795 return (start + nitems <= total ? nitems : total - start);
797 return blocks[row].Count;
801 public object Current {
803 return (IsFixed ? group[index % group.Length]
804 : blocks[row][index - blocks[row].Index]);
808 public Candidates (MPlist list, int column)
810 int nblocks = list.Count;
812 blocks = new Block[nblocks];
813 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
814 start += (blocks[i] = new Block (index, list)).Count;
817 group = new object[column];
822 public Candidates (Xex.Term[] candidates, int column)
824 int nblocks = candidates.Length;
826 blocks = new Block[nblocks];
827 for (int i = 0, start = 0; i < nblocks; i++)
828 start += (blocks[i] = new Block (index, candidates[i])).Count;
831 group = new object[column];
836 public static void Detach (Context ic)
838 ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
839 ic.candidates = null;
840 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
844 // Fill the array "group" by candidates stating from START.
845 // START must be a multiple of "column". Return the number of
846 // valid candidates in "group".
848 private int fill_group (int start)
850 int nitems = group.Length;
855 while (start < b.Index)
858 while (start >= b.Index + b.Count)
864 for (int i = 0; i < nitems; i++, start++)
869 if (r == blocks.Length)
880 // Update "row" to what contains the first candidate of
881 // the previous candidate-group, update "current_index", and
882 // update "group" if necessary. Return the previous
883 // candidate-group. Set NITEMS to the number of valid
884 // candidates contained in that group.
886 public int PrevGroup ()
893 nitems = group.Length;
894 if ((index -= col + nitems) < 0)
895 index = (Total / nitems) * nitems;
896 nitems = fill_group (index);
900 row = row > 0 ? row-- : blocks.Length - 1;
901 nitems = blocks[row].Count;
902 index = blocks[row].Index;
904 index += col < nitems ? col : nitems - 1;
908 public int NextGroup ()
915 nitems = group.Length;
916 if ((index += nitems - col) >= Total)
918 nitems = fill_group (index);
922 row = row < blocks.Length - 1 ? row + 1 : 0;
923 nitems = blocks[row].Count;
924 index = blocks[row].Count;
926 index += col < nitems ? col : nitems - 1;
936 int nitems = PrevGroup ();
937 index += col < nitems - 1 ? col : nitems - 1;
946 int nitems = GroupLength;
948 if (col == nitems - 1)
950 nitems = NextGroup ();
964 index += GroupLength - (Column + 1);
967 public object Select (int col)
969 int maxcol = GroupLength - 1;
972 index = index - Column + col;
976 public object Select (Selector selector)
978 switch (selector.Tag)
980 case '<': First (); break;
981 case '>': Last (); break;
982 case '-': Prev (); break;
983 case '+': Next (); break;
984 case '[': PrevGroup (); break;
985 case ']': NextGroup (); break;
991 public override string ToString ()
993 return (String.Format ("<candidates row={0} col={1}>", row, index)
999 internal class Selector : Xex.TermValue
1001 private static Xex.Symbol name = "selector";
1002 public static Xex.Symbol Name { get { return name; } }
1004 static new Dictionary<MSymbol, Selector> selectors;
1008 selectors = new Dictionary<MSymbol, Selector> ();
1009 selectors ["@<"] = selectors["@first"] = new Selector ('<');
1010 selectors ["@="] = selectors["@current"] = new Selector ('=');
1011 selectors ["@>"] = selectors["@last"] = new Selector ('>');
1012 selectors ["@-"] = selectors["@previous"] = new Selector ('-');
1013 selectors ["@+"] = selectors["@next"] = new Selector ('+');
1014 selectors ["@["] = selectors["@previous-candidate-change"]
1015 = new Selector ('[');
1016 selectors ["@]"] = selectors["@next-candidate-change"]
1017 = new Selector (']');
1020 private readonly char tag;
1022 public char Tag { get { return tag; } }
1024 private Selector (char tag) { this.tag = tag; }
1026 public static Xex.TermValue Parser (Xex.Domain domain, XmlNode node)
1028 return Get ((MSymbol) node.InnerText);
1031 public static Xex.TermValue Get (MSymbol name)
1034 if (! selectors.TryGetValue (name, out selector))
1035 throw new Exception ("Invalid selector name: " + name);
1042 public MSymbol name;
1043 public List<Entry> entries = new List<Entry> ();
1045 public Map (MSymbol name) { this.name = name; }
1049 public KeySeq keyseq;
1050 public Xex.Term[] actions;
1052 public Entry (Xex.Domain domain, KeySeq keyseq, Xex.Term[] actions)
1054 this.keyseq = keyseq;
1055 this.actions = actions;
1059 public override string ToString ()
1061 string str = "(" + name;
1062 foreach (Entry e in entries)
1063 str += " " + e.keyseq.ToString ();
1068 internal class Keymap
1070 public Dictionary<Key, Keymap> submaps;
1071 public Xex.Term[] map_actions, branch_actions;
1073 public Keymap () { }
1075 public void Add (KeySeq keys, int index,
1076 Xex.Term[] map_actions, Xex.Term[] branch_actions)
1078 if (index == keys.keyseq.Count)
1080 this.map_actions = map_actions;
1081 this.branch_actions = branch_actions;
1085 Key key = keys.keyseq[index];
1088 if (submaps == null)
1089 submaps = new Dictionary<Key, Keymap> ();
1091 submaps.TryGetValue (key, out sub);
1093 submaps[key] = sub = new Keymap ();
1094 sub.Add (keys, index + 1, map_actions, branch_actions);
1098 public void AddMap (Map map, Xex.Term[] branch_actions)
1100 foreach (Map.Entry entry in map.entries)
1101 Add (entry.keyseq, 0, entry.actions, branch_actions);
1104 public Keymap Lookup (KeySeq keys, ref int index)
1108 if (index < keys.keyseq.Count
1110 && submaps.TryGetValue (keys.keyseq[index], out sub))
1113 return sub.Lookup (keys, ref index);
1118 private void describe (MText mt, KeySeq keyseq)
1120 if (map_actions != null || branch_actions != null)
1124 mt.Cat ('(').Cat (keyseq.ToString ());
1125 if (map_actions != null)
1126 foreach (Xex.Term term in map_actions)
1127 mt.Cat (' ').Cat (term.ToString ());
1128 if (branch_actions != null)
1129 foreach (Xex.Term term in branch_actions)
1130 mt.Cat (' ').Cat (term.ToString ());
1133 if (submaps != null)
1134 foreach (KeyValuePair<Key, Keymap> kv in submaps)
1136 keyseq.keyseq.Add (kv.Key);
1137 kv.Value.describe (mt, keyseq);
1138 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
1142 public override string ToString ()
1145 KeySeq keyseq = new KeySeq ();
1147 describe (mt, keyseq);
1152 internal class State
1154 public MSymbol name;
1156 public Xex.Term[] enter_actions, fallback_actions;
1157 public Keymap keymap = new Keymap ();
1159 public State (MSymbol name, MText title)
1165 public State (MInputMethod im, XmlNode node)
1167 this.name = node.Attributes[Qsname].Value;
1168 XmlAttribute attr = node.Attributes[Qtitle];
1170 title = (MText) attr.Value;
1173 keymap = new Keymap ();
1174 for (node = node.FirstChild; node != null; node = node.NextSibling)
1176 if (node.Name == Qstate_hook)
1177 enter_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1178 else if (node.Name == Qcatch_all_branch)
1179 fallback_actions = Xex.ParseTerms (im.domain, node.FirstChild);
1180 else if (node.Name == Qbranch)
1182 MSymbol mapname = node.Attributes[Qmname].Value;
1184 if (im.maps.TryGetValue (mapname, out map))
1185 keymap.AddMap (map, Xex.ParseTerms (im.domain,
1188 throw new Exception ("Unknown map: " + mapname);
1193 public State (MInputMethod im, MPlist plist)
1195 if (! plist.IsSymbol)
1196 throw new Exception ("Invalid state: " + plist);
1197 this.name = plist.Symbol;
1206 keymap = new Keymap ();
1207 for (; ! plist.IsEmpty; plist = plist.next)
1209 if (! plist.IsPlist)
1210 throw new Exception ("Invalid branch: " + plist);
1211 MPlist p = plist.Plist;
1213 throw new Exception ("Invalid branch: " + p);
1214 MSymbol mapname = p.Symbol;
1215 if (mapname == MSymbol.t)
1216 enter_actions = im.parse_actions (p.next, false);
1217 else if (mapname == MSymbol.nil)
1218 fallback_actions = im.parse_actions (p.next, false);
1222 if (im.maps.TryGetValue (mapname, out map))
1223 keymap.AddMap (map, im.parse_actions (p.next, false));
1225 throw new Exception ("Unknown map: " + mapname);
1230 public override string ToString ()
1232 MText mt = "(" + name;
1235 mt.Cat (" \"" + title + "\"");
1236 mt.Cat (keymap.ToString ());
1237 return (string) mt + ")";
1242 internal Xex.Domain domain;
1244 protected LoadStatus load_status = LoadStatus.None;
1245 protected MDatabase.Tag tag;
1246 private MDatabase mdb;
1248 private MText description;
1249 internal MText title;
1250 internal Command[] commands;
1251 internal Xex.Symbol[] var_names;
1252 internal Dictionary<MSymbol, Plugin> plugins;
1253 internal Dictionary<MSymbol, Map> maps;
1254 internal MPlist states;
1256 static MInputMethod ()
1258 im_domain.DefType (typeof (KeySeq));
1259 im_domain.DefType (typeof (Marker));
1260 im_domain.DefType (typeof (Selector));
1262 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1263 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, -1);
1264 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1265 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1266 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
1267 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
1268 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1269 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1270 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1271 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1272 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1273 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1274 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1275 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1276 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
1277 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1278 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
1279 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag",
1282 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
1283 List<MDatabase> list = MDatabase.List (tag);
1284 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
1285 foreach (MDatabase mdb in list)
1286 im_table[mdb.tag] = new MInputMethod (mdb.tag);
1290 private MInputMethod (MDatabase.Tag tag)
1293 domain = new Xex.Domain (tag[1].Name, im_domain, null);
1296 // Instance Properties
1297 public MSymbol Language { get { return tag[1]; } }
1298 public MSymbol Name { get { return tag[2]; } }
1299 public MSymbol SubName { get { return tag[3]; } }
1301 public bool Info (out MText description,
1303 out Xex.Variable[] variables,
1304 out Command[] commands)
1306 if ((load_status & LoadStatus.Header) != LoadStatus.Header
1307 && ! load_header ())
1315 description = this.description;
1317 if (var_names == null)
1321 variables = new Xex.Variable[var_names.Length];
1323 foreach (Xex.Symbol name in var_names)
1324 variables[i++] = domain.GetVar (name, false);
1326 commands = this.commands;
1330 public static MInputMethod Find (MSymbol language, MSymbol name)
1332 return Find (language, name, MSymbol.nil);
1335 public static MInputMethod Find (MSymbol language, MSymbol name,
1338 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
1342 return (im_table.TryGetValue (tag, out im) ? im : null);
1345 private bool Open ()
1347 return ((load_status == LoadStatus.Full) || load_body ());
1350 public static MInputMethod[] List ()
1352 MInputMethod[] array = new MInputMethod[im_table.Count];
1355 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
1356 array[i++] = kv.Value;
1360 private bool load_header ()
1362 mdb = MDatabase.Find (tag);
1366 if (mdb.Format == MSymbol.plist)
1367 load (mdb.Load (Mmap), false);
1370 XmlDocument doc = new XmlDocument (Xex.Symbol.NameTable);
1371 if (! mdb.Load (doc, Mmap_list))
1372 throw new Exception ("Load error" + mdb.tag);
1373 load (doc.DocumentElement, false);
1375 } catch (Exception e) {
1376 Console.WriteLine ("{0}\n", e);
1377 load_status = LoadStatus.Error;
1380 load_status |= LoadStatus.Header;
1384 private bool load_body ()
1386 mdb = MDatabase.Find (tag);
1390 if (mdb.Format == MSymbol.plist)
1391 load (mdb.Load (), true);
1394 XmlDocument doc = new XmlDocument (Xex.Symbol.NameTable);
1395 if (! mdb.Load (doc))
1396 throw new Exception ("Load error" + mdb.tag);
1397 load (doc.DocumentElement, true);
1399 } catch (Exception e) {
1400 Console.WriteLine (e);
1401 load_status = LoadStatus.Error;
1404 load_status = LoadStatus.Full;
1408 private void add_default_state ()
1410 MSymbol Qinit = "init";
1411 State state = new State (Qinit, title);
1412 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1413 state.keymap.AddMap (kv.Value, null);
1414 states.Add (Qinit, state);
1417 private void load (MPlist plist, bool full)
1419 maps = new Dictionary<MSymbol, Map> ();
1420 states = new MPlist ();
1422 for (; ! plist.IsEmpty; plist = plist.next)
1425 MPlist pl = plist.Plist;
1428 MSymbol sym = pl.Symbol;
1431 if (sym == Mdescription)
1432 description = parse_description (pl);
1433 else if (sym == Mtitle)
1438 else if (sym == Mvariable)
1439 parse_variables (pl);
1440 else if (sym == Mcommand)
1441 parse_commands (pl);
1446 else if (sym == Minclude)
1448 else if (sym == Mmacro)
1450 else if (sym == Mmap)
1452 else if (sym == Mstate)
1457 if (description == null)
1458 description = (MText) "No description";
1460 title = new MText (tag[2].Name);
1461 if (commands == null)
1462 commands = new Command[0];
1466 add_default_state ();
1469 private void load (XmlNode node, bool full)
1471 bool skip_header = load_status == LoadStatus.Header;
1473 maps = new Dictionary<MSymbol, Map> ();
1474 states = new MPlist ();
1476 if (node.NodeType == XmlNodeType.Document)
1477 node = node.FirstChild;
1478 while (node.NodeType != XmlNodeType.Element)
1479 node = node.NextSibling;
1480 for (node = node.FirstChild; node != null; node = node.NextSibling)
1482 if (node.NodeType != XmlNodeType.Element)
1486 if (node.Name == "description")
1487 description = parse_description (node);
1488 else if (node.Name == "title")
1489 title = parse_title (node);
1490 else if (node.Name == "variable-list")
1491 parse_variables (node);
1492 else if (node.Name == "command-list")
1493 parse_commands (node);
1497 if (node.Name == "module-list")
1498 parse_plugins (node);
1499 else if (node.Name == "macro-list")
1500 parse_macros (node);
1501 else if (node.Name == "map-list")
1503 else if (node.Name == "state-list")
1504 parse_states (node);
1507 if (description == null)
1508 description = (MText) "No description";
1510 title = new MText (tag[2].Name);
1511 if (commands == null)
1512 commands = new Command[0];
1516 add_default_state ();
1519 private static MText parse_description (MPlist plist)
1525 plist = plist.Plist;
1526 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1527 && plist.next.IsMText)
1528 return plist.next.Text;
1533 private static MText parse_description (XmlNode node)
1535 if (node.HasChildNodes)
1536 node = node.FirstChild;
1537 return node.InnerText;
1540 private static MText parse_title (XmlNode node)
1542 return node.InnerText;
1545 private Xex.Variable get_global_var (Xex.Symbol name)
1547 if (im_global == null || this != im_global)
1550 new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil, "global");
1551 im_global = im_table[tag];
1552 if (! im_global.Open ())
1553 throw new Exception ("Failed to load global");
1555 return im_global.domain.GetVar (name, false);
1558 private void parse_variables (MPlist plist)
1560 var_names = new Xex.Symbol[plist.Count];
1562 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1564 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1565 throw new Exception ("Invalid variable: " + plist);
1567 MPlist p = plist.Plist;
1568 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1569 var_names[i] = name;
1571 MText mt = parse_description (p);
1572 string desc = mt == null ? null : (string) mt;
1575 Xex.Variable vari = get_global_var (name);
1577 domain.Defvar (vari);
1586 int nrange = p.Count;
1587 range = new int[nrange * 2];
1588 for (int j = 0; j < nrange; j++)
1592 MPlist p0 = p.Plist;
1594 if (! p0.IsInteger || ! p0.next.IsInteger)
1595 throw new Exception ("Invalid range: " + p0);
1596 range[j * 2] = p0.Integer;
1597 range[j * 2 + 1] = p0.next.Integer;
1599 else if (p.IsInteger)
1600 range[j * 2] = range[j * 2 + 1] = p.Integer;
1602 throw new Exception ("Invalid range: " + p);
1605 domain.DefvarInt (name, n, desc, range);
1609 string str = (string) p.Text;
1610 string[] range = null;
1615 range = new string[p.Count];
1616 for (int j = 0; j < range.Length; j++)
1619 range[j] = (string) p.Text;
1621 throw new Exception ("Invalid range: " + p);
1624 domain.DefvarStr (name, str, desc, range);
1626 else if (p.IsSymbol)
1628 Xex.Symbol sym = p.Symbol.Name;
1636 range = new Xex.Symbol[p.Count];
1637 for (int j = 0; j < range.Length; j++)
1640 range[j] = p.Symbol.Name;
1642 throw new Exception ("Invalid range: " + p);
1645 domain.DefvarSym (name, sym, desc, range);
1647 else if (! p.IsEmpty)
1648 throw new Exception ("Invalid variable type: " + p.val);
1652 private void parse_variables (XmlNode node)
1654 XmlNodeList node_list = node.ChildNodes;
1656 var_names = new Xex.Symbol[node_list.Count];
1657 for (int i = 0; i < node_list.Count; i++)
1659 Xex.Symbol name = node_list[i].Attributes[0].Value;
1660 Xex.Variable vari = get_global_var (name);
1662 domain.Defvar (vari);
1663 domain.Defvar (node_list[i]);
1664 var_names[i] = name;
1668 private void parse_commands (MPlist plist)
1670 commands = new Command[plist.Count];
1672 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1673 if (plist.IsPlist && plist.Plist.IsSymbol)
1674 commands[i++] = new Command (plist.Plist);
1677 private void parse_commands (XmlNode node)
1679 XmlNodeList node_list = node.ChildNodes;
1681 commands = new Command[node_list.Count];
1682 for (int i = 0; i < node_list.Count; i++)
1684 if (node_list[i].NodeType == XmlNodeType.Element)
1685 commands[i] = new Command (node_list[i]);
1689 private void parse_plugins (MPlist plist)
1691 plugins = new Dictionary<MSymbol, Plugin> ();
1693 for (; ! plist.IsEmpty; plist = plist.Next)
1695 MPlist p = plist.Plist;
1696 MSymbol sym = p.Symbol;
1697 Plugin plugin = new Plugin (sym.Name);
1699 for (p = p.next; ! p.IsEmpty; p = p.next)
1701 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1702 domain.Defun (func);
1707 private void parse_plugins (XmlNode node)
1709 plugins = new Dictionary<MSymbol, Plugin> ();
1711 foreach (XmlNode n in node.ChildNodes)
1713 Plugin plugin = new Plugin (n.Attributes[0].Value);
1714 foreach (XmlNode nn in n.ChildNodes)
1716 Xex.Function func = new PluginMethod (plugin,
1717 nn.Attributes[0].Value);
1718 domain.Defun (func);
1723 private void parse_include (XmlNode node)
1726 MSymbol language, name, subname;
1727 MSymbol part, section;
1728 node = node.FirstChild;
1729 n = node.FirstChild;
1730 language = n.InnerText;
1735 subname = n.InnerText;
1737 subname = MSymbol.nil;
1738 node = node.NextSibling;
1739 part = node.InnerText;
1740 node = node.NextSibling;
1742 section = node.InnerText;
1744 section = MSymbol.nil;
1745 include_part (language, name, subname, part, section);
1748 private void parse_macros (XmlNode node)
1750 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1751 if (nn.NodeType == XmlNodeType.Element)
1753 if (nn.Name == Xex.Qdefun)
1754 domain.Defun (nn, true);
1755 else if (nn.Name == Qxi_include)
1758 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1759 if (nn.NodeType == XmlNodeType.Element
1760 && nn.Name == Xex.Qdefun)
1761 domain.Defun (nn, false);
1764 private void parse_maps (XmlNode node)
1766 for (node = node.FirstChild; node != null; node = node.NextSibling)
1768 if (node.Name == Qmap)
1770 MSymbol name = node.Attributes[0].Value;
1771 Map map = new Map (name);
1773 for (XmlNode nd = node.FirstChild; nd != null;
1774 nd = nd.NextSibling)
1775 if (nd.Name == Qrule)
1777 XmlNode n = nd.FirstChild;
1778 if (n.Name != Qkeyseq)
1780 KeySeq keyseq = (KeySeq) KeySeq.Parser (domain, n);
1781 Xex.Term[] actions = Xex.ParseTerms (domain, n.NextSibling);
1782 map.entries.Add (new Map.Entry (domain, keyseq, actions));
1785 else if (node.Name == Qxi_include)
1786 parse_include (node);
1790 private void parse_states (MPlist plist)
1792 for (; ! plist.IsEmpty; plist = plist.next)
1795 State state = new State (this, plist.Plist);
1796 states.Add (state.name, state);
1800 private void parse_states (XmlNode node)
1802 for (node = node.FirstChild; node != null; node = node.NextSibling)
1804 if (node.Name == Qstate)
1806 State state = new State (this, node);
1807 states.Add (state.name, state);
1809 else if (node.Name == Qxi_include)
1810 parse_include (node);
1814 private void include_part (MSymbol language, MSymbol name, MSymbol subname,
1815 MSymbol part, MSymbol section)
1817 MInputMethod im = MInputMethod.Find (language, name, subname);
1824 if (section == MSymbol.nil)
1825 im.domain.CopyFunc (domain);
1827 im.domain.CopyFunc (domain, (Xex.Symbol) section.Name);
1829 else if (part == Mmap)
1831 if (section == MSymbol.nil)
1833 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1834 maps[kv.Key] = kv.Value;
1839 if (im.maps.TryGetValue (section, out map))
1840 maps[section] = map;
1843 else if (part == Mstate)
1845 if (section == MSymbol.nil)
1847 for (MPlist p = im.states; ! p.IsEmpty; p = p.next)
1848 states.Add (p.Key, p.Val);
1852 MSymbol state_name = (string) section.Name;
1853 State state = (State) im.states.Get (state_name);
1855 states.Add (state.name, state);
1860 private void parse_include (MPlist plist)
1862 if (! plist.IsPlist)
1864 MPlist p = plist.Plist;
1865 MSymbol language, name, subname;
1866 language = p.Symbol;
1869 name = subname = MSymbol.nil;
1875 subname = MSymbol.nil;
1880 if (! plist.IsSymbol)
1882 MSymbol part = plist.Symbol;
1884 MSymbol section = MSymbol.nil;
1886 section = plist.Symbol;
1887 include_part (language, name, subname, part, section);
1890 private Xex.Term parse_cond (MPlist plist)
1892 Xex.Term[] args = new Xex.Term[plist.Count];
1894 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1896 if (! plist.IsPlist)
1897 throw new Exception ("Invalid cond args: " + plist);
1898 MPlist p = plist.Plist;
1899 List<Xex.Term> arg = new List<Xex.Term> ();
1900 arg.Add (parse_action (p, true));
1901 for (p = p.next; ! p.IsEmpty; p = p.next)
1902 arg.Add (parse_action (p, false));
1903 args[i] = new Xex.Term (arg);
1905 return new Xex.Term (domain, Qcond, args);
1908 private Xex.Term parse_insert (MPlist plist)
1913 arg = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1914 else if (plist.IsMText)
1915 arg = new Xex.Term ((string) plist.Text);
1916 else if (plist.IsInteger)
1917 arg = new Xex.Term (plist.Integer);
1918 else if (plist.IsPlist)
1920 MPlist pl = plist.Plist;
1922 args = new Xex.Term[pl.Count];
1924 for (i = 0; ! pl.IsEmpty; i++, pl = pl.next)
1927 args[i] = new Xex.Term ((string) pl.Text);
1928 else if (pl.IsPlist)
1930 List<Xex.Term> list = new List<Xex.Term> ();
1931 for (MPlist p = pl.Plist; ! p.IsEmpty; p = p.next)
1934 list.Add (new Xex.Term ((string) p.Text));
1936 throw new Exception ("Invalid candidates: " + p);
1940 throw new Exception ("Invalid candidates: " + pl);
1942 return new Xex.Term (domain, Qinsert_candidates, args);
1945 throw new Exception ("Invalid arg to insert: " + plist);
1946 args = new Xex.Term[1];
1948 return new Xex.Term (domain, Qinsert, args);
1951 private Xex.Term parse_select (MPlist plist)
1953 Xex.Term[] args = new Xex.Term[1];
1954 if (plist.IsInteger)
1955 args[0] = new Xex.Term (plist.Integer);
1956 else if (! plist.IsSymbol)
1957 throw new Exception ("Invalid arg to select: " + plist);
1958 else if (plist.Symbol.Name[0] == '@')
1959 args[0] = new Xex.Term (Selector.Get (plist.Symbol));
1961 args[0] = new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
1962 return new Xex.Term (domain, Qselect, args);
1965 private Xex.Term parse_funcall_with_marker (MPlist plist, Xex.Symbol func)
1967 Xex.Term[] args = new Xex.Term[1];
1968 if (plist.IsInteger && func != Qmark)
1969 args[0] = new Xex.Term (plist.Integer);
1970 else if (plist.IsSymbol)
1971 args[0] = new Xex.Term (Marker.Get (plist.Symbol));
1973 throw new Exception ("Invalid arg to " + func + ": " + plist);
1974 return new Xex.Term (domain, func, args);
1977 private Xex.Term parse_char_at (MSymbol name)
1979 Xex.Term[] args = new Xex.Term[1];
1980 args[0] = new Xex.Term (Marker.Get (name));
1981 return new Xex.Term (domain, Qchar_at, args);
1984 private Xex.Term parse_shift (MPlist plist)
1986 Xex.Term[] args = new Xex.Term[1];
1987 if (! plist.IsSymbol)
1988 throw new Exception ("Invalid arg to shift: " + plist);
1989 args[0] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1990 return new Xex.Term (domain, Qshift, args);
1993 private Xex.Term parse_action (MPlist plist, bool as_funarg)
1997 MPlist p = plist.Plist;
1999 if (p.IsMText || p.IsPlist)
2000 return parse_insert (plist);
2002 throw new Exception ("Invalid action: " + p);
2003 MSymbol sym = p.Symbol;
2004 Xex.Symbol name = sym.Name;
2007 return parse_cond (p);
2008 if (name == Qinsert)
2009 return parse_insert (p);
2010 if (name == Qselect)
2011 return parse_select (p);
2012 if (name == Qdelete || name == Qmove || name == Qmark)
2013 return parse_funcall_with_marker (p, name);
2015 return parse_shift (p);
2016 if (name == Qset || name == Qadd || name == Qsub
2017 || name == Qmul || name == Qdiv)
2020 throw new Exception ("Invalid action: " + p);
2021 Xex.Symbol varname = p.Symbol.Name;
2022 Xex.Term[] args = new Xex.Term[1];
2023 args[0] = parse_action (p.next, true);
2024 return new Xex.Term (domain, name, varname, args);
2030 else if (name == Qhide)
2031 name = Qhide_candidates;
2032 else if (name == Qshow)
2033 name = Qshow_candidates;
2035 return new Xex.Term (domain, name, null);
2037 return new Xex.Term (domain, name, parse_actions (p, true));
2040 else if (plist.IsSymbol)
2042 if (plist.Symbol == Matat)
2043 return new Xex.Term (domain, Qkey_count, null);
2044 if (plist.Symbol == Mat_minus_zero)
2045 return new Xex.Term (domain, Qsurrounding_text_flag, null);
2046 if (plist.Symbol.Name[0] == '@')
2047 return parse_char_at (plist.Symbol);
2048 return new Xex.Term (domain, (Xex.Symbol) plist.Symbol.Name);
2050 else if (plist.IsMText)
2051 return (as_funarg ? new Xex.Term ((string) plist.Text)
2052 : parse_insert (plist));
2053 else if (plist.IsInteger)
2054 return (as_funarg ? new Xex.Term (plist.Integer)
2055 : parse_insert (plist));
2057 throw new Exception ("Invalid action: " + plist);
2060 private Xex.Term[] parse_actions (MPlist plist, bool as_funarg)
2062 Xex.Term[] terms = new Xex.Term[plist.Count];
2064 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
2065 terms[i] = parse_action (plist, as_funarg);
2069 private void parse_macros (MPlist plist)
2071 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2074 MPlist p = pl.Plist;
2077 domain.Defun ((Xex.Symbol) p.Symbol.Name, null, null, true);
2079 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
2082 MPlist p = pl.Plist;
2086 domain.Defun ((Xex.Symbol) p.Symbol.Name, null,
2087 parse_actions (p.next, false), false);
2091 private void parse_maps (MPlist plist)
2093 for (; ! plist.IsEmpty; plist = plist.next)
2096 MPlist pl = plist.Plist;
2100 Map map = new Map (pl.Symbol);
2101 maps[pl.Symbol] = map;
2102 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
2106 MPlist p = pl.Plist;
2109 keys = new KeySeq (p.Text);
2111 keys = new KeySeq (p.Plist);
2116 = p.IsEmpty ? null : parse_actions (p, false);
2117 map.entries.Add (new Map.Entry (domain, keys, actions));
2122 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
2126 ((Context) domain.context).insert (args[0].Intval, null);
2128 ((Context) domain.context).insert ((MText) args[0].Strval, null);
2132 private static Xex.Term Finsert_candidates (Xex.Domain domain,
2136 Context ic = (Context) domain.context;
2137 Xex.Variable v = ic.domain.GetVar (Qcandidates_group_size, false);
2138 int column = (v == null ? 0 : v.Value.Intval);
2139 Candidates candidates = new Candidates (args, column);
2140 object candidate = candidates.Current;
2142 if (candidate is MText)
2143 ic.insert ((MText) candidate, candidates);
2145 ic.insert ((int) candidate, candidates);
2149 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
2152 Context ic = (Context) domain.context;
2153 Marker m = (Marker) args[0].Objval;
2155 return new Xex.Term (m.CharAt (ic));
2158 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
2161 Context ic = (Context) domain.context;
2165 pos = args[0].Intval;
2168 Marker m = (Marker) args[0].Objval;
2169 pos = m.Position (ic);
2171 return new Xex.Term (ic.delete (pos));
2174 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
2177 Context ic = (Context) domain.context;
2178 Candidates can = ic.candidates;
2182 object candidate = can.Current;
2184 if (candidate is MText)
2185 ic.delete (ic.cursor_pos - ((MText) candidate).Length);
2187 ic.delete (ic.cursor_pos - 1);
2189 candidate = can.Select (args[0].Intval);
2191 candidate = can.Select ((Selector) args[0].Objval);
2192 if (candidate is MText)
2193 ic.insert ((MText) candidate, can);
2195 ic.insert ((int) candidate, can);
2200 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
2203 ((Context) domain.context).show ();
2207 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
2210 ((Context) domain.context).hide ();
2214 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
2217 Context ic = (Context) domain.context;
2218 int pos = (args[0].IsInt ? args[0].Intval
2219 : ((Marker) args[0].Objval).Position (ic));
2224 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
2227 Marker m = (Marker) args[0].Objval;
2228 m.Mark ((Context) domain.context);
2232 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
2235 Context ic = (Context) domain.context;
2238 ic.pushback (args[0].Intval);
2239 else if (args[0].IsStr)
2240 ic.pushback (new KeySeq (args[0].Strval));
2242 ic.pushback ((KeySeq) args[0].Objval);
2246 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
2249 ((Context) domain.context).pop ();
2253 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
2256 int n = args.Length == 0 ? -2 : args[0].Intval;
2257 ((Context) domain.context).undo (n);
2261 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
2264 ((Context) domain.context).commit ();
2268 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
2271 ((Context) domain.context).commit ();
2272 args = new Xex.Term[2];
2273 args[0] = args[1] = Tcatch_tag;
2274 return Xex.Fthrow (domain, vari, args);
2277 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
2280 Context ic = (Context) domain.context;
2281 MSymbol state_name = (string) args[0].Symval;
2282 State state = (State) ic.im.states.Get (state_name);
2284 throw new Exception ("Unknown state: " + state_name);
2285 ((Context) domain.context).shift (state);
2289 private static Xex.Term Fshiftback (Xex.Domain domain, Xex.Variable vari,
2292 ((Context) domain.context).shift (null);
2296 private static Xex.Term Fkey_count (Xex.Domain domain, Xex.Variable vari,
2299 return new Xex.Term (((Context) domain.context).key_head);
2302 private static Xex.Term Fsurrounding_flag (Xex.Domain domain,
2306 return new Xex.Term (((Context) domain.context).GetSurroundingText == null
2310 public override string ToString ()
2313 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
2314 if (commands != null)
2316 str += " (commands";
2317 foreach (Command cmd in commands)
2321 if (var_names != null)
2323 str += " (variables";
2324 foreach (Xex.Symbol var in var_names)
2328 if (plugins != null)
2331 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
2332 str += " " + kv.Value;
2336 foreach (KeyValuePair<MSymbol, Map> kv in maps)
2337 str += " " + kv.Value;
2339 for (MPlist p = states; ! p.IsEmpty; p = p.next)
2340 str += " (" + p.Key + " " + ((State) p.Val).keymap + ")";
2344 public class Context
2346 internal MInputMethod im;
2347 internal Xex.Domain domain;
2348 private bool active;
2350 public Callback PreeditChanged;
2351 public Callback StatusChanged;
2352 public Callback CandidateChanged;
2353 public Callback GetSurroundingText;
2354 public Callback DelSurroundingText;
2356 private MText status;
2357 private MText produced = new MText ();
2358 internal MText preedit = new MText ();
2359 internal int cursor_pos;
2360 internal MPlist marker_positions = new MPlist ();
2362 internal Candidates candidates;
2363 private bool candidate_show;
2364 public bool CandidateShow { get { return candidate_show; } }
2366 private State initial_state, state, prev_state;
2367 private MText state_preedit = new MText ();
2368 private int state_key_head;
2369 private object state_var_values, state_initial_var_values;
2370 private int state_pos;
2372 private Keymap keymap;
2373 // Sequence of input keys.
2374 internal KeySeq keys = new KeySeq ();
2375 // Index into KEYS specifying the next key to handle.
2376 internal int key_head;
2378 internal MText preceding_text = new MText ();
2379 internal MText following_text = new MText ();
2381 // Set to false before calling the method 'handle_key', and set
2382 // to true when some key is unhandled.
2383 private bool key_unhandled;
2385 // The unhandled key. It has the meaning only when
2386 // 'key_unhandled' is true.
2387 private Key unhandled_key;
2389 internal ChangedStatus changed;
2391 private void set_cursor (string prefix, int pos)
2395 candidates = (Candidates) preedit.GetProp (cursor_pos - 1,
2401 internal void reset ()
2403 status = initial_state.title;
2407 set_cursor ("reset", 0);
2408 marker_positions.Clear ();
2410 candidate_show = false;
2412 state = prev_state = null;
2413 state_preedit.Del ();
2414 state_var_values = state_initial_var_values;
2416 shift (initial_state);
2418 preceding_text.Del ();
2419 following_text.Del ();
2421 changed = ChangedStatus.None;
2424 static Xex.Term[] catch_args = new Xex.Term[2];
2426 private bool take_actions (Xex.Term[] actions)
2428 catch_args[0] = Tcatch_tag;
2429 catch_args[1]= new Xex.Term (domain, Qprogn, actions);
2430 Xex.Term term = new Xex.Term (domain, Qcatch, catch_args);
2431 term = term.Eval (domain);
2432 return (! term.IsSymbol || term.Symval != Tcatch_tag.Symval);
2435 static MPlist callback_arg = new MPlist ();
2437 private bool get_surrounding_text (int len)
2439 if (len < 0 ? -len <= preceding_text.Length
2440 : len <= following_text.Length)
2442 if (GetSurroundingText == null)
2444 callback_arg.Set (MSymbol.integer, len);
2445 if (! GetSurroundingText (this, callback_arg)
2446 || ! callback_arg.IsMText)
2450 preceding_text = callback_arg.Text;
2451 return (-len <= preceding_text.Length);
2453 following_text = callback_arg.Text;
2454 return (len <= following_text.Length);
2457 internal int GetSurroundingChar (int pos)
2459 if (! get_surrounding_text (pos < 0 ? pos : pos + 1))
2462 return preceding_text[preceding_text.Length + pos];
2463 return following_text[pos];
2466 private void adjust_markers (int from, int to, int inserted)
2468 int diff = inserted - (to - from);
2470 for (MPlist p = marker_positions; ! p.IsEmpty; p = p.next)
2472 int pos = p.Integer;
2474 p.Set (p.Key, pos >= to ? pos + diff : from);
2476 if (cursor_pos >= to)
2477 set_cursor ("adjust", cursor_pos + diff);
2478 else if (cursor_pos > from)
2479 set_cursor ("adjust", from);
2482 private void preedit_replace (int from, int to, int c,
2483 Candidates candidates)
2485 preedit.Del (from, to);
2486 preedit.Ins (from, c);
2487 if (candidates != null)
2489 preedit.PushProp (from, from + 1, Mcandidates, candidates);
2490 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2493 adjust_markers (from, to, 1);
2496 private void preedit_replace (int from, int to, MText mt,
2497 Candidates candidates)
2499 preedit[from, to] = mt;
2500 if (candidates != null)
2502 preedit.PushProp (from, from + mt.Length, Mcandidates, candidates);
2503 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2506 adjust_markers (from, to, mt == null ? 0 : mt.Length);
2509 internal void insert (int c, Candidates candidates)
2511 preedit_replace (cursor_pos, cursor_pos, c, candidates);
2512 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2515 internal void insert (MText mt, Candidates candidates)
2517 preedit_replace (cursor_pos, cursor_pos, mt, candidates);
2518 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2521 internal int delete (int pos)
2523 int deleted = pos - cursor_pos;
2525 if (pos < cursor_pos)
2529 if (DelSurroundingText != null)
2531 Console.WriteLine ("deleting the prev {0} chars", - pos);
2532 callback_arg.Set (MSymbol.integer, pos);
2533 if (DelSurroundingText (this, callback_arg))
2535 if (callback_arg.IsInteger)
2536 deleted = callback_arg.Integer - cursor_pos;
2537 preceding_text.Del ();
2540 deleted = - cursor_pos;
2544 if (pos < cursor_pos)
2545 preedit_replace (pos, cursor_pos, null, null);
2549 if (pos > preedit.Length)
2551 if (DelSurroundingText != null)
2553 Console.WriteLine ("deleting the next {0} chars",
2554 pos - preedit.Length);
2555 callback_arg.Set (MSymbol.integer, pos - preedit.Length);
2556 if (DelSurroundingText (this, callback_arg))
2558 if (callback_arg.IsInteger)
2559 deleted = callback_arg.Integer - cursor_pos;
2560 preceding_text.Del ();
2563 deleted = preedit.Length - cursor_pos;
2565 pos = preedit.Length;
2567 if (pos > cursor_pos)
2568 preedit_replace (cursor_pos, pos, null, null);
2571 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2575 internal void show ()
2577 candidate_show = true;
2578 changed |= ChangedStatus.CandidateShow;
2581 internal void hide ()
2583 candidate_show = false;
2584 changed |= ChangedStatus.CandidateShow;
2587 internal void move (int pos)
2591 else if (pos > preedit.Length)
2592 pos = preedit.Length;
2593 if (pos != cursor_pos)
2595 set_cursor ("move", pos);
2596 changed |= ChangedStatus.Preedit;
2600 internal void pushback (int n)
2613 if (key_head > keys.keyseq.Count)
2614 key_head = keys.keyseq.Count;
2618 internal void pushback (KeySeq keyseq)
2622 if (key_head < keys.keyseq.Count)
2623 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2624 for (int i = 0; i < keyseq.keyseq.Count; i++)
2625 keys.keyseq.Add (keyseq.keyseq[i]);
2628 internal void pop ()
2630 if (key_head < keys.keyseq.Count)
2631 keys.keyseq.RemoveRange (key_head, 1);
2634 internal void undo (int n)
2637 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2639 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2643 internal void commit ()
2645 if (preedit.Length > 0)
2647 Candidates.Detach (this);
2648 produced.Cat (preedit);
2649 preedit_replace (0, preedit.Length, null, null);
2653 internal void shift (State state)
2657 if (prev_state == null)
2662 if (state == initial_state)
2665 keys.keyseq.RemoveRange (0, key_head);
2667 if (state != this.state)
2669 domain.RestoreValues (state_initial_var_values);
2670 if (state.enter_actions != null)
2671 take_actions (state.enter_actions);
2677 if (state != this.state && state.enter_actions != null)
2678 take_actions (state.enter_actions);
2679 prev_state = this.state;
2682 if (this.state == null || this.state.title != state.title)
2683 this.changed |= ChangedStatus.StateTitle;
2685 keymap = state.keymap;
2688 public Context (MInputMethod im)
2690 if (im.load_status != LoadStatus.Full
2692 throw new Exception ("Openging " + im.tag + " failed");
2694 domain = new Xex.Domain ("context", im.domain, this);
2695 initial_state = (State) im.states.Val;
2696 state_initial_var_values = domain.SaveValues ();
2699 if (PreeditChanged != null)
2701 callback_arg.Set (MSymbol.mtext, preedit);
2702 PreeditChanged (this, callback_arg);
2704 if (StatusChanged != null)
2706 callback_arg.Set (MSymbol.mtext, status);
2707 StatusChanged (this, callback_arg);
2711 public ChangedStatus Changed { get { return changed; } }
2713 internal object GetCandidates (out int column)
2716 if (cursor_pos == 0)
2718 Candidates candidates
2719 = (Candidates) preedit.GetProp (cursor_pos - 1, Mcandidates);
2720 if (candidates == null)
2722 column = candidates.Column;
2723 return candidates.Current;
2726 private void save_state ()
2728 state_var_values = domain.SaveValues ();
2729 state_preedit.Del ();
2730 state_preedit.Ins (0, preedit);
2731 state_key_head = key_head;
2732 state_pos = cursor_pos;
2735 private void restore_state ()
2737 domain.RestoreValues (state_var_values);
2739 preedit.Ins (0, state_preedit);
2740 set_cursor ("restore", state_pos);
2743 private bool handle_key ()
2745 Console.WriteLine ("{0}:key='{1}'", state.name, keys.keyseq[key_head]);
2746 Keymap sub = keymap.Lookup (keys, ref key_head);
2752 if (keymap.map_actions != null)
2754 if (! take_actions (keymap.map_actions))
2757 else if (keymap.submaps != null)
2759 for (int i = state_key_head; i < key_head; i++)
2760 preedit_replace (cursor_pos, cursor_pos,
2761 keys.keyseq[i].ToChar (), null);
2763 if (keymap.submaps == null)
2765 if (keymap.branch_actions != null)
2767 if (! take_actions (keymap.branch_actions))
2770 if (keymap != state.keymap)
2776 State current_state = state;
2778 if (keymap.branch_actions != null)
2780 if (! take_actions (keymap.branch_actions))
2783 if (state == current_state)
2785 if (state == initial_state
2786 && key_head < keys.keyseq.Count)
2788 if (keymap != state.keymap)
2790 else if (keymap.branch_actions == null)
2791 shift (initial_state);
2797 public bool Toggle ()
2803 public bool UnhandledKey (out Key key)
2805 key = unhandled_key;
2806 return key_unhandled;
2809 public MText Preedit { get { return preedit; } }
2810 public MText Produced { get { return produced; } }
2813 // true: All keys are handled and there's no text to commit.
2814 // false: Some key is left unhandled or there's a text to
2815 // commit. The caller should refer to UnhandledKey and
2818 public bool Filter (Key key)
2822 key_unhandled = true;
2823 unhandled_key = key;
2826 if (key == Key.Reload)
2828 changed = ChangedStatus.None;
2830 preceding_text.Del ();
2831 following_text.Del ();
2833 key_unhandled = false;
2834 keys.keyseq.Add (key);
2836 while (key_head < keys.keyseq.Count)
2838 if (! handle_key ())
2840 unhandled_key = keys.keyseq[key_head++];
2841 key_unhandled = true;
2847 keys.keyseq.RemoveRange (0, key_head);
2850 if ((changed & ChangedStatus.Preedit) != ChangedStatus.None
2851 && PreeditChanged != null)
2853 callback_arg.Set (MSymbol.mtext, preedit);
2854 PreeditChanged (this, callback_arg);
2856 if ((changed & ChangedStatus.StateTitle) != ChangedStatus.None
2857 && StatusChanged != null)
2859 callback_arg.Set (MSymbol.mtext, status);
2860 StatusChanged (this, callback_arg);
2862 if ((changed & ChangedStatus.Candidate) != ChangedStatus.None
2863 && CandidateChanged != null)
2865 CandidateChanged (this, callback_arg);
2867 return (! key_unhandled && produced.Length == 0);
2870 public bool Filter ()
2872 changed = ChangedStatus.None;
2874 preceding_text.Del ();
2875 following_text.Del ();
2878 if ((changed & ChangedStatus.Preedit) != ChangedStatus.None
2879 && PreeditChanged != null)
2881 callback_arg.Set (MSymbol.mtext, preedit);
2882 PreeditChanged (this, callback_arg);
2884 if ((changed & ChangedStatus.StateTitle) != ChangedStatus.None
2885 && StatusChanged != null)
2887 callback_arg.Set (MSymbol.mtext, status);
2888 StatusChanged (this, callback_arg);
2890 if ((changed & ChangedStatus.Candidate) != ChangedStatus.None
2891 && CandidateChanged != null)
2893 CandidateChanged (this, callback_arg);
2895 return (produced.Length == 0);
2899 public class Session
2905 public Session (MInputMethod im, MText mt, int pos)
2907 ic = new Context (im);
2910 ic.GetSurroundingText = get_surrounding_text;
2911 ic.DelSurroundingText = del_surrounding_text;
2914 private bool get_surrounding_text (Context ic, MPlist args)
2916 int len = args.Integer;
2918 args.Set (MSymbol.mtext, mt[0, pos]);
2920 args.Set (MSymbol.mtext, mt[pos, mt.Length]);
2924 private bool del_surrounding_text (Context ic, MPlist args)
2926 int pos = this.pos + args.Integer;
2927 Console.WriteLine ("del-surround: {0}-{1}", this.pos, pos);
2930 mt.Del (pos, this.pos);
2934 mt.Del (this.pos, pos);
2938 public bool HandleKey (ref Key key)
2940 if (! ic.Filter (key))
2942 MText produced = ic.Produced;
2943 mt.Ins (pos, produced);
2944 pos += produced.Length;
2946 if (ic.UnhandledKey (out unhandled))
2955 public bool Close ()
2957 bool result = ic.Filter ();
2960 mt.Ins (pos, ic.Produced);
2961 pos += ic.Produced.Length;
2968 public int CurrentPos { get { return pos; } set { pos = value; } }
2969 public MText Preedit { get { return ic.Preedit; } }