+ public static void Detach (Context ic)
+ {
+ ic.preedit.PopProp (0, ic.preedit.Length, Mcandidates);
+ ic.candidates = null;
+ ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
+ | CandidateAll);
+ }
+
+ // Fill the array "group" by candidates stating from INDEX.
+ // INDEX must be a multiple of "column". Set NTIMES to the
+ // number of valid candidates in "group". Update "block" if
+ // necessary. Return "group".
+
+ private int fill_group (int start)
+ {
+ int nitems = group.Length;
+ int r = row;
+ Block b = blocks[r];
+
+ if (start < b.Index)
+ while (start < b.Index)
+ b = blocks[--r];
+ else
+ while (start >= b.Index + b.Count)
+ b = blocks[++r];
+ row = r;
+
+ int count = b.Count;
+ start -= b.Index;
+ for (int i = 0; i < nitems; i++, start++)
+ {
+ if (start >= count)
+ {
+ r++;
+ if (r == blocks.Length)
+ return i;
+ b = blocks[r];
+ count = b.Count;
+ start = 0;
+ }
+ group[i] = b[start];
+ }
+ return nitems;
+ }
+
+ // Update "row" to what contains the first candidate of
+ // the previous candidate-group, update "current_index", and
+ // update "group" if necessary. Return the previous
+ // candidate-group. Set NITEMS to the number of valid
+ // candidates contained in that group.
+
+ public int PrevGroup ()
+ {
+ int nitems;
+ int col = Column;
+
+ if (IsFixed)
+ {
+ nitems = group.Length;
+ if ((index -= col + nitems) < 0)
+ index = (Total / nitems) * nitems;
+ nitems = fill_group (index);
+ }
+ else
+ {
+ row = row > 0 ? row-- : blocks.Length - 1;
+ nitems = blocks[row].Count;
+ index = blocks[row].Index;
+ }
+ index += col < nitems ? col : nitems - 1;
+ return nitems;
+ }
+
+ public int NextGroup ()
+ {
+ int nitems;
+ int col = Column;
+
+ if (IsFixed)
+ {
+ nitems = group.Length;
+ if ((index += nitems - col) >= Total)
+ index = 0;
+ nitems = fill_group (index);
+ }
+ else
+ {
+ row = row < blocks.Length - 1 ? row + 1 : 0;
+ nitems = blocks[row].Count;
+ index = blocks[row].Count;
+ }
+ index += col < nitems ? col : nitems - 1;
+ return nitems;
+ }
+
+ public void Prev ()
+ {
+ int col = Column;
+
+ if (col == 0)
+ {
+ int nitems = PrevGroup ();
+ index += col < nitems - 1 ? col : nitems - 1;
+ }
+ else
+ index--;
+ }
+
+ public void Next ()
+ {
+ int col = Column;
+ int nitems = GroupLength;
+
+ if (col == nitems - 1)
+ {
+ nitems = NextGroup ();
+ index -= Column;
+ }
+ else
+ index++;
+ }
+
+ public void First ()
+ {
+ index -= Column;
+ }
+
+ public void Last ()
+ {
+ index += GroupLength - (Column + 1);
+ }
+
+ public void Select (int col)
+ {
+ int maxcol = GroupLength - 1;
+ if (col > maxcol)
+ col = maxcol;
+ index = index - Column + col;
+ }
+ }
+
+ internal class Selector : Xex.TermValue
+ {
+ static new Dictionary<MSymbol, Selector> selectors;
+
+ static Selector ()
+ {
+ selectors = new Dictionary<MSymbol, Selector> ();
+ MSymbol[] symlist = new MSymbol[] { "@<", "@=", "@>", "@-", "@+",
+ "@[", "@]" };
+ foreach (MSymbol s in symlist)
+ selectors[s] = new Selector (s);
+ selectors["@first"] = new Selector ('<');
+ selectors["@current"] = new Selector ('=');
+ selectors["@last"] = new Selector ('>');
+ selectors["@previous"] = new Selector ('-');
+ selectors["@next"] = new Selector ('+');
+ selectors["@previous-candidate-change"] = new Selector ('[');
+ selectors["@next-candidate-change"] = new Selector (']');
+ }
+
+ private char tag;
+
+ private Selector (MSymbol sym) { tag = sym.Name[1]; }
+
+ private Selector (char tag) { this.tag = tag; }
+
+ public static Xex.TermValue parser (Xex.Domain domain, XmlNode node)
+ {
+ return Get ((MSymbol) node.InnerText);
+ }
+
+ public static Xex.TermValue Get (MSymbol name)
+ {
+ Selector selector;
+ if (! selectors.TryGetValue (name, out selector))
+ throw new Exception ("Invalid selector name: " + name);
+ return selector;
+ }
+
+ public void Select (Candidates candidates)
+ {
+ switch (tag)
+ {
+ case '<': candidates.First (); break;
+ case '>': candidates.Last (); break;
+ case '-': candidates.Prev (); break;
+ case '+': candidates.Next (); break;
+ case '[': candidates.PrevGroup (); break;
+ case ']': candidates.NextGroup (); break;
+ default: break;
+ }
+ }
+ }
+
+ internal class Map
+ {
+ public MSymbol name;
+ public List<Entry> entries = new List<Entry> ();
+
+ public Map (MSymbol name) { this.name = name; }
+
+ public class Entry
+ {
+ public KeySeq keyseq;
+ public Xex.Term[] actions;
+
+ public Entry (Xex.Domain domain, KeySeq keyseq, Xex.Term[] actions)
+ {
+ this.keyseq = keyseq;
+ this.actions = actions;
+ }
+ }
+
+ public override string ToString ()
+ {
+ string str = "(" + name;
+ foreach (Entry e in entries)
+ str += " " + e.keyseq.ToString ();
+ return str + ")";
+ }
+ }
+
+ internal class Keymap
+ {
+ public Dictionary<Key, Keymap> submaps;
+ public Xex.Term[] map_actions, branch_actions;
+
+ public Keymap () { }
+
+ public void Add (KeySeq keys, int index,
+ Xex.Term[] map_actions, Xex.Term[] branch_actions)
+ {
+ if (index == keys.keyseq.Count)
+ {
+ this.map_actions = map_actions;
+ this.branch_actions = branch_actions;
+ }
+ else
+ {
+ Key key = keys.keyseq[index];
+ Keymap sub = null;
+
+ if (submaps == null)
+ submaps = new Dictionary<Key, Keymap> ();
+ else
+ submaps.TryGetValue (key, out sub);
+ if (sub == null)
+ submaps[key] = sub = new Keymap ();
+ sub.Add (keys, index + 1, map_actions, branch_actions);
+ }
+ }
+
+ public void AddMap (Map map, Xex.Term[] branch_actions)
+ {
+ foreach (Map.Entry entry in map.entries)
+ Add (entry.keyseq, 0, entry.actions, branch_actions);
+ }
+
+ public Keymap Lookup (KeySeq keys, ref int index)
+ {
+ Keymap sub;
+
+ if (index < keys.keyseq.Count
+ && submaps != null
+ && submaps.TryGetValue (keys.keyseq[index], out sub))
+ {
+ index++;
+ return sub.Lookup (keys, ref index);
+ }
+ return this;
+ }
+
+ private void describe (MText mt, KeySeq keyseq)
+ {
+ if (map_actions != null || branch_actions != null)
+ {
+ if (mt.Length > 0)
+ mt.Cat (" ");
+ mt.Cat ('(').Cat (keyseq.ToString ());
+ if (map_actions != null)
+ foreach (Xex.Term term in map_actions)
+ mt.Cat (' ').Cat (term.ToString ());
+ if (branch_actions != null)
+ foreach (Xex.Term term in branch_actions)
+ mt.Cat (' ').Cat (term.ToString ());
+ mt.Cat (')');
+ }
+ if (submaps != null)
+ foreach (KeyValuePair<Key, Keymap> kv in submaps)
+ {
+ keyseq.keyseq.Add (kv.Key);
+ kv.Value.describe (mt, keyseq);
+ keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
+ }
+ }
+
+ public override string ToString ()
+ {
+ MText mt = "";
+ KeySeq keyseq = new KeySeq ();
+
+ describe (mt, keyseq);
+ return (string) mt;
+ }
+ }
+
+ internal class State
+ {
+ public Xex.Symbol name;
+ public MText title;
+ public Xex.Term[] enter_actions, fallback_actions;
+ public Keymap keymap = new Keymap ();
+
+ public State (Xex.Symbol name, MText title)
+ {
+ this.name = name;
+ this.title = title;
+ }
+
+ public State (MInputMethod im, XmlNode node)
+ {
+ this.name = node.Attributes[Qsname].Value;
+ XmlAttribute attr = node.Attributes[Qtitle];
+ if (attr != null)
+ title = (MText) attr.Value;
+ else
+ title = im.title;
+ keymap = new Keymap ();
+ for (node = node.FirstChild; node != null; node = node.NextSibling)
+ {
+ if (node.Name == Qstate_hook)
+ enter_actions = Xex.ParseTerms (im.domain, node.FirstChild);
+ else if (node.Name == Qcatch_all_branch)
+ fallback_actions = Xex.ParseTerms (im.domain, node.FirstChild);
+ else if (node.Name == Qbranch)
+ {
+ MSymbol mapname = node.Attributes[Qmname].Value;
+ Map map;
+ if (im.maps.TryGetValue (mapname, out map))
+ keymap.AddMap (map, Xex.ParseTerms (im.domain,
+ node.FirstChild));
+ else
+ throw new Exception ("Unknown map: " + mapname);
+ }
+ }
+ }
+
+ public State (MInputMethod im, MPlist plist)
+ {
+ if (! plist.IsSymbol)
+ throw new Exception ("Invalid state: " + plist);
+ this.name = plist.Symbol.Name;
+ plist = plist.next;
+ if (plist.IsMText)
+ {
+ title = plist.Text;
+ plist = plist.next;
+ }
+ else
+ title = im.title;
+ keymap = new Keymap ();
+ for (; ! plist.IsEmpty; plist = plist.next)
+ {
+ if (! plist.IsPlist)
+ throw new Exception ("Invalid branch: " + plist);
+ MPlist p = plist.Plist;
+ if (! p.IsSymbol)
+ throw new Exception ("Invalid branch: " + p);
+ MSymbol mapname = p.Symbol;
+ if (mapname == MSymbol.t)
+ enter_actions = im.parse_actions (p.next, false);
+ else if (mapname == MSymbol.nil)
+ fallback_actions = im.parse_actions (p.next, false);
+ else
+ {
+ Map map;
+ if (im.maps.TryGetValue (mapname, out map))
+ keymap.AddMap (map, im.parse_actions (p.next, false));
+ else
+ throw new Exception ("Unknown map: " + mapname);
+ }
+ }
+ }
+
+ public override string ToString ()
+ {
+ MText mt = "(" + name;
+
+ if (title != null)
+ mt.Cat (" \"" + title + "\"");
+ mt.Cat (keymap.ToString ());
+ return (string) mt + ")";
+ }
+ }
+
+ // Instance members
+ internal Xex.Domain domain;
+
+ protected LoadStatus load_status = LoadStatus.None;
+ protected MDatabase.Tag tag;
+ private MDatabase mdb;