2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Reflection;
12 using Xex = System.Xml.Expression.Xexpression;
16 public class MInputMethod
19 public delegate bool Callback (Context ic, MPlist args);
22 public static Callback PreeditStart, PreeditDone, PreeditDraw;
23 public static Callback StatusStart, StatusDone, StatusDraw;
24 public static Callback CandidateStart, CandidateDone, CandidateDraw;
25 public static Callback SetSpot;
26 public static Callback Toggle;
27 public static Callback Reset;
28 public static Callback GetSurroundingText;
29 public static Callback DeleteSurroundingText;
31 internal static Xex.Domain im_domain = new Xex.Domain (null);
32 private static MSymbol Minput_method = "input-method";
33 private static MSymbol Mdescription = "description";
34 private static MSymbol Mvariable = "variable";
35 private static MSymbol Mcommand = "command";
36 private static MSymbol Mmodule = "module";
37 private static MSymbol Mtitle = "title";
38 private static MSymbol Minclude = "include";
39 private static MSymbol Mmacro = "macro";
40 private static MSymbol Mmap = "map";
41 private static MSymbol Mmap_list = "map-list";
42 private static MSymbol Mstate = "state";
43 internal static MSymbol Mcandidates = "candidates";
44 private static MSymbol Minsert = "insert";
45 private static MSymbol Mdelete = "delete";
46 private static MSymbol Mmove = "move";
47 private static MSymbol Mmark = "mark";
48 private static MSymbol Mmarker = "marker";
49 private static MSymbol Mset = "set";
50 private static MSymbol Madd = "add";
51 private static MSymbol Msub = "sub";
52 private static MSymbol Mmul = "mul";
53 private static MSymbol Mdiv = "div";
54 private static MSymbol Mif = "if";
55 private static MSymbol Mcond = "cond";
56 private static MSymbol Mchar_at = "char-at";
57 private static MSymbol Msurrounding_flag = "surrounding-text-flag";
58 private static MSymbol Mpushback = "pushback";
59 private static MSymbol Mkeyseq = "keyseq";
61 private static Xex.Symbol Nprogn = "progn";
62 private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
63 private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
65 private static Dictionary<MDatabase.Tag, MInputMethod> im_table
66 = new Dictionary<MDatabase.Tag, MInputMethod> ();
68 internal static MInputMethod im_global = null;
71 private enum LoadStatus
81 public enum ChangedStatus
88 CandidateIndex = 0x10,
92 private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
93 | ChangedStatus.CandidateIndex
94 | ChangedStatus.CandidateShow);
96 public enum KeyModifier
100 Shift_R = 0x00800000,
102 Control_L = 0x01000000,
103 Control_R = 0x02000000,
104 Control = 0x03000000,
119 private static Dictionary<string, uint> keysyms
120 = new Dictionary<string, uint> ();
121 private static Dictionary<string, KeyModifier> keymodifiers
122 = new Dictionary<string, KeyModifier> ();
123 private static uint keysym_base = 0x200000;
124 private static uint char_mask = ~((uint) KeyModifier.All);
128 keysyms["bs"] = keysyms["backspace"] = 0x08;
129 keysyms["tab"] = 0x09;
130 keysyms["lf"] = keysyms["linefeed"] = 0x10;
131 keysyms["cr"] = keysyms["return"] = keysyms["enter"] = 0x13;
132 keysyms["esc"] = keysyms["escape"] = 0x1B;
133 keysyms["spc"] = keysyms["space"] = 0x20;
134 keysyms["del"] = keysyms["delete"] = 0x7F;
135 keymodifiers["shift-l"] = KeyModifier.Shift_L;
136 keymodifiers["shift-r"] = KeyModifier.Shift_R;
137 keymodifiers["shift"] = KeyModifier.Shift;
138 keymodifiers["control-l"] = KeyModifier.Control_L;
139 keymodifiers["control-r"] = KeyModifier.Control_R;
140 keymodifiers["control"] = KeyModifier.Control;
141 keymodifiers["alt-l"] = KeyModifier.Alt_L;
142 keymodifiers["alt-r"] = KeyModifier.Alt_R;
143 keymodifiers["alt"] = KeyModifier.Alt;
144 keymodifiers["altgr"] = KeyModifier.AltGr;
145 keymodifiers["super"] = KeyModifier.Super;
146 keymodifiers["hyper"] = KeyModifier.Hyper;
149 private static uint decode_keysym (MSymbol keysym)
152 string name = keysym.Name;
154 if (name.Length == 1)
156 name = name.ToLower ();
157 if (! keysyms.TryGetValue (name, out key))
158 keysyms[name] = key = keysym_base++;
162 private static uint combine_modifiers (uint c, KeyModifier modifiers)
164 if (c < 0x7F && c != 0x20)
166 if ((modifiers & KeyModifier.Shift) != KeyModifier.None
167 && Char.IsLower ((char) c))
169 modifiers &= ~KeyModifier.Shift;
170 c = Char.ToUpper ((char) c);
172 if ((modifiers & KeyModifier.Control) != KeyModifier.None)
174 modifiers &= ~KeyModifier.Control;
178 return c | (uint) modifiers;
181 public Key (uint c) { key = c; }
182 public Key (int c) { key = (uint) c; }
184 public Key (uint c, KeyModifier modifiers)
186 key = combine_modifiers (c, modifiers);
189 public Key (MSymbol keysym, KeyModifier modifiers)
191 key = combine_modifiers (decode_keysym (keysym), modifiers);
194 public Key (MSymbol keysym)
196 string str = keysym.Name;
197 int len = str.Length;
199 KeyModifier modifiers = KeyModifier.None;
201 for (i = 0; i + 2 < len && str[i + 1] == '-'; i += 2)
204 modifiers |= KeyModifier.Shift;
205 else if (str[i] == 'C')
206 modifiers |= KeyModifier.Control;
207 else if (str[i] == 'A')
208 modifiers |= KeyModifier.Alt;
209 else if (str[i] == 'G')
210 modifiers |= KeyModifier.AltGr;
211 else if (str[i] == 's')
212 modifiers |= KeyModifier.Super;
213 else if (str[i] == 'H')
214 modifiers |= KeyModifier.Hyper;
217 key = combine_modifiers (str[i], modifiers);
219 key = combine_modifiers (decode_keysym (keysym), modifiers);
222 public Key (MPlist plist)
224 KeyModifier modifiers = KeyModifier.None;
227 for (p = plist; ! p.IsEmpty; p = p.next)
231 if (! p.next.IsEmpty)
232 throw new Exception ("Invalid Key: " + plist);
235 else if (! p.IsSymbol)
236 throw new Exception ("Invalid Key: " + plist);
239 string name = p.Symbol.Name.ToLower ();
242 if (! keymodifiers.TryGetValue (name, out m))
247 if (p.IsEmpty || ! p.next.IsEmpty)
248 throw new Exception ("Invalid Key: " + plist);
250 key = combine_modifiers ((uint) p.Integer, modifiers);
252 key = combine_modifiers (decode_keysym (p.Symbol), modifiers);
255 public bool HasModifier
257 get { return ((key & (uint) KeyModifier.All) != 0); }
260 public bool Match (Key k)
264 if ((k.key & char_mask) != (key & char_mask))
266 KeyModifier m1 = ((KeyModifier) key) & KeyModifier.All;
267 KeyModifier m2 = ((KeyModifier) k.key) & KeyModifier.All;
268 return (((m1 & KeyModifier.Shift) == (m2 & KeyModifier.Shift)
269 || ((m1 & KeyModifier.Shift) == KeyModifier.Shift
270 && (m2 & KeyModifier.Shift) != KeyModifier.None))
271 && ((m1 & KeyModifier.Control) == (m2 & KeyModifier.Control)
272 || ((m1 & KeyModifier.Control) == KeyModifier.Control
273 && (m2 & KeyModifier.Control) != KeyModifier.None))
274 && ((m1 & KeyModifier.Alt) == (m2 & KeyModifier.Alt)
275 || ((m1 & KeyModifier.Alt) == KeyModifier.Alt
276 && (m2 & KeyModifier.Alt) != KeyModifier.None))
277 && ((m1 & KeyModifier.High) == (m2 & KeyModifier.High)));
280 public override string ToString ()
282 string str = Char.ToString ((char) key);
283 KeyModifier m = ((KeyModifier) key) & KeyModifier.All;
285 if (m != KeyModifier.None)
287 if ((m & KeyModifier.Shift) != KeyModifier.None)
289 if ((m & KeyModifier.Control) != KeyModifier.None)
291 if ((m & KeyModifier.Alt) != KeyModifier.None)
293 if ((m & KeyModifier.AltGr) != KeyModifier.None)
295 if ((m & KeyModifier.Super) != KeyModifier.None)
297 if ((m & KeyModifier.Hyper) != KeyModifier.None)
304 public class KeySeq : Xex.TermValue
306 public List<Key> keyseq = new List<Key> ();
308 public override Xex.TermValue Clone ()
310 KeySeq ks = new KeySeq ();
311 ks.keyseq.InsertRange (0, keyseq);
317 public KeySeq (MPlist plist)
319 foreach (MPlist p in plist)
322 keyseq.Add (new Key (p.Symbol));
323 else if (p.IsInteger)
324 keyseq.Add (new Key ((char) p.Integer));
326 keyseq.Add (new Key (p.Plist));
328 throw new Exception ("Invalid Key Sequence: " + plist);
332 public KeySeq (MText mt) : base ()
334 for (int i = 0; i < mt.Length; i++)
335 keyseq.Add (new Key ((uint) mt[i]));
338 public KeySeq (List<Xex.Term> list)
340 int len = list.Count;
342 for (int i = 0; i < len; i++)
345 keyseq.Add (new Key (list[i].Intval));
346 else if (list[i].IsStr)
347 keyseq.Add (new Key (list[i].Strval));
348 else if (list[i].IsSymbol)
349 keyseq.Add (new Key ((string) list[i].Symval));
351 throw new Exception ("Invalid key: " + list[i]);
355 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
357 Xex.Term term = new Xex.Term (domain, node.FirstChild).Eval (domain);
358 return (term.IsStr ? new KeySeq ((MText) term.Strval)
359 : new KeySeq (term.Listval));
362 public override string ToString ()
366 foreach (Key key in keyseq)
370 foreach (Key k in keyseq)
371 str += " " + k.ToString ();
375 foreach (Key key in keyseq)
376 str += key.ToString ();
384 public MText description;
385 public List<KeySeq> keys;
387 public Command (MPlist p)
391 description = parse_description (p);
392 if (description == null)
393 description = "No description";
394 keys = new List<KeySeq> ();
395 Console.WriteLine ("cmd:" + p);
396 for (p = p.next; ! p.IsEmpty; p = p.next)
399 keys.Add (new KeySeq (p.Text));
401 keys.Add (new KeySeq (p.Plist));
405 public Command (XmlNode node)
407 name = node.Attributes[0].Value;
408 keys = new List<KeySeq> ();
409 for (node = node.FirstChild; node != null; node = node.NextSibling)
411 if (node.Name == "description")
412 description = parse_description (node);
413 else if (node.Name == "keyseq")
414 keys.Add ((KeySeq) KeySeq.parser (null, node));
418 public override string ToString ()
420 string str = "(" + name + " \"" + (string) description;
421 foreach (KeySeq keyseq in keys)
427 internal class Plugin
430 private Assembly assembly;
431 private Type plugin_type;
433 public Plugin (string name)
438 public MethodInfo GetMethod (Xex.Symbol name)
440 if (assembly == null)
442 assembly = Assembly.LoadFrom (name + ".dll");
443 plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
446 MethodInfo info = plugin_type.GetMethod ((string) name);
448 throw new Exception ("Invalid plugin method: " + name);
452 public override string ToString ()
454 return String.Format ("(module {0}", name);
458 internal class PluginMethod : Xex.Function
460 private Plugin plugin;
461 private MethodInfo method_info;
462 object[] parameters = new object[2];
464 public PluginMethod (Plugin plugin, string name)
465 : base ((Xex.Symbol) name, 0, -1)
467 this.plugin = plugin;
470 public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
473 args = (Xex.Term[]) args.Clone ();
474 for (int i = 0; i < args.Length; i++)
476 args[i] = args[i].Eval (domain);
480 if (method_info == null)
481 method_info = plugin.GetMethod (name);
482 parameters[0] = domain.context;
483 parameters[1] = args;
484 return (Xex.Term) method_info.Invoke (null, parameters);
488 internal abstract class Marker : Xex.TermValue
490 private MSymbol name;
492 public Marker (MSymbol name)
497 public abstract int Position (Context ic);
498 public abstract void Mark (Context ic);
500 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
502 MSymbol name = node.InnerText;
504 return Get ((Context) domain.context, name);
507 public class Named : Marker
511 public Named (MSymbol name) : this (name, 0) { }
513 private Named (MSymbol name, int p) : base (name) { pos = p; }
515 public override int Position (Context ic) { return pos; }
517 public override void Mark (Context ic) { pos = ic.cursor_pos; }
519 public override Xex.TermValue Clone ()
521 return new Named (name, pos);
525 public class Predefined : Marker
527 public Predefined (MSymbol name) : base (name) { }
529 public override int Position (Context ic)
531 switch (name.Name[1]) {
533 case '>': return ic.preedit.Length;
534 case '-': return ic.cursor_pos - 1;
535 case '+': return ic.cursor_pos + 1;
537 if (ic.cursor_pos > 0)
539 int pos = ic.cursor_pos;
541 ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to);
546 if (ic.cursor_pos < ic.preedit.Length - 1)
548 int pos = ic.cursor_pos;
550 ic.preedit.FindProp (Mcandidates, pos, out from, out pos);
553 return ic.preedit.Length;
555 return name.Name[1] - '0';
559 public override void Mark (Context ic)
561 throw new Exception ("Can't set predefined marker: " + name);
564 public override Xex.TermValue Clone ()
566 return new Predefined (name);
570 static internal Dictionary<MSymbol,Predefined> predefined_markers;
574 predefined_markers = new Dictionary<MSymbol,Predefined> ();
575 MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
576 "@0", "@1", "@2", "@3", "@4",
577 "@5", "@6", "@7", "@8", "@9" };
578 foreach (MSymbol s in symlist)
579 predefined_markers[s] = new Predefined (s);
582 public static Marker Get (Context ic, MSymbol name)
587 if (predefined_markers.TryGetValue (name, out pred))
589 if (name.Name[0] == '@')
590 throw new Exception ("Invalid marker name: " + name);
591 m = (Marker) ic.markers.Get (name);
594 m = new Named (name);
595 ic.markers.Put (name, m);
601 internal class Candidates
608 public Block (int index, Xex.Term term)
612 Data = (MText) term.Strval;
615 MPlist plist = new MPlist ();
617 foreach (Xex.Term t in term.Listval)
618 p = p.Add (MSymbol.mtext, (MText) t.Strval);
623 public Block (int index, MPlist plist)
628 else if (plist.IsPlist)
631 throw new Exception ("Invalid candidate: " + plist);
636 get { return (Data is MText
637 ? ((MText) Data).Length
638 : ((MPlist) Data).Count); }
641 public object this[int i]
644 if (Data is MText) return ((MText) Data)[i];
645 return ((MPlist) Data)[i];
650 private Block[] blocks;
652 private int index = 0;
653 public object[] group;
655 private bool IsFixed { get { return group != null; } }
658 Block last = blocks[blocks.Length - 1];
659 return last.Index + last.Count; }
663 get { return (IsFixed ? index % group.Length
664 : index - blocks[row].Index); }
667 public object Group {
668 get { return (IsFixed ? group : blocks[row].Data); }
671 public int GroupLength
676 int nitems = group.Length;
677 int start = index - (index % nitems);
679 return (start + nitems <= total ? nitems : total - start);
681 return blocks[row].Count;
685 public object Current {
687 return (IsFixed ? group[index % group.Length]
688 : blocks[row][index - blocks[row].Index]);
692 public Candidates (MPlist list, int column)
694 int nblocks = list.Count;
696 blocks = new Block[nblocks];
697 for (int i = 0, start = 0; i < nblocks; i++, list = list.next)
698 start += (blocks[i] = new Block (index, list)).Count;
700 group = new object[column];
703 public Candidates (List<Xex.Term> list, int column)
705 int nblocks = list.Count;
707 blocks = new Block[nblocks];
708 for (int i = 0, start = 0; i < nblocks; i++)
709 start += (blocks[i] = new Block (index, list[i])).Count;
711 group = new object[column];
714 public static void Detach (Context ic)
716 ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
717 ic.candidates = null;
718 ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
722 // Fill the array "group" by candidates stating from INDEX.
723 // INDEX must be a multiple of "column". Set NTIMES to the
724 // number of valid candidates in "group". Update "block" if
725 // necessary. Return "group".
727 private int fill_group (int start)
729 int nitems = group.Length;
734 while (start < b.Index)
737 while (start >= b.Index + b.Count)
743 for (int i = 0; i < nitems; i++, start++)
748 if (r == blocks.Length)
759 // Update "row" to what contains the first candidate of
760 // the previous candidate-group, update "current_index", and
761 // update "group" if necessary. Return the previous
762 // candidate-group. Set NITEMS to the number of valid
763 // candidates contained in that group.
765 public int PrevGroup ()
772 nitems = group.Length;
773 if ((index -= col + nitems) < 0)
774 index = (Total / nitems) * nitems;
775 nitems = fill_group (index);
779 row = row > 0 ? row-- : blocks.Length - 1;
780 nitems = blocks[row].Count;
781 index = blocks[row].Index;
783 index += col < nitems ? col : nitems - 1;
787 public int NextGroup ()
794 nitems = group.Length;
795 if ((index += nitems - col) >= Total)
797 nitems = fill_group (index);
801 row = row < blocks.Length - 1 ? row + 1 : 0;
802 nitems = blocks[row].Count;
803 index = blocks[row].Count;
805 index += col < nitems ? col : nitems - 1;
815 int nitems = PrevGroup ();
816 index += col < nitems - 1 ? col : nitems - 1;
825 int nitems = GroupLength;
827 if (col == nitems - 1)
829 nitems = NextGroup ();
843 index += GroupLength - (Column + 1);
846 public void Select (int col)
848 int maxcol = GroupLength - 1;
851 index = index - Column + col;
855 internal abstract class Selector : Xex.TermValue
857 private Selector () { }
859 public abstract void Select (Candidates candidates);
861 public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
863 MSymbol name = node.InnerText;
866 if (predefined_selectors.TryGetValue (name, out pred))
868 if (name.Name[0] == '@')
869 throw new Exception ("Invalid selector name: " + name);
871 if (! Int32.TryParse (node.InnerText, out index))
872 throw new Exception ("Invalid selector name: " + name);
873 return new Numbered (index);
876 public override Xex.TermValue Clone () { return this; }
878 public class Numbered : Selector
882 public Numbered (int index) { this.index = index; }
884 public override void Select (Candidates can) { can.Select (index); }
887 public class Predefined : Selector
891 internal Predefined (MSymbol sym) { this.tag = sym.Name[1]; }
893 public override void Select (Candidates candidates)
897 case '<': candidates.First (); break;
898 case '>': candidates.Last (); break;
899 case '-': candidates.Prev (); break;
900 case '+': candidates.Next (); break;
901 case '[': candidates.PrevGroup (); break;
902 case ']': candidates.NextGroup (); break;
908 static new Dictionary<MSymbol, Predefined> predefined_selectors;
912 predefined_selectors = new Dictionary<MSymbol, Predefined> ();
913 MSymbol[] symlist = new MSymbol[] { "@<", "@=", "@>", "@-", "@+",
915 foreach (MSymbol s in symlist)
916 predefined_selectors[s] = new Predefined (s);
923 public Dictionary<Key, Map> submaps;
924 public Xex.Term actions;
926 public void Add (KeySeq keys, int index, Xex.Term actions)
931 submaps = new Dictionary<Key, Map> ();
933 submaps.TryGetValue (keys.keyseq[index], out sub);
936 Key key = keys.keyseq[index];
937 submaps[key] = sub = new Map ();
939 if (index + 1 < keys.keyseq.Count)
940 sub.Add (keys, index + 1, actions);
942 this.actions = actions;
945 public Xex.Term Lookup (KeySeq keys, int index)
949 if (index + 1 == keys.keyseq.Count)
951 if (submaps.TryGetValue (keys.keyseq[index], out sub))
952 return sub.Lookup (keys, index + 1);
956 private void describe (MText mt, KeySeq keyseq)
958 if (keyseq.keyseq.Count > 0)
960 mt.Cat (" (").Cat (keyseq.ToString ());
961 mt.Cat (' ').Cat (actions.ToString ());
965 foreach (KeyValuePair<Key, Map> kv in submaps)
967 keyseq.keyseq.Add (kv.Key);
968 kv.Value.describe (mt, keyseq);
969 keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
973 public override string ToString ()
975 MText mt = "(" + name.Name;
976 KeySeq keyseq = new KeySeq ();
978 describe (mt, keyseq);
988 public MPlist branches = new MPlist ();
990 public State (MSymbol name)
995 public override string ToString ()
997 MText mt = "(" + name.Name;
1000 mt.Cat (" \"" + title + "\"");
1001 for (MPlist p = branches; ! p.IsEmpty; p = p.next)
1002 mt.Cat (" (" + p.Key + " " + (Xex) p.Val + ")");
1003 return (string) mt + ")";
1008 internal Xex.Domain domain = new Xex.Domain (im_domain, null);
1010 private LoadStatus load_status = LoadStatus.None;
1011 private MDatabase.Tag tag;
1012 private MDatabase mdb;
1014 private MText description;
1015 internal MText title;
1016 internal Command[] commands;
1017 internal Xex.Symbol[] var_names;
1018 internal Dictionary<MSymbol, Plugin> plugins;
1019 internal Dictionary<MSymbol, Map> maps;
1020 internal MPlist states;
1022 static MInputMethod ()
1024 im_domain.DefTerm ("keyseq", KeySeq.parser);
1025 im_domain.DefTerm ("marker", Marker.parser);
1026 im_domain.DefTerm ("selector", Selector.parser);
1028 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1029 im_domain.DefSubr (Finsert_candidates, "candidates", false, 1, -1);
1030 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1031 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1032 im_domain.DefSubr (Fshow, "show", false, 0, 0);
1033 im_domain.DefSubr (Fhide, "hide", false, 0, 0);
1034 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1035 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1036 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1037 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1038 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1039 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1040 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1041 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1042 im_domain.DefSubr (Fshift_back, "shiftback", false, 0, 0);
1043 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1044 im_domain.DefSubr (Fkey_count, "key-count", false, 1, 1);
1045 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag",
1048 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
1049 List<MDatabase> list = MDatabase.List (tag);
1050 M17n.DebugPrint ("Found {0} input methods\n", list.Count);
1051 foreach (MDatabase mdb in list)
1052 im_table[mdb.tag] = new MInputMethod (mdb.tag);
1056 private MInputMethod (MDatabase.Tag tag)
1061 // Instance Properties
1062 public MSymbol Language { get { return tag[1]; } }
1063 public MSymbol Name { get { return tag[2]; } }
1064 public MSymbol SubName { get { return tag[3]; } }
1066 public bool Info (out MText description,
1068 out Xex.Variable[] variables,
1069 out Command[] commands)
1071 if ((load_status & LoadStatus.Header) != LoadStatus.Header
1072 && ! load_header ())
1080 description = this.description;
1082 if (var_names == null)
1086 variables = new Xex.Variable[var_names.Length];
1088 foreach (Xex.Symbol name in var_names)
1089 variables[i++] = domain.GetVar (name, false);
1091 commands = this.commands;
1095 public static MInputMethod Find (MSymbol language, MSymbol name)
1097 return Find (language, name, MSymbol.nil);
1100 public static MInputMethod Find (MSymbol language, MSymbol name,
1103 MDatabase.Tag tag = new MDatabase.Tag (Minput_method, language,
1107 return (im_table.TryGetValue (tag, out im) ? im : null);
1112 return ((load_status == LoadStatus.Full) || load_body ());
1115 public static MInputMethod[] List ()
1117 MInputMethod[] array = new MInputMethod[im_table.Count];
1120 foreach (KeyValuePair<MDatabase.Tag, MInputMethod> kv in im_table)
1121 array[i++] = kv.Value;
1125 private bool load_header ()
1127 mdb = MDatabase.Find (tag);
1130 mdb.name_table = Xex.Symbol.Table;
1132 MSymbol format = mdb.Format;
1134 if (format == MSymbol.plist)
1135 load ((MPlist) mdb.Load (Mmap), false);
1138 XmlDocument doc = (XmlDocument) mdb.Load (Mmap_list);
1139 load (doc.DocumentElement, false);
1141 } catch (Exception e) {
1142 Console.WriteLine ("{0}\n", e);
1143 load_status = LoadStatus.Error;
1146 load_status |= LoadStatus.Header;
1150 private bool load_body ()
1152 mdb = MDatabase.Find (tag);
1155 mdb.name_table = Xex.Symbol.Table;
1157 object obj = mdb.Load ();
1159 load ((MPlist) obj, true);
1161 load ((XmlDocument) obj, true);
1162 } catch (Exception e) {
1163 Console.WriteLine (e);
1164 load_status = LoadStatus.Error;
1167 load_status = LoadStatus.Full;
1171 private void load (MPlist plist, bool full)
1173 maps = new Dictionary<MSymbol, Map> ();
1174 states = new MPlist ();
1176 for (; ! plist.IsEmpty; plist = plist.next)
1179 MPlist pl = plist.Plist;
1182 MSymbol sym = pl.Symbol;
1185 if (sym == Mdescription)
1186 description = parse_description (pl);
1187 else if (sym == Mtitle)
1192 else if (sym == Mvariable)
1193 parse_variables (pl);
1194 else if (sym == Mcommand)
1195 parse_commands (pl);
1200 else if (sym == Minclude)
1202 else if (sym == Mmacro)
1204 else if (sym == Mmap)
1206 else if (sym == Mstate)
1211 if (description == null)
1212 description = (MText) "No description";
1214 title = new MText (tag[2].Name);
1215 if (commands == null)
1216 commands = new Command[0];
1221 State state = new State ((MSymbol) "init");
1222 plist = new MPlist ();
1223 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1224 state.branches.Add (kv.Key, null);
1225 states.Add (state.name, state);
1229 private void load (XmlNode node, bool full)
1231 bool skip_header = load_status == LoadStatus.Header;
1233 maps = new Dictionary<MSymbol, Map> ();
1234 states = new MPlist ();
1236 if (node.NodeType == XmlNodeType.Document)
1237 node = node.FirstChild;
1238 while (node.NodeType != XmlNodeType.Element)
1239 node = node.NextSibling;
1240 for (node = node.FirstChild; node != null; node = node.NextSibling)
1242 if (node.NodeType != XmlNodeType.Element)
1246 if (node.Name == "description")
1247 description = parse_description (node);
1248 else if (node.Name == "title")
1249 title = parse_title (node);
1250 else if (node.Name == "variable-list")
1251 parse_variables (node);
1252 else if (node.Name == "command-list")
1253 parse_commands (node);
1257 if (node.Name == "module-list")
1258 parse_plugins (node);
1259 else if (node.Name == "macro-list")
1260 parse_macros (node);
1261 else if (node.Name == "map-list")
1263 else if (node.Name == "state-list")
1264 parse_states (node);
1267 if (description == null)
1268 description = (MText) "No description";
1270 title = new MText (tag[2].Name);
1271 if (commands == null)
1272 commands = new Command[0];
1277 State state = new State ((MSymbol) "init");
1278 foreach (KeyValuePair<MSymbol, Map>kv in maps)
1279 state.branches.Add (kv.Key, null);
1280 states.Add (state.name, state);
1284 private static void transform (MPlist plist)
1286 for (; ! plist.IsEmpty; plist = plist.next)
1290 MPlist p = new MPlist ();
1291 p.Add (MSymbol.symbol, Minsert);
1292 p.Add (MSymbol.mtext, plist.Text);
1293 plist.Set (MSymbol.plist, p);
1295 else if (plist.IsInteger)
1297 MPlist p = new MPlist ();
1298 p.Add (MSymbol.symbol, Minsert);
1299 p.Add (MSymbol.integer, plist.Integer);
1300 plist.Set (MSymbol.plist, p);
1302 else if (plist.IsPlist)
1304 MPlist pl = plist.Plist;
1308 if (pl.Symbol == Madd)
1309 pl.Set (MSymbol.symbol, (MSymbol) "+=");
1310 else if (pl.Symbol == Msub)
1311 pl.Set (MSymbol.symbol, (MSymbol) "-=");
1312 else if (pl.Symbol == Mmul)
1313 pl.Set (MSymbol.symbol, (MSymbol) "*=");
1314 else if (pl.Symbol == Mdiv)
1315 pl.Set (MSymbol.symbol, (MSymbol) "/=");
1316 else if (pl.Symbol == Minsert)
1318 // (insert (CANDIDATES ...))
1319 // => (candidates CANDIDATES ...)
1320 if (pl.next.IsPlist)
1322 pl.Set (MSymbol.symbol, Mcandidates);
1324 MPlist p = pl.Plist;
1325 pl.Set (p.key, p.val);
1326 for (p = p.next; ! p.IsEmpty; p = p.next);
1327 pl.Add (p.key, p.val);
1330 else if (pl.Symbol == Mif)
1334 transform (pl.next);
1336 else if (pl.Symbol == Mcond)
1338 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1341 MPlist p = pl.Plist;
1349 else if (pl.Symbol == Mdelete
1350 || pl.Symbol == Mmove
1351 || pl.Symbol == Mmark)
1356 MSymbol sym = pl.Symbol;
1357 MPlist p = new MPlist ();
1358 p.Add (MSymbol.symbol, Mmarker);
1359 p.Add (MSymbol.symbol, sym);
1360 pl.Set (MSymbol.plist, p);
1363 else if (pl.Symbol == Mpushback)
1367 pl.Plist.Push (MSymbol.symbol, Mkeyseq);
1370 else if (pl.IsMText)
1372 // (CANDIDATES ...) => (candidates CANDIDATES ...)
1373 pl.Push (MSymbol.symbol, Mcandidates);
1376 else if (plist.IsSymbol)
1378 MSymbol sym = plist.Symbol;
1380 if (sym.Name.Length >= 3
1381 && sym.Name[0] == '@'
1382 && (sym.Name[1] == '-' || sym.Name[1] == '+'))
1384 int pos = int.Parse (sym.Name.Substring (1));
1385 MPlist p = new MPlist ();
1389 p.Add (MSymbol.symbol, Msurrounding_flag);
1393 if (sym.Name[1] == '+')
1395 p.Add (MSymbol.symbol, Mchar_at);
1396 p.Add (MSymbol.integer, pos);
1398 plist.Set (MSymbol.plist, p);
1404 private static MText parse_description (MPlist plist)
1410 plist = plist.Plist;
1411 if (plist.IsSymbol && plist.Symbol == (MSymbol) "_"
1412 && plist.next.IsMText)
1413 return plist.next.Text;
1418 private static MText parse_description (XmlNode node)
1420 if (node.HasChildNodes)
1421 node = node.FirstChild;
1422 return node.InnerText;
1425 private static MText parse_title (XmlNode node)
1427 return node.InnerText;
1430 private void new_variable (Xex.Symbol name, string desc, int val,
1431 MPlist pl, Xex.Variable vari)
1439 range = new int[pl.Count * 2];
1440 for (int i = 0; i < range.Length; i++)
1444 MPlist p = pl.Plist;
1446 if (! p.IsInteger || ! p.next.IsInteger)
1447 throw new Exception ("Invalid range: " + p);
1448 range[i * 2] = p.Integer;
1449 range[i * 2 + 1] = p.next.Integer;
1451 else if (pl.IsInteger)
1452 range[i * 2] = range[i * 2 + 1] = pl.Integer;
1454 throw new Exception ("Invalid range: " + pl);
1458 domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
1461 Xex.Term term = new Xex.Term (val);
1463 vari.DefaultValue = term;
1468 private void new_variable (Xex.Symbol name, string desc, MText val,
1469 MPlist pl, Xex.Variable vari)
1477 range = new string[pl.Count * 2];
1478 for (int i = 0; i < range.Length; i++)
1481 range[i] = (string) pl.Text;
1483 throw new Exception ("Invalid range: " + pl);
1487 domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
1490 Xex.Term term = new Xex.Term ((string) val);
1492 vari.DefaultValue = term;
1497 private void new_variable (Xex.Symbol name, string desc, MSymbol val,
1498 MPlist pl, Xex.Variable vari)
1501 Xex.Symbol sym = val.Name;
1507 range = new Xex.Symbol[pl.Count * 2];
1508 for (int i = 0; i < range.Length; i++)
1511 range[i] = pl.Symbol.Name;
1513 throw new Exception ("Invalid range: " + pl);
1517 domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
1520 Xex.Term term = new Xex.Term (sym);
1522 vari.DefaultValue = term;
1527 private Xex.Variable get_global_var (Xex.Symbol name)
1529 if (im_global == null || this != im_global)
1531 tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
1533 im_global = im_table[tag];
1534 if (! im_global.Open ())
1535 throw new Exception ("Failed to load global");
1537 return im_global.domain.GetVar (name, false);
1540 private void parse_variables (MPlist plist)
1542 var_names = new Xex.Symbol[plist.Count];
1544 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1546 if (! plist.IsPlist || ! plist.Plist.IsSymbol)
1547 throw new Exception ("Invalid variable: " + plist);
1549 MPlist p = plist.Plist;
1550 Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
1551 var_names[i] = name;
1553 string desc = (string) parse_description (p);
1554 Xex.Variable vari = get_global_var (name);
1556 domain.Defvar (vari);
1562 new_variable (name, desc, p.Integer, p.next, vari);
1564 new_variable (name, desc, p.Text, p.next, vari);
1565 else if (p.IsSymbol)
1566 new_variable (name, desc, p.Symbol, p.next, vari);
1568 throw new Exception ("Invalid variable type: " + p.val);
1573 private void parse_variables (XmlNode node)
1575 XmlNodeList node_list = node.ChildNodes;
1577 var_names = new Xex.Symbol[node_list.Count];
1578 for (int i = 0; i < node_list.Count; i++)
1580 Xex.Symbol name = node_list[i].Attributes[0].Value;
1581 Xex.Variable vari = get_global_var (name);
1583 domain.Defvar (vari);
1584 domain.Defvar (node_list[i]);
1585 var_names[i] = name;
1589 private void parse_commands (MPlist plist)
1591 commands = new Command[plist.Count];
1593 for (int i = 0; ! plist.IsEmpty; plist = plist.next)
1594 if (plist.IsPlist && plist.Plist.IsSymbol)
1595 commands[i++] = new Command (plist.Plist);
1598 private void parse_commands (XmlNode node)
1600 XmlNodeList node_list = node.ChildNodes;
1602 commands = new Command[node_list.Count];
1603 for (int i = 0; i < node_list.Count; i++)
1605 if (node_list[i].NodeType == XmlNodeType.Element)
1606 commands[i] = new Command (node_list[i]);
1610 private void parse_plugins (MPlist plist)
1612 plugins = new Dictionary<MSymbol, Plugin> ();
1614 for (; ! plist.IsEmpty; plist = plist.Next)
1616 MPlist p = plist.Plist;
1617 MSymbol sym = p.Symbol;
1618 Plugin plugin = new Plugin (sym.Name);
1620 for (p = p.next; ! p.IsEmpty; p = p.next)
1622 Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
1623 domain.Defun (func);
1628 private void parse_plugins (XmlNode node)
1630 plugins = new Dictionary<MSymbol, Plugin> ();
1632 foreach (XmlNode n in node.ChildNodes)
1634 Plugin plugin = new Plugin (n.Attributes[0].Value);
1635 foreach (XmlNode nn in n.ChildNodes)
1637 Xex.Function func = new PluginMethod (plugin,
1638 nn.Attributes[0].Value);
1639 domain.Defun (func);
1644 private void parse_macros (XmlNode node)
1646 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1647 if (nn.NodeType == XmlNodeType.Element)
1648 domain.Defun (nn, true);
1649 for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
1650 if (nn.NodeType == XmlNodeType.Element)
1651 domain.Defun (nn, false);
1654 private void parse_maps (XmlNode node)
1658 private void parse_states (XmlNode node)
1662 private void parse_include (MPlist plist)
1664 if (! plist.IsPlist)
1666 MPlist p = plist.Plist;
1667 MSymbol language, name, subname;
1668 language = p.Symbol;
1671 name = subname = MSymbol.nil;
1677 subname = MSymbol.nil;
1682 MInputMethod im = MInputMethod.Find (language, name, subname);
1688 if (! plist.IsSymbol)
1690 MSymbol target_type = plist.Symbol;
1692 MSymbol target_name = MSymbol.nil;
1694 target_name = plist.Symbol;
1695 if (target_type == Mmacro)
1697 if (target_name == MSymbol.nil)
1698 im.domain.CopyFunc (domain);
1700 im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
1702 else if (target_type == Mmap)
1704 if (target_name == MSymbol.nil)
1706 foreach (KeyValuePair<MSymbol, Map> kv in im.maps)
1707 maps[kv.Key] = kv.Value;
1712 if (im.maps.TryGetValue (target_name, out map))
1713 maps[target_name] = map;
1716 else if (target_type == Mstate)
1718 if (target_name == MSymbol.nil)
1720 for (p = im.states; ! p.IsEmpty; p = p.next)
1721 states.Add (p.key, p.val);
1725 object state = im.states.Get (target_name);
1727 states.Add (target_name, state);
1732 private Xex.Term parse_cond (MPlist plist)
1734 Xex.Term[] args = new Xex.Term[plist.Count];
1736 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1738 if (! plist.IsPlist)
1739 throw new Exception ("Invalid cond args: " + plist);
1740 MPlist p = plist.Plist;
1741 List<Xex.Term> arg = new List<Xex.Term> (parse_action (p));
1742 args[i] = new Xex.Term (arg);
1744 return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
1747 private Xex.Term[] parse_action (MPlist plist)
1749 Xex.Term[] terms = new Xex.Term[plist.Count];
1751 for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
1754 terms[i] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
1755 else if (plist.IsMText)
1756 terms[i] = new Xex.Term ((string) plist.Text);
1757 else if (plist.IsInteger)
1758 terms[i] = new Xex.Term (plist.Integer);
1759 else if (plist.IsPlist)
1761 MPlist p = plist.Plist;
1763 throw new Exception ("Invalid action: " + p);
1764 MSymbol name = p.Symbol;
1767 terms[i] = parse_cond (p);
1768 else if (name == Mset || name == Madd || name == Msub
1769 || name == Mmul || name == Mdiv)
1772 throw new Exception ("Invalid action: " + p);
1773 Xex.Symbol varname = p.Symbol.Name;
1774 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1775 varname, parse_action (p.next));
1778 terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
1782 throw new Exception ("Invalid action: " + plist);
1788 private void parse_macros (MPlist plist)
1790 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1793 MPlist p = pl.Plist;
1797 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
1799 for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
1802 MPlist p = pl.Plist;
1807 domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
1808 parse_action (p.next), false);
1812 private void parse_maps (MPlist plist)
1814 for (; ! plist.IsEmpty; plist = plist.next)
1817 MPlist pl = plist.Plist;
1821 Map map = new Map ();
1822 map.name = pl.Symbol;
1823 maps[map.name] = map;
1824 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1828 MPlist p = pl.Plist;
1831 keys = new KeySeq (p.Text);
1833 keys = new KeySeq (p.Plist);
1840 new Xex.Term (domain, Nprogn, parse_action (p)));
1845 private void parse_states (MPlist plist)
1847 for (; ! plist.IsEmpty; plist = plist.next)
1850 MPlist pl = plist.Plist;
1861 State state = new State (pl.Symbol);
1862 state.title = title;
1864 states = new MPlist ();
1865 states.Add (state.name, state);
1866 for (pl = pl.next; ! pl.IsEmpty; pl = pl.next)
1870 MPlist p = pl.Plist;
1873 MSymbol map_name = p.Symbol;
1875 Xex.Term term = new Xex.Term (domain, Nprogn, parse_action (p));
1876 state.branches.Add (map_name, term);
1881 private static Xex.Term Finsert (Xex.Domain domain, Xex.Variable vari,
1884 ((Context) domain.context).insert (args[0]);
1888 private static Xex.Term Finsert_candidates (Xex.Domain domain,
1892 ((Context) domain.context).insert_candidates (args[0]);
1896 private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
1899 Context ic = (Context) domain.context;
1900 Marker m = (Marker) args[0].Objval;
1902 return new Xex.Term (ic.char_at (m.Position (ic)));
1905 private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
1908 ((Context) domain.context).delete ((int) args[0].Intval);
1912 private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
1915 Candidates can = ((Context) domain.context).candidates;
1918 ((Selector) args[0].Objval).Select (can);
1922 private static Xex.Term Fshow (Xex.Domain domain, Xex.Variable vari,
1925 ((Context) domain.context).show ();
1929 private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
1932 ((Context) domain.context).hide ();
1936 private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
1940 ((Context) domain.context).move (args[0].Intval);
1943 Marker m = (Marker) args[0].Objval;
1944 Context ic = (Context) domain.context;
1945 ((Context) domain.context).move (m.Position (ic));
1950 private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
1953 Marker m = (Marker) args[0].Objval;
1954 m.Mark ((Context) domain.context);
1958 private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
1961 Context ic = (Context) domain.context;
1964 ic.pushback (args[0].Intval);
1965 else if (args[0].IsStr)
1966 ic.pushback (new KeySeq (args[0].Strval));
1968 ic.pushback ((KeySeq) args[0].Objval);
1972 private static Xex.Term Fpop (Xex.Domain domain, Xex.Variable vari,
1975 ((Context) domain.context).pop ();
1979 private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
1982 int n = args.Length == 0 ? -2 : args[0].Intval;
1983 ((Context) domain.context).undo (n);
1987 private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
1990 ((Context) domain.context).commit ();
1994 private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
1997 ((Context) domain.context).commit ();
1998 args = new Xex.Term[2];
1999 args[0] = args[1] = Tcatch_tag;
2000 return Xex.Fthrow (domain, vari, args);
2003 private static Xex.Term Fshift (Xex.Domain domain, Xex.Variable vari,
2006 ((Context) domain.context).shift (args[0].Symval);
2010 private static Xex.Term Fshift_back (Xex.Domain domain, Xex.Variable vari,
2013 ((Context) domain.context).shift_back ();
2017 private static Xex.Term Fkey_count (Xex.Domain domain, Xex.Variable vari,
2020 return new Xex.Term (((Context) domain.context).key_head);
2023 private static Xex.Term Fsurrounding_flag (Xex.Domain domain,
2027 return new Xex.Term (((Context) domain.context).surrounding_flag);
2030 public override string ToString ()
2032 string str = (String.Format ("({0} (title \"{1}\")", tag, title));
2033 if (commands != null)
2035 str += " (commands";
2036 foreach (Command cmd in commands)
2040 if (var_names != null)
2042 str += " (variables";
2043 foreach (Xex.Symbol var in var_names)
2047 if (plugins != null)
2050 foreach (KeyValuePair<MSymbol, Plugin> kv in plugins)
2051 str += " " + kv.Value;
2055 foreach (KeyValuePair<MSymbol, Map> kv in maps)
2056 str += " " + kv.Value;
2058 foreach (MPlist p in states)
2063 public class Context
2065 internal static Xex.Symbol Ncandidates_group_size
2066 = "candidates-group-size";
2067 public MInputMethod im;
2068 private MText produced;
2069 private bool active;
2070 private MText status;
2071 internal MText preedit;
2072 internal int cursor_pos;
2073 internal Candidates candidates;
2074 private int candidate_from, candidate_to;
2075 private bool candidate_show;
2077 private Stack<State> state_stack;
2078 internal KeySeq keys;
2079 internal int key_head;
2080 private int state_key_head;
2081 private object state_var_values;
2082 private int commit_key_head;
2083 private MText state_preedit;
2084 private int state_pos;
2085 internal MPlist markers = new MPlist ();
2086 internal MText preceding_text = new MText ();
2087 internal MText following_text = new MText ();
2088 private bool key_unhandled;
2090 internal Xex.Domain domain;
2092 internal ChangedStatus changed;
2094 public ChangedStatus Changed { get { return changed; } }
2096 public Context (MInputMethod im)
2099 domain = new Xex.Domain (im.domain, this);
2100 state_stack = new Stack<State> ();
2101 state_stack.Push ((State) im.states.val);
2102 keys = new KeySeq ();
2105 private void adjust_markers (int from, int to, object inserted)
2107 int ins = (inserted == null ? 0
2108 : inserted is int ? 1
2109 : ((MText) inserted).Length);
2110 int diff = ins - (to - from);
2112 for (MPlist plist = markers; ! plist.IsEmpty; plist = plist.next)
2114 int pos = plist.Integer;
2118 plist.val = pos + diff;
2123 if (cursor_pos >= to)
2125 else if (cursor_pos > from)
2129 private void preedit_replace (int from, int to, int c)
2131 preedit.Del (from, to);
2132 preedit.Ins (from, c);
2133 adjust_markers (from, to, c);
2136 private void preedit_replace (int from, int to, MText mt)
2138 preedit[from, to] = mt;
2139 adjust_markers (from, to, mt);
2142 internal void insert (Xex.Term arg)
2145 preedit_replace (cursor_pos, cursor_pos, arg.Intval);
2147 preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
2148 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2151 private void update_candidate ()
2153 object candidate = candidates.Current;
2155 if (candidate is MText)
2157 preedit_replace (candidate_from, candidate_to, (MText) candidate);
2158 candidate_to = candidate_from + ((MText) candidate).Length;
2162 preedit_replace (candidate_from, candidate_to, (int) candidate);
2163 candidate_to = candidate_from + 1;
2165 preedit.PushProp (candidate_from, candidate_to,
2167 cursor_pos = candidate_from;
2168 changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
2172 internal void insert_candidates (Xex.Term arg)
2175 Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
2178 column = v.Value.Intval;
2179 candidates = new Candidates (arg.Listval, column);
2180 candidate_from = candidate_to = cursor_pos;
2181 update_candidate ();
2184 internal void select (int n)
2186 if (candidates != null)
2188 candidates.Select (n);
2189 update_candidate ();
2193 internal int marker (MSymbol sym)
2195 int pos = cursor_pos;
2197 if (sym.Name.Length == 2 && sym.Name[0] == '@')
2199 switch (sym.Name[0])
2201 case '<': pos = 0; break;
2202 case '>': pos = preedit.Length; break;
2203 case '-': pos = cursor_pos - 1; break;
2204 case '+': pos = cursor_pos + 1; break;
2209 preedit.FindProp (Mcandidates, pos - 1,
2216 if (cursor_pos < preedit.Length - 1)
2219 preedit.FindProp (Mcandidates, pos,
2223 pos = preedit.Length;
2226 if (sym.Name[0] >= '0' && sym.Name[0] <= '9')
2231 else if (sym.Name.Length >= 3 && sym.Name[0] == '@')
2233 pos = int.Parse (sym.Name.Substring (2));
2237 object val = markers.Get (sym);
2245 internal int char_at (int pos)
2252 if (preceding_text.Length < -pos)
2254 MPlist plist = new MPlist ();
2255 plist.Push (MSymbol.integer, pos);
2256 if (GetSurroundingText != null
2257 && GetSurroundingText (this, plist)
2259 && preceding_text.Length < plist.Text.Length)
2260 preceding_text = plist.Text;
2262 c = (-pos < preceding_text.Length
2263 ? preceding_text[preceding_text.Length + pos] : -1);
2265 else if (pos >= 0 && pos < preedit.Length)
2269 pos -= preedit.Length;
2270 if (pos >= following_text.Length)
2272 MPlist plist = new MPlist ();
2273 plist.Push (MSymbol.integer, pos + 1);
2274 if (GetSurroundingText != null
2275 && GetSurroundingText (this, plist)
2277 && following_text.Length < plist.Text.Length)
2278 following_text = plist.Text;
2280 c = (pos < following_text.Length ? following_text[pos] : -1);
2285 internal void delete (int pos)
2287 if (pos < cursor_pos)
2288 preedit_replace (pos, cursor_pos, null);
2290 preedit_replace (cursor_pos, pos, null);
2291 changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
2294 internal void show ()
2296 candidate_show = true;
2297 changed |= ChangedStatus.CandidateShow;
2300 internal void hide ()
2302 candidate_show = false;
2303 changed |= ChangedStatus.CandidateShow;
2306 internal void move (int pos)
2310 else if (pos > preedit.Length)
2311 pos = preedit.Length;
2312 if (pos != cursor_pos)
2315 changed |= ChangedStatus.Preedit;
2319 internal void mark (MSymbol sym)
2321 MPlist slot = markers.Find (sym);
2324 markers.Push (sym, cursor_pos);
2326 slot.val = cursor_pos;
2329 internal void pushback (int n)
2342 if (key_head > keys.keyseq.Count)
2343 key_head = keys.keyseq.Count;
2347 internal void pushback (KeySeq keyseq)
2351 if (key_head < keys.keyseq.Count)
2352 keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
2353 for (int i = 0; i < keyseq.keyseq.Count; i++)
2354 keys.keyseq.Add (keyseq.keyseq[i]);
2357 internal void pop ()
2359 if (key_head < keys.keyseq.Count)
2360 keys.keyseq.RemoveRange (key_head, 1);
2363 internal void undo (int n)
2366 keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
2368 keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
2372 internal void commit ()
2374 produced.Cat (preedit);
2376 changed |= ChangedStatus.Preedit;
2379 internal void shift (MSymbol sym)
2383 if (sym == MSymbol.t)
2385 if (state_stack.Count > 1)
2386 state = state_stack.Pop ();
2388 state = state_stack.Peek ();
2392 state = (State) im.states.Get (sym);
2394 throw new Exception ("Unknown state: " + state.name);
2397 state = state_stack.Pop ();
2398 if (state == (State) im.states.val)
2405 state_key_head = key_head;
2406 state_pos = cursor_pos;
2407 state_preedit = preedit.Dup ();
2408 if (state != state_stack.Peek ())
2410 state_stack.Push (state);
2411 state_var_values = domain.SaveValues ();
2412 status = state.title;
2415 changed |= ChangedStatus.StateTitle;
2417 = (Xex) state.branches.Get (MSymbol.t);
2418 if (on_entry != null)
2419 on_entry.Eval (domain);
2424 internal void reset ()
2427 state_preedit.Del ();
2431 key_head = commit_key_head = 0;
2432 state_stack.Clear ();
2433 state_stack.Push ((State) im.states.Val);
2438 internal object GetCandidates (out int column)
2441 if (cursor_pos == 0)
2443 Candidates candidates
2444 = (Candidates) preedit.GetProp (cursor_pos - 1, Mcandidates);
2445 if (candidates == null)
2447 column = candidates.Column;
2448 return candidates.Current;
2451 internal void HandleKey ()
2455 public bool Toggle ()