private static Dictionary<MDatabase.Tag, MInputMethod> im_table
= new Dictionary<MDatabase.Tag, MInputMethod> ();
+ internal static MInputMethod im_global;
+
// Sub classes
private class Exception : System.Exception
{
}
}
- public class KeySeq : Xex.Object
+ public class KeySeq : Xex.TermValue
{
List<Key> keyseq = new List<Key> ();
}
}
- internal class PluginMethod : Function
+ internal class PluginMethod : Xex.Function
{
public Plugin plugin;
public MSymbol method;
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);
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 NamedMaker (MSymbol name, MInputContext ic) : base (name, ic)
- {
- ic.markers.Put (name, this);
- }
+ public Named (MSymbol name) : base (name) { }
- public override int Position {
- get { return position; }
- set { position = value; }
+ public override int Position (MInputContext ic) { return pos; }
+
+ public override void Mark (MInputContext ic) { pos = ic.cursor_pos; }
}
- }
- 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_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';
+ }
}
- set {
+
+ public override void Mark (MInputContext ic)
+ {
throw new Exception ("Can't set predefined marker: " + 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 strline)
+ predefined_markers[s] = new Predefined (s);
+ }
+
+ public static Marker Get (MInputContext ic, MSymbol name)
+ {
+ Marker m;
+
+ if (predefined_markers.TryGetValue (name, out m))
+ return m;
+ 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;
+ }
}
internal class Map
M17n.DebugPrint ("Found {0} input methods\n", list.Count);
foreach (MDatabase mdb in list)
im_table[mdb.tag] = new MInputMethod (mdb.tag);
+ tag = new MDatabase.Tag (Minput_method, MSymbol.t, MSymbol.nil, "global");
+ im_global = im_table[tag];
}
// Constructor
variables = new Xex.Variable[var_names.Length];
int i = 0;
foreach (Xex.Name name in var_names)
- variables[i++] = domain.GetVar (name);
+ variables[i++] = domain.GetVar (name, false);
commands = this.commands;
return true;
}
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);
}
}
private void new_variable (Xex.Name 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,
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,
MPlist pl, Xex.Variable vari)
{
- Xex.Term term = new Xex.Term ((Xex.Name) val.name);
Xex.Name[] range;
+ Xex.Name sym = val.Name;
if (pl.IsEmpty)
range = null;
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 void parse_variables (MPlist plist)
{
- variables = new Variable[plist.Count];
+ var_names = new Xex.Name[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;
+ for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
+ {
+ if (! plist.IsPlist || ! plist.Plist.IsSymbol)
+ throw new Exception ("Invalid variable: " + plist);
+
+ MPlist p = plist.Plist;
+ Xex.Name name = (Xex.Name) p.Symbol.Name;
+ var_names[i] = name;
+ p = p.next;
+ string desc = (string) parse_description (p);
+ Xex.Variable vari = im_global.domain.GetVar (name, false);
+
+ if (vari != null)
+ domain.Defvar (vari);
+ if (desc != null)
p = p.next;
- string desc = parse_description (p);
- Xex.Variable vari = null;
- Xex.Term val;
-
- 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);
- }
- }
+ 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.Name[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.Name name = node_list[i].Attributes[0].Value;
+ Xex.Variable vari = im_global.domain.GetVar (name, false);
+
+ if (vari != null)
+ domain.Defvar (vari);
+ else
+ domain.Defvar (node_list[i]);
+
+ var_names[i] = name;
+ }
}
private void parse_commands (MPlist plist)
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")
+ if (nn.NodeType == XmlNodeType.Element)
domain.Defun ((MSymbol) node.GetAttribute ("id"));
for (XmlNode nn = node.FirstChild; nn != null; nn = nn.NextSibling)
- if (nn.NodeType == XmlNodeType.Element
- && nn.Name != "xi:include")
+ if (nn.NodeType == XmlNodeType.Element)
domain.Defun ((MSymbol) node.GetAttribute ("id"), null,
- nn.FirstChild);
+ nn.FirstChild);
}
private void parse_maps (XmlNode node)
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].Nameval;
+ return Marker.Get (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);
+ return ((MInputContext) domain.context).char_at (args[0].Intval);
}
private static Xex.Term Fdelete (Xex.Domain domain, Xex.Variable vari,
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 Fpushback (Xex.Domain domain, Xex.Variable vari,
- Xex[] args)
+ Xex.Term[] args)
{
MInputContext ic = (MInputContext) domain.context;
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));
private static Name Ndescription = "description";
private static Name Nrange = "range";
- internal abstract class Function
+ public abstract class Function
{
public Name name;
public int min_arg, max_arg;
}
}
- internal class Str : Variable
+ public class Str : Variable
{
public string[] range;
}
}
- internal class Sym : Variable
+ public class Sym : Variable
{
public Name[] range;
return lambda;
}
+ public void Defun (Name name, Name[] args, bool args_evalled)
+ {
+ Function func = new Function.Lambda (this, name, args, args_evalled)
+ }
+
public void Defun (XmlNode node)
{
Name name = node.Attributes[Nfname].Value;
((Function.Lambda) func).SetBody (node, this);
}
- public void Defvar (XmlNode node)
+ public Variable Defvar (XmlNode node)
{
Name name = node.Attributes[0].Value;
String desc;
vari = new Variable (name, Zero);
}
variables[name] = vari;
+ return vari;
}
- public void Defvar (Variable vari)
+ public Variable Defvar (Variable vari)
{
variables[vari.name] = vari;
+ return vari;
}
internal Variable Defvar (Name name)
domain.functions[kv.Key] = kv.Value;
}
- public Variable GetVar (Name name)
+ public Variable GetVar (Name name, bool create)
{
Variable vari;
if (! variables.TryGetValue (name, out vari))
- variables[name] = vari = new Variable (name, Zero);
+ {
+ if (! create)
+ return null;
+ variables[name] = vari = new Variable (name, Zero);
+ }
return vari;
}
if (name == Nfuncall)
name = node.Attributes[0].Value;
else if (node.Attributes[Nvname] != null)
- vari = domain.GetVar (node.Attributes[Nvname].Value);
+ vari = domain.GetVar (node.Attributes[Nvname].Value, true);
func = domain.GetFunc (name);
XmlNodeList nlist = node.ChildNodes;
int nargs = nlist.Count;
args[i] = new Term (nlist[i], domain);
}
+ public Funcall (Domain domain, Name fname, Term[] args)
+ {
+ func = domain.GetFunc (fname);
+ this.args = args;
+ }
+
public Funcall (Domain domain, Name fname, Name vname, Term[] args)
{
func = domain.GetFunc (fname);
- vari = domain.GetVar (vname);
+ vari = domain.GetVar (vname, true);
this.args = args;
}
else if (name == Nstring)
objval = node.InnerText.Clone ();
else if (name == Nvarref)
- objval = domain.GetVar ((Name) node.Attributes[0].Value);
+ objval = domain.GetVar ((Name) node.Attributes[0].Value, true);
else if (name == Nlist)
{
List<Term> list = new List<Term> ();