private static MSymbol Mvariable = "variable";
private static MSymbol Mcommand = "command";
private static MSymbol Mmodule = "module";
- private static MSymbol Mmodule_list = "module-list";
private static MSymbol Mtitle = "title";
private static MSymbol Minclude = "include";
private static MSymbol Mmacro = "macro";
- private static MSymbol Mmacro_list = "macro-list";
private static MSymbol Mmap = "map";
private static MSymbol Mmap_list = "map-list";
private static MSymbol Mstate = "state";
- private static MSymbol Mstate_list = "state-list";
internal static MSymbol Mcandidates = "candidates";
private static MSymbol Minsert = "insert";
private static MSymbol Mdelete = "delete";
private static MSymbol Mmove = "move";
private static MSymbol Mmark = "mark";
private static MSymbol Mmarker = "marker";
+ private static MSymbol Mset = "set";
private static MSymbol Madd = "add";
private static MSymbol Msub = "sub";
private static MSymbol Mmul = "mul";
private static MSymbol Mpushback = "pushback";
private static MSymbol Mkeyseq = "keyseq";
+ private static Xex.Symbol Nprogn = "progn";
+ private static Xex.Term Tnil = new Xex.Term ((Xex.Symbol) "nil");
+ private static Xex.Term Tcatch_tag = new Xex.Term ((Xex.Symbol) "@mimtag");
+
private static Dictionary<MDatabase.Tag, MInputMethod> im_table
= new Dictionary<MDatabase.Tag, MInputMethod> ();
- // Sub classes
- private class Exception : System.Exception
- {
- bool error;
-
- public Exception (string msg) : base (msg)
- {
- error = true;
- }
-
- public Exception (string fmt, params object[] args)
- : base (String.Format (fmt, args))
- {
- error = true;
- }
-
- public Exception (string msg, bool error) : base (msg)
- {
- this.error = error;
- }
- }
+ internal static MInputMethod im_global = null;
[FlagsAttribute]
private enum LoadStatus
}
}
- public class KeySeq : Xex.Object
+ public class KeySeq : Xex.TermValue
{
- List<Key> keyseq = new List<Key> ();
+ public List<Key> keyseq = new List<Key> ();
+
+ public override Xex.TermValue Clone ()
+ {
+ KeySeq ks = new KeySeq ();
+ ks.keyseq.InsertRange (0, keyseq);
+ return ks;
+ }
public KeySeq () { }
{
foreach (Xex.Term term in args)
{
- if (term.IsName)
- keyseq.Add (new Key ((MSymbol) term.Nameval.name));
+ if (term.IsSymbol)
+ keyseq.Add (new Key ((MSymbol) ((string) term.Symval)));
else
keyseq.Add (new Key (term.Strval));
}
{
string str;
- foreach (Key key in this)
+ foreach (Key key in keyseq)
if (key.HasModifier)
{
str = "(keyseq";
- foreach (Key k in this)
+ foreach (Key k in keyseq)
str += " " + k.ToString ();
return str + ")";
}
str = "\"";
- foreach (Key key in this)
+ foreach (Key key in keyseq)
str += key.ToString ();
return str + "\"";
}
internal class Plugin
{
- public string name;
- public Assembly assembly;
- public MPlist methods;
+ private string name;
+ private Assembly assembly;
+ private Type plugin_type;
- public override string ToString ()
+ public Plugin (string name)
{
- string str = "(" + name;
- for (MPlist p = methods; ! p.IsEmpty; p = p.next)
- str += " " + p.key;
- return str + ")";
+ this.name = name;
}
- public Xex.Term Call (MSymbol method, MInputContext ic, Xex.Term[] args)
+ public MethodInfo GetMethod (Xex.Symbol name)
{
if (assembly == null)
{
- try {
- assembly = Assembly.LoadFrom (name + ".dll");
- } catch {
- return Xex.Zero;
- }
- Type t = assembly.GetType ("Plugin");
- for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
- p.Set (p.key, t.GetMethod (p.key.Name));
+ assembly = Assembly.LoadFrom (name + ".dll");
+ plugin_type = assembly.GetType ("M17n.MInputMethod.Plugin");
}
- MethodInfo method_info = (MethodInfo) methods.Get (method);
- if (method_info == null)
- return Xex.Zero;
- Xex.Term result = (Xex.Term) method_info.Invoke (null, args);
- return result.Eval ();
+ MethodInfo info = plugin_type.GetMethod ((string) name);
+ if (info == null)
+ throw new Exception ("Invalid plugin method: " + name);
+ return info;
+ }
+
+ public override string ToString ()
+ {
+ return String.Format ("(module {0}", name);
}
}
- internal class PluginMethod : Function
+ internal class PluginMethod : Xex.Function
{
- public Plugin plugin;
- public MSymbol method;
+ private Plugin plugin;
+ private MethodInfo method_info;
+ object[] parameters = new object[2];
- public PluginMethod (Plugin plugin, MSymbol name)
- : base ((Name) name.name, 0, -1)
+ public PluginMethod (Plugin plugin, string name)
+ : base ((Xex.Symbol) name, 0, -1)
{
this.plugin = plugin;
- method = name.name.Substring (plugin.name.Length + 1);
}
- public override Term Call (Xex.Domain domain, Xex.Variable vari,
- Xex.Term[] args)
+ public override Xex.Term Call (Xex.Domain domain, Xex.Variable vari,
+ Xex.Term[] args)
{
- Xex.Term[] args = (Xex.Term[]) args.Clone ();
+ args = (Xex.Term[]) args.Clone ();
for (int i = 0; i < args.Length; i++)
{
args[i] = args[i].Eval (domain);
if (domain.Thrown)
return args[i];
}
- return plugin.Call (method, (MIntutContext) domain.context, args);
+ if (method_info == null)
+ method_info = plugin.GetMethod (name);
+ parameters[0] = domain.context;
+ parameters[1] = args;
+ return (Xex.Term) method_info.Invoke (null, parameters);
}
}
- internal class PluginCall : Xex.TermValue
- {
- PluginMethod method;
- Xex.Term[] args;
-
- public PluginCall (Plugin plugin, MSymbol entry, Xex.Term[] args)
- {
- this.plugin = plugin;
- this.entry = entry;
- this.args = args;
- }
-
- public override Xex.Term Eval (Xex.Domain domain)
- {
- Xex.Term[] args = new Xex.Term[this.args.Length];
- for (int i = 0; i < args.Length; i++)
- args[i] = this.args[i].Eval (domain);
- return plugin.Call (entry, args);
- }
- }
-
internal abstract class Marker : Xex.TermValue
{
MSymbol name;
- MInputContext ic;
- public Marker (MSymbol name, MInputContext ic)
+ public Marker (MSymbol name)
{
this.name = name;
- this.ic = ic;
}
- public abstract int Position { get; set; }
- }
+ public abstract int Position (MInputContext ic);
+ public abstract void Mark (MInputContext ic);
- internal class NamedMarker : Marker
- {
- int position;
+ public class Named : Marker
+ {
+ int pos;
+
+ public Named (MSymbol name) : this (name, 0) { }
+
+ private Named (MSymbol name, int p) : base (name) { pos = p; }
- public NamedMaker (MSymbol name, MInputContext ic) : base (name, ic)
+ public override int Position (MInputContext ic) { return pos; }
+
+ public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
+
+ public override Xex.TermValue Clone ()
{
- ic.markers.Put (name, this);
+ return new Named (name, pos);
}
-
- public override int Position {
- get { return position; }
- set { position = value; }
}
- }
- internal class SystemMarker : Marker
- {
- public SystemMarker (MSymbol name, MInputContext ic) : base (name, ic)
- {
- }
+ public class Predefined : Marker
+ {
+ public Predefined (MSymbol name) : base (name) { }
- public override int Position {
- get {
- switch (name.Name[1])
- {
- case '<': return 0;
- case '>': return ic.preedit.Length;
- case '-': return ic.cursor_pos - 1;
- case '+': return ic.cursor_pos + 1;
- case '[':
- if (ic.cursor_pos > 0)
- {
- int pos = ic.cursor_os;
- int to;
- ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
- out pos, out to);
- return pos;
- }
- return 0;
- case ']':
- if (ic.cursor_pos < ic.preedit.Length - 1)
- {
- int pos = ic.cursor_pos;
- int from;
- ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
- out from, out pos);
- return pos;
- }
- return ic.preedit.Length;
- default:
- return name.Name[1] - '0';
- }
+ public override int Position (MInputContext ic)
+ {
+ switch (name.Name[1]) {
+ case '<': return 0;
+ case '>': return ic.preedit.Length;
+ case '-': return ic.cursor_pos - 1;
+ case '+': return ic.cursor_pos + 1;
+ case '[':
+ if (ic.cursor_pos > 0)
+ {
+ int pos = ic.cursor_pos;
+ int to;
+ ic.preedit.FindProp (MInputMethod.Mcandidates, pos - 1,
+ out pos, out to);
+ return pos;
+ }
+ return 0;
+ case ']':
+ if (ic.cursor_pos < ic.preedit.Length - 1)
+ {
+ int pos = ic.cursor_pos;
+ int from;
+ ic.preedit.FindProp (MInputMethod.Mcandidates, pos,
+ out from, out pos);
+ return pos;
+ }
+ return ic.preedit.Length;
+ default:
+ return name.Name[1] - '0';
+ }
}
- set {
+
+ public override void Mark (MInputContext ic)
+ {
throw new Exception ("Can't set predefined marker: " + name);
}
+
+ public override Xex.TermValue Clone ()
+ {
+ return new Predefined (name);
+ }
+ }
+
+ static internal Dictionary<MSymbol,Predefined> predefined_markers;
+
+ static Marker ()
+ {
+ predefined_markers = new Dictionary<MSymbol,Predefined> ();
+ MSymbol[] symlist = new MSymbol[] {"@<", "@>", "@-", "@+", "@[", "@]",
+ "@0", "@1", "@2", "@3", "@4",
+ "@5", "@6", "@7", "@8", "@9" };
+ foreach (MSymbol s in symlist)
+ predefined_markers[s] = new Predefined (s);
+ }
+
+ public static Marker Get (MInputContext ic, MSymbol name)
+ {
+ Predefined pred;
+ Marker m;
+
+ if (predefined_markers.TryGetValue (name, out pred))
+ return pred;
+ if (name.Name[0] == '@')
+ throw new Exception ("Invalid marker name: " + name);
+ m = (Marker) ic.markers.Get (name);
+ if (m == null)
+ {
+ m = new Named (name);
+ ic.markers.Put (name, m);
+ }
+ return m;
}
}
{
public MSymbol name;
public Dictionary<Key, Map> submaps;
- public Xex actions;
+ public Xex.Term actions;
- public void Add (KeySeq keys, int index, Xex actions)
+ public void Add (KeySeq keys, int index, Xex.Term actions)
{
Map sub = null;
if (submaps == null)
submaps = new Dictionary<Key, Map> ();
else
- submaps.TryGetValue (keys[index], out sub);
+ submaps.TryGetValue (keys.keyseq[index], out sub);
if (sub == null)
{
- Key key = keys[index];
+ Key key = keys.keyseq[index];
submaps[key] = sub = new Map ();
}
- if (index + 1 < keys.Count)
+ if (index + 1 < keys.keyseq.Count)
sub.Add (keys, index + 1, actions);
else
this.actions = actions;
}
- public Xex Lookup (KeySeq keys, int index)
+ public Xex.Term Lookup (KeySeq keys, int index)
{
Map sub;
- if (index + 1 == keys.Count)
+ if (index + 1 == keys.keyseq.Count)
return actions;
- if (submaps.TryGetValue (keys[index], out sub))
+ if (submaps.TryGetValue (keys.keyseq[index], out sub))
return sub.Lookup (keys, index + 1);
- return null;
+ return Tnil;
}
private void describe (MText mt, KeySeq keyseq)
{
- if (keyseq.Count > 0)
+ if (keyseq.keyseq.Count > 0)
{
mt.Cat (" (").Cat (keyseq.ToString ());
- if (actions != null)
- mt.Cat (' ').Cat (actions.ToString ());
+ mt.Cat (' ').Cat (actions.ToString ());
mt.Cat (')');
}
if (submaps != null)
foreach (KeyValuePair<Key, Map> kv in submaps)
{
- keyseq.Add (kv.Key);
+ keyseq.keyseq.Add (kv.Key);
kv.Value.describe (mt, keyseq);
- keyseq.RemoveAt (keyseq.Count - 1);
+ keyseq.keyseq.RemoveAt (keyseq.keyseq.Count - 1);
}
}
private MText description;
internal MText title;
internal Command[] commands;
- internal Xex.Name[] var_names;
+ internal Xex.Symbol[] var_names;
internal Dictionary<MSymbol, Plugin> plugins;
internal Dictionary<MSymbol, Map> maps;
internal MPlist states;
}
description = this.description;
title = this.title;
- variables = new Xex.Variable[var_names.Length];
- int i = 0;
- foreach (Xex.Name name in var_names)
- variables[i++] = domain.GetVar (name);
+ if (var_names == null)
+ variables = null;
+ else
+ {
+ variables = new Xex.Variable[var_names.Length];
+ int i = 0;
+ foreach (Xex.Symbol name in var_names)
+ variables[i++] = domain.GetVar (name, false);
+ }
commands = this.commands;
return true;
}
private bool load_body ()
{
- domain = new Xex.Domain (domain, null);
+ domain = new Xex.Domain (im_domain, null);
mdb = MDatabase.Find (tag);
if (mdb == null)
return false;
pl = pl.next;
if (sym == Mdescription)
- {
- description = parse_description (pl);
- if (description == null)
- description = new MText ("No description");
- }
+ description = parse_description (pl);
else if (sym == Mtitle)
{
if (pl.IsMText)
description = (MText) "No description";
if (title == null)
title = new MText (tag[2].Name);
- if (variables == null)
- variables = new Variable[0];
if (commands == null)
commands = new Command[0];
if (! full)
State state = new State ((MSymbol) "init");
plist = new MPlist ();
foreach (KeyValuePair<MSymbol, Map>kv in maps)
- state.branches.Add (kv.Key, new Xex (plist, domain));
+ state.branches.Add (kv.Key, null);
states.Add (state.name, state);
}
}
description = (MText) "No description";
if (title == null)
title = new MText (tag[2].Name);
- if (variables == null)
- variables = new Variable[0];
if (commands == null)
commands = new Command[0];
if (! full)
if (states.IsEmpty)
{
State state = new State ((MSymbol) "init");
- MPlist plist = new MPlist ();
foreach (KeyValuePair<MSymbol, Map>kv in maps)
- state.branches.Add (kv.Key, new Xex (plist, domain));
+ state.branches.Add (kv.Key, null);
states.Add (state.name, state);
}
}
return node.InnerText;
}
- private void new_variable (Xex.Name name, string desc, int val,
+ private void new_variable (Xex.Symbol name, string desc, int val,
MPlist pl, Xex.Variable vari)
{
- Xex.Term term = new Xex.Term (val);
int[] range;
if (pl.IsEmpty)
else if (pl.IsInteger)
range[i * 2] = range[i * 2 + 1] = pl.Integer;
else
- throw new Exception ("Invalid range: " + p);
+ throw new Exception ("Invalid range: " + pl);
}
}
if (vari == null)
- domain.Defvar (new Xex.Variable.Int (name, desc, term, range));
+ domain.Defvar (new Xex.Variable.Int (name, desc, val, range));
else
{
+ Xex.Term term = new Xex.Term (val);
vari.Value = term;
vari.DefaultValue = term;
vari.Range = range;
}
}
- private void new_variable (Xex.Name name, string desc, MText val,
+ private void new_variable (Xex.Symbol name, string desc, MText val,
MPlist pl, Xex.Variable vari)
{
- Xex.Term term = new Xex.Term ((string) val);
string[] range;
if (pl.IsEmpty)
for (int i = 0; i < range.Length; i++)
{
if (pl.IsMText)
- range[i] = pl.Text;
+ range[i] = (string) pl.Text;
else
- throw new Exception ("Invalid range: " + p);
+ throw new Exception ("Invalid range: " + pl);
}
}
if (vari == null)
- domain.Defvar (new Xex.Variable.Str (name, desc, term, range));
+ domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range));
else
{
+ Xex.Term term = new Xex.Term ((string) val);
vari.Value = term;
vari.DefaultValue = term;
vari.Range = range;
}
}
- private void new_variable (Xex.Name name, string desc, MSymbol val,
+ private void new_variable (Xex.Symbol name, string desc, MSymbol val,
MPlist pl, Xex.Variable vari)
{
- Xex.Term term = new Xex.Term ((Xex.Name) val.name);
- Xex.Name[] range;
+ Xex.Symbol[] range;
+ Xex.Symbol sym = val.Name;
if (pl.IsEmpty)
range = null;
else
{
- range = new Xex.Name[pl.Count * 2];
+ range = new Xex.Symbol[pl.Count * 2];
for (int i = 0; i < range.Length; i++)
{
if (pl.IsSymbol)
- range[i] = pl.Symbol.name;
+ range[i] = pl.Symbol.Name;
else
- throw new Exception ("Invalid range: " + p);
+ throw new Exception ("Invalid range: " + pl);
}
}
if (vari == null)
- domain.Defvar (new Xex.Variable.Sym (name, desc, term, range));
+ domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range));
else
{
+ Xex.Term term = new Xex.Term (sym);
vari.Value = term;
vari.DefaultValue = term;
vari.Range = range;
}
}
+ private Xex.Variable get_global_var (Xex.Symbol name)
+ {
+ if (im_global == null || this != im_global)
+ {
+ tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil,
+ "global");
+ im_global = im_table[tag];
+ if (! im_global.Open ())
+ throw new Exception ("Failed to load global");
+ }
+ return im_global.domain.GetVar (name, false);
+ }
+
private void parse_variables (MPlist plist)
{
- variables = new Variable[plist.Count];
+ var_names = new Xex.Symbol[plist.Count];
- for (int i = 0; ! plist.IsEmpty; plist = plist.next)
- if (plist.IsPlist && plist.Plist.IsSymbol)
- {
- MPlist p = plist.Plist;
- Xex.Name name = (Name) p.Symbol.name;
- p = p.next;
- string desc = parse_description (p);
- Xex.Variable vari = null;
- Xex.Term val;
+ for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
+ {
+ if (! plist.IsPlist || ! plist.Plist.IsSymbol)
+ throw new Exception ("Invalid variable: " + plist);
- if (im_global.domain.GetVar (name, out vari))
- domain.Defvar (vari);
- if (desc != null)
- p = p.next;
- if (! p.IsEmpty)
- {
- if (p.IsInteger)
- new_variable (name, desc, p.Integer, p.next, vari);
- else if (p.IsMText)
- new_variable (name, desc, p.Text, p.next, vari);
- else if (p.IsSymbol)
- new_variable (name, desc, p.Symbol, p.next, vari);
- else
- throw new Exception ("Unknown type: " + p.val);
- }
- }
+ MPlist p = plist.Plist;
+ Xex.Symbol name = (Xex.Symbol) p.Symbol.Name;
+ var_names[i] = name;
+ p = p.next;
+ string desc = (string) parse_description (p);
+ Xex.Variable vari = get_global_var (name);
+ if (vari != null)
+ domain.Defvar (vari);
+ if (desc != null)
+ p = p.next;
+ if (! p.IsEmpty)
+ {
+ if (p.IsInteger)
+ new_variable (name, desc, p.Integer, p.next, vari);
+ else if (p.IsMText)
+ new_variable (name, desc, p.Text, p.next, vari);
+ else if (p.IsSymbol)
+ new_variable (name, desc, p.Symbol, p.next, vari);
+ else
+ throw new Exception ("Invalid variable type: " + p.val);
+ }
+ }
}
private void parse_variables (XmlNode node)
{
XmlNodeList node_list = node.ChildNodes;
- variables = new Variable[node_list.Count];
+ var_names = new Xex.Symbol[node_list.Count];
for (int i = 0; i < node_list.Count; i++)
- if (node_list[i].NodeType == XmlNodeType.Element)
- variables[i] = new Variable (node_list[i]);
+ {
+ Xex.Symbol name = node_list[i].Attributes[0].Value;
+ Xex.Variable vari = get_global_var (name);
+ if (vari != null)
+ domain.Defvar (vari);
+ else
+ domain.Defvar (node_list[i]);
+
+ var_names[i] = name;
+ }
}
private void parse_commands (MPlist plist)
{
MPlist p = plist.Plist;
MSymbol sym = p.Symbol;
- Plugin plugin = new Plugin ();
+ Plugin plugin = new Plugin (sym.Name);
- plugin.name = sym.Name;
- plugin.methods = new MPlist ();
for (p = p.next; ! p.IsEmpty; p = p.next)
- plugin.methods.Add (p.Symbol, null);
- plugins.Add (sym, plugin);
+ {
+ Xex.Function func = new PluginMethod (plugin, p.Symbol.Name);
+ domain.Defun (func);
+ }
}
}
foreach (XmlNode n in node.ChildNodes)
{
- Plugin plugin = new Plugin ();
- plugin.name = n.Attributes["id"].Value;
- plugin.methods = new MPlist ();
+ Plugin plugin = new Plugin (n.Attributes[0].Value);
foreach (XmlNode nn in n.ChildNodes)
- plugin.methods.Add ((MSymbol) nn.Attributes["id"].Value,
- null);
- plugins.Add (plugin.name, plugin);
+ {
+ Xex.Function func = new PluginMethod (plugin,
+ nn.Attributes[0].Value);
+ domain.Defun (func);
+ }
}
}
private void parse_macros (XmlNode node)
{
for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
- if (nn.NodeType == XmlNodeType.Element
- && nn.Name == "xi:include")
- {
- XmlNode n = nn.FirstChild.FirstChild;
- MSymbol language = n.InnerText;
- n = n.NextSibling;
- MSymbol name = n.InnerText;
- n = n.NextSibling;
- MSymbol subname = (n != null ? n.InnerText : MSymbol.nil);
- n = n.ParentNode.NextSibling;
- MSymbol section = n.InnerText;
- n = n.NextSibling;
- MSymbol id = (n != null ? n.InnerText : MSymbol.nil);
-
- MInputMethod im = MInputMethod.Find (language, name, subname);
- if (im == null || ! im.Open ())
- continue;
- if (id == MSymbol.nil)
- im.domain.CopyFunc (domain);
- else
- im.domain.CopyFunc (domain, id);
- }
- for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
- if (nn.NodeType == XmlNodeType.Element
- && nn.Name != "xi:include")
- domain.Defun ((MSymbol) node.GetAttribute ("id"));
+ if (nn.NodeType == XmlNodeType.Element)
+ domain.Defun (nn, true);
for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
- if (nn.NodeType == XmlNodeType.Element
- && nn.Name != "xi:include")
- domain.Defun ((MSymbol) node.GetAttribute ("id"), null,
- nn.FirstChild);
+ if (nn.NodeType == XmlNodeType.Element)
+ domain.Defun (nn, false);
}
private void parse_maps (XmlNode node)
if (target_name == MSymbol.nil)
im.domain.CopyFunc (domain);
else
- im.domain.CopyFunc (domain, target_name);
+ im.domain.CopyFunc (domain, (Xex.Symbol) target_name.Name);
}
else if (target_type == Mmap)
{
}
}
+ private Xex.Term parse_cond (MPlist plist)
+ {
+ Xex.Term[] args = new Xex.Term[plist.Count];
+
+ for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
+ {
+ if (! plist.IsPlist)
+ throw new Exception ("Invalid cond args: " + plist);
+ MPlist p = plist.Plist;
+ List<Xex.Term> arg = new List<Xex.Term> (parse_action (p));
+ args[i] = new Xex.Term (arg);
+ }
+ return new Xex.Term (domain, (Xex.Symbol) Mcond.Name, args);
+ }
+
+ private Xex.Term[] parse_action (MPlist plist)
+ {
+ Xex.Term[] terms = new Xex.Term[plist.Count];
+
+ for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
+ {
+ if (plist.IsSymbol)
+ terms[i] = new Xex.Term ((Xex.Symbol) plist.Symbol.Name);
+ else if (plist.IsMText)
+ terms[i] = new Xex.Term ((string) plist.Text);
+ else if (plist.IsInteger)
+ terms[i] = new Xex.Term (plist.Integer);
+ else if (plist.IsPlist)
+ {
+ MPlist p = plist.Plist;
+ if (! p.IsSymbol)
+ throw new Exception ("Invalid action: " + p);
+ MSymbol name = p.Symbol;
+ p = p.next;
+ if (name == Mcond)
+ terms[i] = parse_cond (p);
+ else if (name == Mset || name == Madd || name == Msub
+ || name == Mmul || name == Mdiv)
+ {
+ if (! p.IsSymbol)
+ throw new Exception ("Invalid action: " + p);
+ Xex.Symbol varname = p.Symbol.Name;
+ terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
+ varname, parse_action (p.next));
+ }
+ else
+ terms[i] = new Xex.Term (domain, (Xex.Symbol) name.Name,
+ parse_action (p));
+ }
+ else
+ throw new Exception ("Invalid action: " + plist);
+ }
+ return terms;
+ }
+
+
private void parse_macros (MPlist plist)
{
for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
if (! p.IsSymbol)
continue;
- domain.Defun (p.Symbol, null, null);
+ domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null, null, true);
}
for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next)
if (pl.IsPlist)
if (! p.IsSymbol)
continue;
transform (p.next);
- domain.Defun (p.Symbol, null, p.next);
+ domain.Defun ((Xex.Symbol) p.Symbol.Name, false, null,
+ parse_action (p.next), false);
}
}
p = p.next;
if (p.IsEmpty)
continue;
- transform (p);
- MXex expr = new MXex (p, domain);
- map.Add (keys, 0, expr);
+ map.Add (keys, 0,
+ new Xex.Term (domain, Nprogn, parse_action (p)));
}
}
}
continue;
MSymbol map_name = p.Symbol;
p = p.next;
- transform (p);
- state.branches.Add (map_name,
- new MXex (p, domain));
+ Xex.Term term = new Xex.Term (domain, Nprogn, parse_action (p));
+ state.branches.Add (map_name, term);
}
}
}
private static Xex.Term Fmarker (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
- MSymbol sym = (MSymbol) args[0].Args[0].Val;
-
- return ((MInputContext) domain.context).marker (sym);
+ MSymbol name = (string) args[0].Symval;
+ return new Xex.Term (Marker.Get ((MInputContext) domain.context, name));
}
- private static Xex.Term char_at (Xex.Domain domain, Xex.Variable vari,
- Xex.Term[] args)
+ private static Xex.Term Fchar_at (Xex.Domain domain, Xex.Variable vari,
+ Xex.Term[] args)
{
- return ((MInputContext) domain.context).char_at ((int) args[0].Val);
+ int c = ((MInputContext) domain.context).char_at (args[0].Intval);
+ return new Xex.Term (c);
}
private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
((MInputContext) domain.context).delete ((int) args[0].Intval);
- return true;
+ return args[0];
}
private static Xex.Term Fselect (Xex.Domain domain, Xex.Variable vari,
if (args[0].IsInt)
ic.select (args[0].Intval);
else
- ic.select ((MSYmbol) ((string) args[0].Nameval));
+ ic.select ((MSymbol) ((string) args[0].Symval));
return args[0];
}
Xex.Term[] args)
{
((MInputContext) domain.context).show ();
- return Xex.Zero;
+ return Tnil;
}
private static Xex.Term Fhide (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
((MInputContext) domain.context).hide ();
- return Xex.Zero;
+ return Tnil;
}
private static Xex.Term Fmove (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
- ((MInputContext) domain.context).move (args[0].Intval);
+ if (args[0].IsInt)
+ ((MInputContext) domain.context).move (args[0].Intval);
+ else
+ {
+ Marker m = (Marker) args[0].Objval;
+ MInputContext ic = (MInputContext) domain.context;
+ ((MInputContext) domain.context).move (m.Position (ic));
+ }
return args[0];
}
private static Xex.Term Fmark (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
- ((MInputContext) domain.context).mark ((Marker) args[0].Objval);
+ Marker m = (Marker) args[0].Objval;
+ m.Mark ((MInputContext) domain.context);
return args[0];
}
private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
- return new KeySeq (args);
+ return new Xex.Term (new KeySeq (args));
}
private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
- Xex[] args)
+ Xex.Term[] args)
{
MInputContext ic = (MInputContext) domain.context;
Xex.Term[] args)
{
((MInputContext) domain.context).pop ();
- return Xex.Zero;
+ return Tnil;
}
private static Xex.Term Fundo (Xex.Domain domain, Xex.Variable vari,
{
int n = args.Length == 0 ? -2 : args[0].Intval;
((MInputContext) domain.context).undo (n);
- return Xex.Zero;
+ return Tnil;
}
private static Xex.Term Fcommit (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
((MInputContext) domain.context).commit ();
- return Xex.Zero;
+ return Tnil;
}
private static Xex.Term Funhandle (Xex.Domain domain, Xex.Variable vari,
{
((MInputContext) domain.context).commit ();
args = new Xex.Term[2];
- args[0] = args[1] = catch_tag;
+ args[0] = args[1] = Tcatch_tag;
return Xex.Fthrow (domain, vari, args);
}
str += " " + cmd;
str += ")";
}
- if (variables != null)
+ if (var_names != null)
{
str += " (variables";
- foreach (Variable var in variables)
+ foreach (Xex.Symbol var in var_names)
str += " " + var;
str += ")";
}
public class MInputContext
{
- internal static MSymbol Mcandidates_group_size = "candidates-group-size";
+ internal static Xex.Symbol Ncandidates_group_size = "candidates-group-size";
private static MSymbol Mat_less_than = "@<";
private static MSymbol Mat_greater_than = "@>";
private static MSymbol Mat_minus = "@-";
private MText produced;
private bool active;
private MText status;
- private bool status_changed;
- private MText preedit;
- private bool preedit_changed;
- private int cursor_pos;
- private bool cursor_pos_changed;
+ internal MText preedit;
+ internal int cursor_pos;
private Candidates candidates;
private MPlist candidate_group;
private int candidate_index;
private int candidate_from, candidate_to;
private bool candidate_show;
- private bool candidate_changed;
private Stack<MInputMethod.State> states;
internal MInputMethod.KeySeq keys;
private int key_head;
private int state_key_head;
- private MPlist state_boundary;
+ private object state_var_values;
private int commit_key_head;
private MText state_preedit;
private int state_pos;
internal MPlist markers = new MPlist ();
- private MPlist vars;
- private MPlist vars_saved;
internal MText preceding_text = new MText ();
internal MText following_text = new MText ();
private bool key_unhandled;
internal Xex.Domain domain;
+ [FlagsAttribute]
+ public enum ChangedStatus
+ {
+ None = 0x00,
+ StateTitle = 0x01,
+ Preedit = 0x02,
+ CursorPos = 0x04,
+ CandidateList = 0x08,
+ CandidateIndex = 0x10,
+ CandidateShow = 0x20,
+ }
+
+ private static ChangedStatus CandidateAll = (ChangedStatus.CandidateList
+ | ChangedStatus.CandidateIndex
+ | ChangedStatus.CandidateShow);
+ private ChangedStatus changed;
+
+ public ChangedStatus Changed { get { return changed; } }
+
public MInputContext (MInputMethod im)
{
this.im = im;
internal void insert (Xex.Term arg)
{
- if (arg.IsInt is int)
+ if (arg.IsInt)
preedit_replace (cursor_pos, cursor_pos, arg.Intval);
else
preedit_replace (cursor_pos, cursor_pos, new MText (arg.Strval));
- preedit_changed = true;
- cursor_pos_changed = true;
+ changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
}
private class Candidates
public int Index;
public object Data;
+ public Block (int index, Xex.Term term)
+ {
+ Index = index;
+ if (term.IsStr)
+ Data = (MText) term.Strval;
+ else
+ {
+ MPlist plist = new MPlist ();
+ MPlist p = plist;
+ foreach (Xex.Term t in term.Listval)
+ p = p.Add (MSymbol.mtext, (MText) t.Strval);
+ Data = plist;
+ }
+ }
+
public Block (int index, MPlist plist)
{
Index = index;
if (plist.IsMText)
Data = plist.Text;
- else
+ else if (plist.IsPlist)
Data = plist.Plist;
+ else
+ throw new Exception ("Invalid candidate: " + plist);
}
public int Count
{
ic.preedit.PopProp (0, ic.preedit.Length, MInputMethod.Mcandidates);
ic.candidates = null;
- ic.preedit_changed = true;
- ic.cursor_pos_changed = true;
- ic.candidate_changed = true;
+ ic.changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
+ | CandidateAll);
}
// Fill the array "group" by candidates stating from INDEX.
preedit.PushProp (candidate_from, candidate_to,
MInputMethod.Mcandidates, this);
cursor_pos = candidate_from;
- preedit_changed = true;
- cursor_pos_changed = true;
- candidate_changed = true;
+ changed |= (ChangedStatus.Preedit | ChangedStatus.CursorPos
+ | CandidateAll);
}
internal void insert_candidates (Xex.Term arg)
{
int column = 0;
+ Xex.Variable v = domain.GetVar (Ncandidates_group_size, false);
- if (domain.IsBound (Mcandidates_group_size))
- {
- object val = domain.GetValue (Mcandidates_group_size);
- if (val is int)
- column = (int) val;
- }
+ if (v != null)
+ column = v.Value.Intval;
candidates = new Candidates (arg.Listval, column);
candidate_from = candidate_to = cursor_pos;
update_candidate ();
preedit_replace (pos, cursor_pos, null);
else
preedit_replace (cursor_pos, pos, null);
- preedit_changed = true;
- cursor_pos_changed = true;
+ changed |= ChangedStatus.Preedit | ChangedStatus.CursorPos;
}
internal void show ()
{
candidate_show = true;
- candidate_changed = true;
+ changed |= ChangedStatus.CandidateShow;
}
internal void hide ()
{
candidate_show = false;
- candidate_changed = true;
+ changed |= ChangedStatus.CandidateShow;
}
internal void move (int pos)
if (pos != cursor_pos)
{
cursor_pos = pos;
- preedit_changed = true;
+ changed |= ChangedStatus.Preedit;
}
}
else
{
key_head = - n;
- if (key_head > keys.Count)
- key_head = keys.Count;
+ if (key_head > keys.keyseq.Count)
+ key_head = keys.keyseq.Count;
}
}
{
if (key_head > 0)
key_head--;
- if (key_head < keys.Count)
- keys.RemoveRange (key_head, keys.Count - key_head);
- for (int i = 0; i < keyseq.Count; i++)
- keys.Add (keyseq[i]);
+ if (key_head < keys.keyseq.Count)
+ keys.keyseq.RemoveRange (key_head, keys.keyseq.Count - key_head);
+ for (int i = 0; i < keyseq.keyseq.Count; i++)
+ keys.keyseq.Add (keyseq.keyseq[i]);
}
internal void pop ()
{
- if (key_head < keys.Count)
- keys.RemoveRange (key_head, 1);
+ if (key_head < keys.keyseq.Count)
+ keys.keyseq.RemoveRange (key_head, 1);
}
internal void undo (int n)
{
if (n < 0)
- keys.RemoveRange (keys.Count + n, - n);
+ keys.keyseq.RemoveRange (keys.keyseq.Count + n, - n);
else
- keys.RemoveRange (n, keys.Count - n);
+ keys.keyseq.RemoveRange (n, keys.keyseq.Count - n);
reset ();
}
{
produced.Cat (preedit);
preedit.Del ();
- preedit_changed = true;
+ changed |= ChangedStatus.Preedit;
}
internal void shift (MSymbol sym)
if (state != states.Peek ())
{
states.Push (state);
- state_boundary = domain.SetBoundary ();
+ state_var_values = domain.SaveValues ();
status = state.title;
if (status == null)
status = im.title;
- status_changed = true;
+ changed |= ChangedStatus.StateTitle;
Xex on_entry
= (Xex) state.branches.Get (MSymbol.t);
if (on_entry != null)
}
}
- internal bool call (MSymbol module, MSymbol method, MPlist arglist)
- {
- MInputMethod.Plugin plugin;
-
- if (! im.plugins.TryGetValue (module, out plugin))
- return false;
- if (plugin.assembly == null)
- {
- Assembly assembly;
-
- try {
- assembly = Assembly.LoadFrom (module.Name + ".dll");
- } catch {
- return false;
- }
- Type t = assembly.GetType ("Plugin");
- for (MPlist p = plugin.methods; ! p.IsEmpty; p = p.next)
- p.Set (p.key, t.GetMethod (p.key.Name));
- }
-
- MethodInfo method_info = (MethodInfo) plugin.methods.Get (method);
- if (method_info == null)
- return false;
- object[] method_arg = new object[1];
- method_arg[0] = arglist;
- bool result = (bool) method_info.Invoke (null, method_arg);
- if (! result)
- return false;
- if (! arglist.IsEmpty)
- (new Xex (arglist, domain)).Eval (domain);
- return true;
- }
-
internal void reset ()
{
preedit.Del ();
internal void HandleKey ()
{
- MInputMethod.State state = states.Peek ();
+ }
-
+ public bool Toggle ()
+ {
+ active = ! active;
+ return active;
}
}
}