1 // -* coding: utf-8; -*
5 Log: function (arg, indent)
10 Xex.LogNode.value = '';
14 if (indent != undefined)
15 for (var i = 0; i <= indent; i++)
17 Xex.LogNode.value = str + arg + "\n" + Xex.LogNode.value;
23 UnknownError: "unknown-error",
24 WrongArgument: "wrong-argument",
26 InvalidInteger: "invalid-integer",
27 TermTypeInvalid: "term-type-invalid",
28 FunctionConflict: "function-conflict",
29 VariableTypeConflict: "variable-type-conflict",
30 VariableRangeConflict: "variable-range-conflict",
31 VariableWrongRange: "variable-wrong-range",
32 VariableWrongValue: "variable-wrong-value",
34 UnknownFunction: "unknown-function",
35 MacroExpansionError: "macro-expansion-error",
36 NoVariableName: "no-variable-name",
37 NoFunctionName: "no-funcion-name",
40 ArithmeticError: "arithmetic-error",
41 WrongType: "wrong-type",
42 IndexOutOfRange: "index-out-of-range",
43 ValueOutOfRange: "value-out-of-range",
44 NoLoopToBreak: "no-loop-to-break",
45 UncaughtThrow: "uncaught-throw"
48 Xex.Variable = function (domain, name, desc, val, range)
57 Xex.Variable.prototype.clone = function ()
59 return new Xex.Variable (this.domain, this.name, this.desc,
60 this.val, this.range);
63 Xex.Variable.prototype.Equals = function (obj)
65 return ((obj instanceof Xex.Variable)
66 && obj.name == this.name);
69 Xex.Variable.prototype.SetValue = function (term)
75 Xex.Function = function (name, with_var, min_args, max_args)
78 this.with_var = with_var;
79 this.min_args = min_args;
80 this.max_args = max_args;
83 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
86 this.with_var = with_var;
87 this.min_args = min_args;
88 this.max_args = max_args;
89 this.builtin = builtin;
92 Xex.Subrountine.prototype.Call = function (domain, vari, args)
94 var newargs = new Array ();
95 for (var i = 0; i < args.length; i++)
97 newargs[i] = args[i].Eval (domain);
101 return this.builtin (domain, vari, newargs)
104 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
107 this.with_var = with_var;
108 this.min_args = min_args;
109 this.max_args = max_args;
110 this.builtin = builtin;
113 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
115 return this.builtin (domain, vari, args)
118 Xex.Lambda = function (name, min_args, max_args, args, body)
121 this.min_args = min_args;
122 this.max_args = max_args;
127 Xex.Lambda.prototype.Call = function (domain, vari, args)
129 var current = domain.bindings;
130 var result = Xex.Zero;
131 var limit = max_args >= 0 ? args.length : args.length - 1;
135 for (i = 0; i < limit; i++)
137 result = args[i].Eval (domain);
138 if (domain.Thrown ())
140 domain.Bind (this.args[i], result);
144 var list = new Array ();
145 for (i = 0; i < args[limit].length; i++)
147 result = args[limit].Eval (domain);
148 if (domain.Thrown ())
152 domain.Bind (this.args[limit], list);
155 domain.Catch (Xex.CatchTag.Return);
156 for (var term in this.body)
158 result = term.Eval (domain);
159 if (domain.Thrown ())
166 domain.UnboundTo (current);
171 Xex.Macro = function (name, min_args, max_args, args, body)
174 this.min_args = min_args;
175 this.max_args = max_args;
180 Xex.Macro.prototype.Call = function (domain, vari, args)
182 var current = domain.bindings;
183 var result = Xex.Zero;
187 for (i = 0; i < args.length; i++)
188 domain.Bind (this.args[i], args[i]);
190 domain.Catch (Xex.CatchTag.Return);
191 for (var i in this.body)
193 result = this.body[i].Eval (domain);
194 if (domain.Thrown ())
201 domain.UnboundTo (current);
206 Xex.Bindings = function (vari)
209 this.old_value = vari.val;
212 Xex.Bindings.prototype.UnboundTo = function (boundary)
214 for (var b = this; b != boundary; b = b.next)
215 b.vari.val = b.old_value;
219 Xex.Bind = function (bindings, vari, val)
221 var b = new Xex.Bindings (vari);
232 Xex.Domain = function (name, parent, context)
235 this.context = context;
238 if (name != 'basic' && ! parent)
239 parent = Xex.BasicDomain
240 this.parent = parent;
247 for (elt in parent.termtypes)
248 this.termtypes[elt] = parent.termtypes[elt];
249 for (elt in parent.functions)
250 this.functions[elt] = parent.functions[elt];
251 for (elt in parent.variables)
252 this.variables[elt] = parent.variables[elt];
255 this.call_stack = new Array ();
256 this.bindings = null;
257 this.catch_stack = new Array ();
258 this.catch_count = 0;
262 Xex.Domain.prototype = {
263 CallStackCount: function () { return this.call_stack.length; },
264 CallStackPush: function (term) { this.call_stack.push (term); },
265 CallStackPop: function () { this.call_stack.pop (); },
266 Bind: function (vari, val)
268 this.bindings = Xex.Bind (this.bindings, vari, val);
270 UnboundTo: function (boundary)
273 this.bindings = this.bindings.UnboundTo (boundary);
275 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
278 this.catch_stack.pop ();
279 if (this.catch_count > this.catch_stack.length)
284 if (this.catch_count < this.catch_stack.length)
286 this.caught = (this.catch_count == this.catch_stack.length - 1);
292 ThrowReturn: function ()
294 for (var i = this.catch_stack.length - 1; i >= 0; i--)
297 if (this.catch_stack[i] == Xex.CatchTag.Return)
301 ThrowBreak: function ()
303 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
304 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
305 "No surrounding loop to break");
308 ThrowSymbol: function (tag)
310 var i = this.catch_count;
311 for (var j = this.catch_stack.length - 1; j >= 0; j--)
314 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
316 this.catch_count = i;
320 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
321 "No corresponding catch: " + tag);
323 DefType: function (obj)
326 if (this.termtypes[type])
327 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
328 "Already defined: " + type);
329 if (this.functions[type])
330 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
331 "Already defined as a funciton or a macro: "
333 this.termtypes[type] = obj.Parser;
335 DefSubr: function (builtin, name, with_var, min_args, max_args)
337 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
340 DefSpecial: function (builtin, name, with_var, min_args, max_args)
342 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
345 Defun: function (name, min_args, max_args, args, body)
347 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
350 DefunByFunc: function (func) { this.functions[func.name] = func; },
351 Defmacro: function (name, min_args, max_args, args, body)
353 this.functions[name] = new Xex.Macro (name, min_args, max_args,
356 DefAlias: function (alias, fname)
358 var func = this.functions[fname];
361 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
362 if (this.termtypes[alias])
363 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
364 "Already defined as a term type: " + alias);
365 if (this.functions[alias])
366 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
367 "Already defined as a function: " + alias);
368 this.functions[alias] = func;
370 Defvar: function (name, desc, val, range)
372 var vari = new Xex.Variable (this, name, desc, val, range);
373 this.variables[name] = vari;
376 GetFunc: function (name)
378 var func = this.functions[name];
380 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
381 "Unknown function: " + name);
384 CopyFunc: function (domain, name)
386 var func = this.functions[name];
387 domain.DefunByFunc (func);
390 CopyFuncAll: function (domain)
392 for (var elt in this.functions)
393 domain.DefunByFunc (this.functions[elt]);
395 GetVarCreate: function (name)
397 var vari = this.variables[name];
399 vari = this.variables[name] = new Xex.Variable (this, name, null,
403 GetVar: function (name) { return this.variables[name]; },
404 SaveValues: function ()
407 for (var elt in this.variables)
408 values[elt] = this.variables[elt].val.Clone ();
411 RestoreValues: function (values)
416 var vari = this.variables[name];
417 vari.val = values[name];
422 Xex.Term = function (type) { this.type = type; }
423 Xex.Term.prototype = {
424 IsTrue: function () { return true; },
425 Eval: function (domain) { return this.Clone (); },
426 Clone: function (domain) { return this; },
427 Equals: function (obj)
429 return (this.type == obj.type
431 && obj.val == this.val);
433 Matches: function (obj) { return this.Equals (obj); },
434 toString: function ()
436 if (this.val != undefined)
437 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
438 return '<' + this.type + '/>';
440 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
441 "Not an integer"); },
442 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
446 Node.prototype.firstElement = function ()
448 for (var n = this.firstChild; n; n = n.nextSibling)
454 Node.prototype.nextElement = function ()
456 for (var n = this.nextSibling; n; n = n.nextSibling)
463 function parse_defvar (domain, node)
465 var name = node.attributes['vname'].nodeValue;
467 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
468 var vari = domain.variables[name];
469 var desc, val, range;
472 desc = vari.description;
476 node = node.firstElement ();
477 if (node && node.nodeName == 'description')
479 desc = node.firstChild.nodeValue;
480 node = node.nextElement ();
484 val = Xex.Term.Parse (domain, node);
485 node = node.nextElement ();
486 if (node && node.nodeName == 'possible-values')
487 for (node = node.firstElement (); node; node = node.nextElement ())
490 if (node.nodeName == 'range')
493 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
494 'Range not allowed for ' + name);
496 for (var n = node.firstElement (); n; n = n.nextElement ())
498 var v = Xex.Term.Parse (domain, n);
500 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
501 'Invalid range value: ' + val);
507 pval = Xex.Term.Parse (domain, node);
508 if (val.type != pval.type)
509 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
510 'Invalid possible value: ' + pval);
513 range = new Array ();
519 domain.Defvar (name, desc, val, range);
523 function parse_defun_head (domain, node)
525 var name = node.attributes['fname'].nodeValue;
527 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
528 var args = new Array ();
529 var nfixed = 0, noptional = 0, nrest = 0;
531 node = node.firstElement ();
532 if (node && node.nodeName == 'args')
535 for (n = n.firstElement (); n; n = n.nextElement ())
537 if (n.nodeName == 'fixed')
539 else if (n.nodeName == 'optional')
541 else if (n.nodeName == 'rest')
544 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
547 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
548 for (n = node.firstElement (); n; n = n.nextElement ())
549 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
551 args.min_args = nfixed;
552 args.max_args = nrest == 0 ? nfixed + noptional : -1;
554 if (node.nodeName == 'defun')
555 domain.Defun (name, args, null);
557 domain.Defmacro (name, args, null);
561 function parse_defun_body (domain, node)
563 var name = node.attributes['fname'].nodeValue;
564 var func = domain.GetFunc (name);
566 for (node = node.firstElement (); node; node = node.nextElement ())
567 if (node.nodeName != 'description' && node.nodeName != 'args')
569 body = Xex.Term.Parse (domain, node, null);
573 Xex.Term.Parse = function (domain, node, stop)
575 if (arguments.length == 2)
577 var name = node.nodeName;
578 var parser = domain.termtypes[name];
581 return parser (domain, node);
582 if (name == 'defun' || name == 'defmacro')
584 name = parse_defun_head (domain, node);
585 parse_defun_body (domain, node);
586 return new Xex.StrTerm (name);
588 if (name == 'defvar')
590 name = parse_defvar (domain, node);
591 return new Xex.StrTerm (name);
593 return new Xex.Funcall.prototype.Parser (domain, node);
595 for (var n = node; n && n != stop; n = n.nextElement ())
596 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
597 parse_defun_head (domain, n);
599 for (var n = node; n && n != stop; n = n.nextElement ())
601 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
602 parse_defun_body (domain, n);
603 else if (n.nodeName == 'defvar')
604 parse_defvar (domain, n);
608 terms = new Array ();
609 terms.push (Xex.Term.Parse (domain, n));
616 Xex.Varref = function (vname)
622 var proto = new Xex.Term ('varref');
624 proto.Clone = function () { return new Xex.Varref (this.val); }
625 proto.Eval = function (domain)
627 if (! this.vari || this.vari.domain != domain)
628 this.vari = domain.GetVarCreate (this.val);
629 Xex.Log (this.ToString () + '=>' + this.vari.val, domain.depth);
630 return this.vari.val;
633 proto.Parser = function (domain, node)
635 return new Xex.Varref (node.attributes['vname'].nodeValue);
638 proto.ToString = function ()
640 return '<varref vname="' + this.val + '"/>';
643 Xex.Varref.prototype = proto;
646 var null_args = new Array ();
648 Xex.Funcall = function (func, vari, args)
652 this.args = args || null_args;
656 var proto = new Xex.Term ('funcall');
658 proto.Parser = function (domain, node)
660 var fname = node.nodeName;
663 if (fname == 'funcall')
664 fname = node.attributes['fname'].nodeValue;
665 var func = domain.GetFunc (fname);
667 attr = node.attributes['vname'];
668 vari = attr != undefined ? domain.GetVarCreate (attr.nodeValue) : false;
669 var args = Xex.Term.Parse (domain, node.firstElement (), null);
670 return new Xex.Funcall (func, vari, args);
673 proto.New = function (domain, fname, vname, args)
675 var func = domain.GetFunc (fname);
676 var vari = vname ? domain.GetVarCreate (vname) : null;
677 var funcall = new Xex.Funcall (func, vari, args);
678 if (func instanceof Xex.Macro)
679 funcall = funcall.Eval (domain);
683 proto.Eval = function (domain)
685 if (! (this.func instanceof Xex.Subrountine))
686 Xex.Log (this, domain.depth);
690 result = this.func.Call (domain, this.vari, this.args);
692 Xex.Log (this + ' => ' + result, --domain.depth);
697 proto.Clone = function ()
699 return new Xex.Funcall (this.func, this.vari, this.args);
702 proto.Equals = function (obj)
704 return (obj.type == 'funcall'
705 && obj.func == this.func
706 && obj.vari.Equals (this.vari)
707 && obj.args.length == this.func.length);
710 proto.toString = function ()
713 var len = this.args.length;
714 var str = '<' + this.func.name;
716 str += ' vname="' + this.vari.name + '"';
719 if (this.func instanceof Xex.Subrountine)
720 for (var i = 0; i < len; i++)
721 arglist += this.args[i].toString ();
723 for (var i = 0; i < len; i++)
725 return str + '>' + arglist + '</' + this.func.name + '>';
728 Xex.Funcall.prototype = proto;
731 Xex.ErrTerm = function (ename, message, stack)
734 this.message = message;
739 var proto = new Xex.Term ('error');
741 proto.IsError = true;
743 proto.Parser = function (domain, node)
745 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
746 node.innerText, false);
749 proto.CallStack = function () { return stack; }
751 proto.SetCallStack = function (value) { statck = value; }
753 proto.Clone = function ()
755 return new Xex.ErrTerm (ename, message, false);
758 proto.Equals = function (obj)
761 && obj.ename == ename && obj.message == message
762 && (obj.stack ? (stack && stack.length == obj.stack.length)
766 proto.Matches = function (obj)
768 return (obj.IsError && obj.ename == ename);
771 proto.toString = function ()
773 return '<error ename="' + this.ename + '">' + this.message + '</error>';
776 Xex.ErrTerm.prototype = proto;
779 Xex.IntTerm = function (num) { this.val = num; };
781 var proto = new Xex.Term ('integer');
783 proto.Intval = function () { return this.val; };
784 proto.IsTrue = function () { return this.val != 0; }
785 proto.Clone = function () { return new Xex.IntTerm (this.val); }
786 proto.Parser = function (domain, node)
788 var str = node.firstChild.nodeValue;
790 if (str.charAt (0) == '?' && str.length == 2)
791 return new Xex.IntTerm (str.charCodeAt (1));
792 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
794 Xex.IntTerm.prototype = proto;
797 Xex.StrTerm = function (str) { this.val = str; };
799 var proto = new Xex.Term ('string');
801 proto.Strval = function () { return this.val; };
802 proto.IsTrue = function () { return this.val.length > 0; }
803 proto.Clone = function () { return new Xex.StrTerm (this.val); }
804 proto.Parser = function (domain, node)
806 return new Xex.StrTerm (node.firstChild.nodeValue);
808 Xex.StrTerm.prototype = proto;
811 Xex.SymTerm = function (str) { this.val = str; };
813 var proto = new Xex.Term ('symbol');
814 proto.IsSymbol = true;
815 proto.IsTrue = function () { return this.val != 'nil'; }
816 proto.Clone = function () { return new Xex.SymTerm (this.val); }
817 proto.Parser = function (domain, node)
819 return new Xex.SymTerm (node.firstChild.nodeValue);
821 Xex.SymTerm.prototype = proto;
824 Xex.LstTerm = function (list) { this.val = list; };
826 var proto = new Xex.Term ('list');
828 proto.IsTrue = function () { return this.val.length > 0; }
829 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
831 proto.Equals = function (obj)
833 if (obj.type != 'list' || obj.val.length != this.val.length)
835 var i, len = this.val.length;
836 for (i = 0; i < len; i++)
837 if (! this.val[i].Equals (obj.val[i]))
842 proto.Parser = function (domain, node)
844 var list = Xex.Term.Parse (domain, node.firstElement (), null);
845 return new Xex.LstTerm (list);
848 proto.toString = function ()
850 var len = this.val.length;
855 for (var i = 0; i < len; i++)
856 str += this.val[i].toString ();
857 return str + '</list>';
859 Xex.LstTerm.prototype = proto;
863 var basic = new Xex.Domain ('basic', null, null);
865 function Fset (domain, vari, args)
868 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
869 'No variable name to set');
870 vari.SetValue (args[0]);
874 function maybe_set_intvar (vari, n)
876 var term = new Xex.IntTerm (n);
878 vari.SetValue (term);
882 function Fadd (domain, vari, args)
884 var n = vari ? vari.val.Intval () : 0;
885 var len = args.length;
887 for (var i = 0; i < len; i++)
888 n += args[i].Intval ();
889 return maybe_set_intvar (vari, n);
892 function Fmul (domain, vari, args)
894 var n = vari ? vari.val.Intval () : 1;
895 for (var i = 0; i < args.length; i++)
897 return maybe_set_intvar (vari, n);
900 function Fsub (domain, vari, args)
906 n = args[0].Intval ();
911 n = vari.val.Intval ();
914 while (i < args.length)
915 n -= args[i++].Intval ();
916 return maybe_set_intvar (vari, n);
919 function Fdiv (domain, vari, args)
925 n = args[0].Intval ();
930 n = vari.val.Intval ();
933 while (i < args.length)
934 n /= args[i++].Intval ();
935 return maybe_set_intvar (vari, n);
938 function Fmod (domain, vari, args)
940 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
943 function Flogior (domain, vari, args)
945 var n = vari == null ? 0 : vari.val;
946 for (var i = 0; i < args.length; i++)
948 return maybe_set_intvar (vari, n);
951 function Fand (domain, vari, args)
953 var len = args.length;
954 for (var i = 0; i < len; i++)
956 var result = args[i].Eval (domain);
957 if (domain.Thrown ())
959 if (! result.IsTrue ())
965 function For (domain, vari, args)
967 var len = args.length;
968 for (var i = 0; i < len; i++)
970 var result = args[i].Eval (domain);
971 if (domain.Thrown ())
973 if (result.IsTrue ())
979 function Feq (domain, vari, args)
981 for (var i = 1; i < args.length; i++)
982 if (! args[i - 1].Equals (args[i]))
987 function Flt (domain, vari, args)
989 var n = args[0].Intval;
991 for (var i = 1; i < args.length; i++)
993 var n1 = args[i].Intval;
1001 function Fle (domain, vari, args)
1003 var n = args[0].Intval;
1004 for (var i = 1; i < args.length; i++)
1006 var n1 = args[i].Intval;
1014 function Fgt (domain, vari, args)
1016 var n = args[0].Intval;
1017 for (var i = 1; i < args.length; i++)
1019 var n1 = args[i].Intval;
1027 function Fge (domain, vari, args)
1029 var n = args[0].Intval;
1030 for (var i = 1; i < args.Length; i++)
1032 var n1 = args[i].Intval;
1040 function Fprogn (domain, vari, args)
1042 var result = Xex.One;
1043 var len = args.length;
1045 for (var i = 0; i < len; i++)
1047 result = args[i].Eval (domain);
1048 if (domain.Thrown ())
1054 function Fif (domain, vari, args)
1056 var result = args[0].Eval (domain);
1058 if (domain.Thrown ())
1060 if (result.IsTrue ())
1061 return args[1].Eval (domain);
1062 if (args.length == 2)
1064 return args[2].Eval (domain);
1067 function Fcond (domain, vari, args)
1069 for (var i = 0; i < args.length; i++)
1072 var result = list.val[0].Eval (domain);
1073 if (result.IsTrue ())
1075 for (var j = 1; j < list.val.length; j++)
1078 result = list.val[j].Eval (domain);
1080 if (domain.Thrown ())
1089 function eval_terms (domain, terms, idx)
1091 var result = Xex.Zero;
1092 domain.caught = false;
1093 for (var i = idx; i < terms.length; i++)
1095 result = terms[i].Eval (domain);
1096 if (domain.Thrown ())
1102 function Fcatch (domain, vari, args)
1107 if (args[0].IsError)
1110 result = eval_terms (domain, args, 1);
1112 if (e instanceof Xex.ErrTerm)
1114 if (! args[0].Matches (e))
1122 else if (args[0].IsSymbol)
1125 domain.Catch (args[0].val);
1126 result = eval_terms (domain, args, 1);
1130 vari.SetValue (result);
1138 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1139 "Not a symbol nor an error: " + args[0]);
1142 function Fthrow (domain, vari, args)
1144 if (args[0].IsSymbl)
1146 domain.ThrowSymbol (args[0]);
1147 return (args[args.length - 1]);
1149 if (args[0].IsError)
1153 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1154 "Not a symbol nor an error:" + args[0]);
1157 Xex.BasicDomain = basic;
1159 basic.DefSubr (Fset, "set", true, 1, 1);
1160 basic.DefAlias ("=", "set");
1161 //basic.DefSubr (Fnot, "not", false, 1, 1);
1162 //basic.DefAlias ("!", "not");
1163 basic.DefSubr (Fadd, "add", true, 1, -1);
1164 basic.DefSubr (Fmul, "mul", true, 1, -1);
1165 basic.DefAlias ("*", "mul");
1166 basic.DefSubr (Fsub, "sub", true, 1, -1);
1167 basic.DefAlias ("-", "sub");
1168 basic.DefSubr (Fdiv, "div", true, 1, -1);
1169 basic.DefAlias ("/", "div");
1170 basic.DefSubr (Fmod, "mod", true, 1, 2);
1171 basic.DefAlias ("%", "mod");
1172 basic.DefSubr (Flogior, "logior", true, 1, -1);
1173 basic.DefAlias ('|', "logior");
1174 //basic.DefSubr (Flogand, "logand", true, 1, -1);
1175 //basic.DefAlias ("&", "logand");
1176 //basic.DefSubr (Flsh, "lsh", true, 1, 2);
1177 //basic.DefAlias ("<<", "lsh");
1178 //basic.DefSubr (Frsh, "rsh", true, 1, 2);
1179 //basic.DefAlias (">>", "rsh");
1180 basic.DefSubr (Feq, "eq", false, 2, -1);
1181 basic.DefAlias ("==", "eq");
1182 //basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1183 //basic.DefAlias ("!=", "noteq");
1184 basic.DefSubr (Flt, "lt", false, 2, -1);
1185 basic.DefAlias ("<", "lt");
1186 basic.DefSubr (Fle, "le", false, 2, -1);
1187 basic.DefAlias ("<=", "le");
1188 basic.DefSubr (Fgt, "gt", false, 2, -1);
1189 basic.DefAlias (">", "gt");
1190 basic.DefSubr (Fge, "ge", false, 2, -1);
1191 basic.DefAlias (">=", "ge");
1192 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1194 //basic.DefSubr (Fappend, "append", true, 0, -1);
1195 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1196 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1197 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1198 //basic.DefSubr (Fins, "ins", true, 2, 2);
1199 //basic.DefSubr (Fdel, "del", true, 2, 2);
1200 //basic.DefSubr (Feval, "eval", false, 1, 1);
1201 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1202 //basic.DefSubr (Freturn, "return", false, 0, 1);
1203 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1205 basic.DefSpecial (Fand, "and", false, 1, -1);
1206 basic.DefAlias ("&&", "and");
1207 basic.DefSpecial (For, "or", false, 1, -1);
1208 basic.DefAlias ("||", "or");
1209 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1210 basic.DefAlias ("expr", "progn");
1211 basic.DefSpecial (Fif, "if", false, 2, 3);
1212 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1213 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1214 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1215 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1216 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1217 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1218 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1219 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1221 basic.DefType (Xex.Funcall.prototype);
1222 basic.DefType (Xex.Varref.prototype);
1223 basic.DefType (Xex.ErrTerm.prototype);
1224 basic.DefType (Xex.IntTerm.prototype);
1225 basic.DefType (Xex.StrTerm.prototype);
1226 basic.DefType (Xex.SymTerm.prototype);
1227 basic.DefType (Xex.LstTerm.prototype);
1231 Xex.Zero = new Xex.IntTerm (0);
1232 Xex.One = new Xex.IntTerm (1);
1233 Xex.nil = new Xex.SymTerm ('nil');
1235 Xex.Load = function (server, file)
1237 var obj = new XMLHttpRequest ();
1238 var url = server ? server + '/' + file : file;
1239 obj.open ('GET', url, false);
1240 obj.overrideMimeType ('text/xml');
1242 return obj.responseXML.firstChild;
1246 // URL of the input method server.
1247 server: "http://www.m17n.org/common/mim-js",
1248 // Boolean flag to tell if MIM is active or not.
1250 // Boolean flag to tell if MIM is running in debug mode or not.
1252 // List of main input methods.
1254 // List of extra input methods;
1256 // Global input method data
1258 // Currently selected input method.
1262 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1269 CandidateIndex:0x10,
1271 Preedit: 0x06, // PreeditText | CursorPos
1272 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1294 ParseError: "parse-error"
1299 var keysyms = new Array ();
1300 keysyms["bs"] = "backspace";
1301 keysyms["lf"] = "linefeed";
1302 keysyms["cr"] = keysyms["enter"] = "return";
1303 keysyms["esc"] = "escape";
1304 keysyms["spc"] = "space";
1305 keysyms["del"] = "delete";
1307 function decode_keysym (str) {
1308 if (str.length == 1)
1310 var parts = str.split ("-");
1311 var len = parts.length, i;
1312 var has_modifier = len > 1;
1314 for (i = 0; i < len - 1; i++)
1315 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1317 var key = parts[len - 1];
1320 key = keysyms[key.toLowerCase ()];
1326 for (i = 1; i < len - 1; i++)
1327 str += '-' + parts[i];
1336 parts = new Array ();
1343 MIM.Key = function (val)
1346 this.has_modifier = false;
1347 if (typeof val == 'string' || val instanceof String)
1349 this.key = decode_keysym (val);
1351 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1352 if (this.key instanceof Array)
1354 this.key = this.key[0];
1355 this.has_modifier = true;
1358 else if (typeof val == 'number' || val instanceof Number)
1359 this.key = String.fromCharCode (val);
1361 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1364 MIM.Key.prototype.toString = function () { return this.key; };
1368 MIM.KeySeq = function (seq)
1370 this.val = new Array ();
1371 this.has_modifier = false;
1377 var len = seq.val.length;
1378 for (var i = 0; i < len; i++)
1381 if (v.type != 'string' && v.type != 'integer'
1382 && v.type != 'symbol')
1383 throw new Xex.ErrTerm (MIM.Error.ParseError,
1384 "Invalid key: " + v);
1385 var key = new MIM.Key (v.val);
1386 this.val.push (key);
1387 if (key.has_modifier)
1388 this.has_modifier = true;
1393 var len = seq.val.length;
1394 for (var i = 0; i < len; i++)
1395 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1398 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1402 var proto = new Xex.Term ('keyseq');
1403 proto.Clone = function () { return this; }
1404 proto.Parser = function (domain, node)
1406 var seq = new Array ();
1407 for (node = node.firstChild; node; node = node.nextSibling)
1408 if (node.nodeType == 1)
1410 var term = Xex.Term.Parse (domain, node);
1411 return new MIM.KeySeq (term);
1413 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1415 proto.toString = function ()
1417 var len = this.val.length;
1421 var str = '<keyseq>';
1422 for (var i = 0; i < len; i++)
1426 else if (this.has_modifier)
1428 str += this.val[i].toString ();
1430 return str + '</keyseq>';
1433 MIM.KeySeq.prototype = proto;
1437 MIM.Marker = function () { }
1438 MIM.Marker.prototype = new Xex.Term ('marker');
1439 MIM.Marker.prototype.CharAt = function (ic)
1441 var p = this.Position (ic);
1442 if (p < 0 || p >= ic.preedit.length)
1444 return ic.preedit.charCodeAt (p);
1447 MIM.FloatingMarker = function (name) { this.val = name; };
1448 var proto = new MIM.Marker ();
1449 MIM.FloatingMarker.prototype = proto;
1450 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1451 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1453 MIM.PredefinedMarker = function (name) { this.val = name; }
1454 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1455 MIM.PredefinedMarker.prototype.Position = function (ic)
1457 if (typeof this.pos == 'number')
1459 return this.pos (ic);
1462 var predefined = { }
1464 function def_predefined (name, position)
1466 predefined[name] = new MIM.PredefinedMarker (name);
1467 predefined[name].pos = position;
1470 def_predefined ('@<', 0);
1471 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1472 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1473 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1474 def_predefined ('@[', function (ic) {
1475 if (ic.cursor_pos > 0)
1477 var pos = ic.cursor_pos;
1478 return ic.preedit.FindProp ('candidates', pos - 1).from;
1482 def_predefined ('@]', function (ic) {
1483 if (ic.cursor_pos < ic.preedit.length - 1)
1485 var pos = ic.cursor_pos;
1486 return ic.preedit.FindProp ('candidates', pos).to;
1488 return ic.preedit.length;
1490 for (var i = 0; i < 10; i++)
1491 def_predefined ("@" + i, i);
1492 predefined['@first'] = predefined['@<'];
1493 predefined['@last'] = predefined['@>'];
1494 predefined['@previous'] = predefined['@-'];
1495 predefined['@next'] = predefined['@+'];
1496 predefined['@previous-candidate-change'] = predefined['@['];
1497 predefined['@next-candidate-change'] = predefined['@]'];
1499 MIM.SurroundMarker = function (name)
1502 this.distance = parseInt (name.slice (1));
1503 if (isNaN (this.distance))
1504 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1506 MIM.SurroundMarker.prototype = new MIM.Marker ();
1507 MIM.SurroundMarker.prototype.Position = function (ic)
1509 return ic.cursor_pos + this.distance;
1511 MIM.SurroundMarker.prototype.CharAt = function (ic)
1513 if (this.val == '@-0')
1515 var p = this.Position (ic);
1517 return ic.GetSurroundingChar (p);
1518 else if (p >= ic.preedit.length)
1519 return ic.GetSurroundingChar (p - ic.preedit.length);
1520 return ic.preedit.charCodeAt (p);
1523 MIM.Marker.prototype.Parser = function (domain, node)
1525 var name = node.firstChild.nodeValue;
1526 if (name.charAt (0) == '@')
1528 var n = predefined[name];
1531 if (name.charAt (1) == '-')
1532 return new MIM.SurroundMarker (name);
1533 throw new Xex.ErrTerm (MIM.Error.ParseError,
1534 "Invalid marker: " + name);
1536 return new MIM.FloatingMarker (name);;
1540 MIM.Selector = function (name)
1544 MIM.Selector.prototype = new Xex.Term ('selector');
1548 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1549 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1550 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1551 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1552 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1553 selectors["@["] = selectors["@previous-candidate-change"]
1554 = new MIM.Selector ('@[');
1555 selectors["@]"] = selectors["@next-candidate-change"]
1556 = new MIM.Selector ('@]');
1558 MIM.Selector.prototype.Parser = function (domain, node)
1560 var name = node.firstChild.nodeValue;
1561 var s = selectors[name];
1563 throw new Xex.ErrTerm (MIM.Error.ParseError,
1564 "Invalid selector: " + name);
1569 MIM.Rule = function (keyseq, actions)
1571 this.keyseq = keyseq;
1572 this.actions = actions;
1574 MIM.Rule.prototype = new Xex.Term ('rule');
1575 MIM.Rule.prototype.Parser = function (domain, node)
1578 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1580 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1581 var keyseq = Xex.Term.Parse (domain, n);
1582 if (keyseq.type != 'keyseq')
1583 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1584 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1585 return new MIM.Rule (keyseq, actions);
1587 MIM.Rule.prototype.toString = function ()
1592 MIM.Map = function (name)
1595 this.rules = new Array ();
1599 var proto = new Xex.Term ('map');
1601 proto.Parser = function (domain, node)
1603 var name = node.attributes['mname'].nodeValue;
1605 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1606 var map = new MIM.Map (name);
1607 for (var n = node.firstChild; n; n = n.nextSibling)
1608 if (n.nodeType == 1)
1609 map.rules.push (Xex.Term.Parse (domain, n));
1613 proto.toString = function ()
1615 var str = '<map mname="' + this.name + '">';
1616 var len = this.rules.length;
1617 for (i = 0; i < len; i++)
1618 str += this.rules[i];
1619 return str + '</map>';
1622 MIM.Map.prototype = proto;
1625 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1627 MIM.Action = function (domain, terms)
1629 var args = new Array ();
1630 args.push (Xex.CatchTag_.mimtag);
1631 for (var i = 0; i < terms.length; i++)
1632 args.push (terms[i]);
1633 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1636 MIM.Action.prototype.Run = function (domain)
1638 var result = this.action.Eval (domain);
1639 if (result.type == 'error')
1641 domain.context.Error = result.toString ();
1644 return (result != Xex.CatchTag._mimtag);
1647 MIM.Keymap = function ()
1650 this.submaps = null;
1656 function add_rule (keymap, rule, branch_actions)
1658 var keyseq = rule.keyseq;
1659 var len = keyseq.val.length;
1662 for (var i = 0; i < len; i++)
1664 var key = keyseq.val[i];
1668 if (! keymap.submaps)
1669 keymap.submaps = {};
1671 sub = keymap.submaps[key.key];
1673 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1677 keymap.map_actions = rule.actions;
1678 keymap.branch_actions = branch_actions;
1681 proto.Add = function (map, branch_actions)
1683 var rules = map.rules;
1684 var len = rules.length;
1686 for (var i = 0; i < len; i++)
1687 add_rule (this, rules[i], branch_actions);
1689 proto.Lookup = function (keys, index)
1693 if (index < keys.val.length && this.submaps
1694 && (sub = this.submaps[keys.val[index].key]))
1697 return sub.Lookup (keys, index);
1699 return { map: this, index: index };
1702 MIM.Keymap.prototype = proto;
1705 MIM.State = function (name)
1708 this.keymap = new MIM.Keymap ();
1712 var proto = new Xex.Term ('state');
1714 proto.Parser = function (domain, node)
1716 var map_list = domain.map_list;
1717 var name = node.attributes['sname'].nodeValue;
1719 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1720 var state = new MIM.State (name);
1721 for (node = node.firstElement (); node; node = node.nextElement ())
1723 if (node.nodeName == 'title')
1724 state.title = node.firstChild.nodeValue;
1727 var n = node.firstElement ();
1728 if (node.nodeName == 'branch')
1729 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1730 Xex.Term.Parse (domain, n, null));
1731 else if (node.nodeName == 'state-hook')
1732 state.enter_actions = Xex.Term.Parse (domain, n, null);
1733 else if (node.nodeName == 'catch-all-branch')
1734 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1740 proto.toString = function ()
1742 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1745 MIM.State.prototype = proto;
1749 function Block (index, term)
1753 this.Data = term.val;
1754 else if (term.IsList)
1756 this.Data = new Array ();
1757 for (var i = 0; i < term.val.length; i++)
1758 this.Data.push (term.val[i].val);
1762 Block.prototype.Count = function () { return this.Data.length; }
1763 Block.prototype.get = function (i)
1765 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1768 MIM.Candidates = function (candidates, column)
1770 this.column = column;
1774 this.blocks = new Array ();
1776 for (var i = 0; i < candidates.length; i++)
1778 var block = new Block (this.total, candidates[i]);
1779 this.blocks.push (block);
1780 this.total += block.Count ();
1786 return (this.column > 0 ? this.index % this.column
1787 : this.index - this.blocks[this.row].Index);
1790 function prev_group ()
1792 var col = get_col.call (this);
1794 if (this.column > 0)
1796 this.index -= this.column;
1797 if (this.index >= 0)
1798 nitems = this.column;
1801 var lastcol = (this.total - 1) % this.column;
1802 this.index = (col < lastcol ? this.total - lastcol + col
1804 this.row = this.blocks.length - 1;
1805 nitems = lastcol + 1;
1807 while (this.blocks[this.row].Index > this.index)
1812 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1813 nitems = this.blocks[this.row].Count ();
1814 this.index = (this.blocks[this.row].Index
1815 + (col < nitems ? col : nitems - 1));
1820 function next_group ()
1822 var col = get_col.call (this);
1824 if (this.column > 0)
1826 this.index += this.column - col;
1827 if (this.index < this.total)
1829 if (this.index + col >= this.total)
1831 nitems = this.total - this.index;
1832 this.index = this.total - 1;
1836 nitems = this.column;
1845 while (this.blocks[this.row].Index > this.index)
1850 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1851 nitems = this.blocks[this.row].Count ();
1852 this.index = (this.blocks[this.row].Index
1853 + (col < nitems ? col : nitems - 1));
1860 if (this.index == 0)
1862 this.index = this.total - 1;
1863 this.row = this.blocks.length - 1;
1868 if (this.blocks[this.row].Index > this.index)
1876 if (this.index == this.total)
1883 var b = this.blocks[this.row];
1884 if (this.index == b.Index + b.Count ())
1891 this.index -= get_col.call (this);
1892 while (this.blocks[this.row].Index > this.index)
1898 var b = this.blocks[this.row];
1899 if (this.column > 0)
1901 if (this.index + 1 < this.total)
1903 this.index += this.column - get_col.call (this) + 1;
1904 while (b.Index + b.Count () <= this.index)
1905 b = this.blocks[++this.row];
1909 this.index = b.Index + b.Count () - 1;
1912 MIM.Candidates.prototype.Current = function ()
1914 var b = this.blocks[this.row];
1915 return b.get (this.index - b.Index);
1918 MIM.Candidates.prototype.Select = function (selector)
1920 if (selector.type == 'selector')
1922 switch (selector.val)
1924 case '@<': first.call (this); break;
1925 case '@>': last.call (this); break;
1926 case '@-': prev.call (this); break;
1927 case '@+': next.call (this); break;
1928 case '@[': prev_group.call (this); break;
1929 case '@]': next_group.cal (this); break;
1932 return this.Current ();
1935 if (this.column > 0)
1937 col = this.index % this.column;
1938 start = this.index - col;
1939 end = start + this.column;
1943 start = this.blocks[this.row].Index;
1944 col = this.index - start;
1945 end = start + this.blocks[this.row].Count;
1947 if (end > this.total)
1949 this.index += selector.val - col;
1950 if (this.index >= end)
1951 this.index = end - 1;
1952 if (this.column > 0)
1954 if (selector.val > col)
1955 while (this.blocks[this.row].Index + this.blocks[this.row].Count
1959 while (this.blocks[this.row].Index > this.index)
1962 return this.Current ();
1966 MIM.im_domain = new Xex.Domain ('input-method', null, null);
1967 MIM.im_domain.DefType (MIM.KeySeq.prototype);
1968 MIM.im_domain.DefType (MIM.Marker.prototype);
1969 MIM.im_domain.DefType (MIM.Selector.prototype);
1970 MIM.im_domain.DefType (MIM.Rule.prototype);
1971 MIM.im_domain.DefType (MIM.Map.prototype);
1972 MIM.im_domain.DefType (MIM.State.prototype);
1975 var im_domain = MIM.im_domain;
1977 function Finsert (domain, vari, args)
1980 if (args[0].type == 'integer')
1981 text = String.fromCharCode (args[0].val);
1984 domain.context.ins (text, null);
1988 function Finsert_candidates (domain, vari, args)
1990 var ic = domain.context;
1991 var gsize = domain.variables['candidates_group_size'];
1992 var candidates = new MIM.Candidates (args, gsize ? gsize.Intval : 0);
1993 ic.ins (candidates.Current (), candidates);
1997 function Fdelete (domain, vari, args)
1999 var ic = domain.context;
2000 var pos = args[0].IsInt ? args[0].Intval : args[0].Position (ic);
2001 return new Xex.IntTerm (ic.del (pos));
2004 function Fselect (domain, vari, args)
2006 var ic = domain.context;
2007 var can = ic.candidates;
2011 var old_text = can.Current ();
2012 var new_text = can.Select (args[0]);
2013 ic.rep (old_text, new_text, can);
2016 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2020 function Fchar_at (domain, vari, args)
2022 return new Xex.IntTerm (args[0].CharAt (domain.context));
2025 function Fmove (domain, vari, args)
2027 var ic = domain.context;
2028 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2030 return new Xex.IntTerm (pos);
2033 function Fmark (domain, vari, args)
2035 args[0].Mark (domain.context);
2039 function Fpushback (domain, vari, args)
2041 var a = (args[0].IsInt ? args[0].Intval ()
2042 : args[0].IsStr ? new KeySeq (args[0])
2044 domain.context.pushback (a);
2048 function Fundo (domain, vari, args)
2050 var ic = domain.context;
2051 var n = args.length == 0 ? -2 : args[0].val;
2053 ic.keys.val.splice (ic.keys.length + n, -n);
2055 ic.keys.val.splice (n, ic.keys.length);
2060 function Fcommit (domain, vari, args)
2062 domain.context.commit ();
2066 function Funhandle (domain, vari, args)
2068 domain.context.commit ();
2069 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2072 function Fshift (domain, vari, args)
2074 var ic = domain.context;
2075 var state_name = args[0].val;
2076 var state = ic.im.state_list[state_name];
2078 throw ("Unknown state: " + state_name);
2083 function Fsurrounding_flag (domain, vari, args)
2085 return new Xex.IntTerm (-1);
2088 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2089 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2090 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2091 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2092 //im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2093 //im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2094 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2095 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2096 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2097 //im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2098 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2099 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2100 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2101 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2102 //im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2103 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2104 //im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2105 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2110 function get_global_var (vname)
2112 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2113 MIM.im_global.Load ()
2114 return MIM.im_global.domain.variables[vname];
2117 function include (node)
2119 node = node.firstElement ();
2120 if (node.nodeName != 'tags')
2123 var lang = null, name = null, extra = null;
2124 for (node = node.firstElement (); node; node = node.nextElement ())
2126 if (node.nodeName == 'language')
2127 lang = node.firstChild.nodeValue;
2128 else if (node.nodeName == 'name')
2129 name = node.firstChild.nodeValue;
2130 else if (node.nodeName == 'extra-id')
2131 extra = node.firstChild.nodeValue;
2133 if (! lang || ! MIM.imlist[lang])
2137 if (! name || ! (im = MIM.imlist[lang][name]))
2142 if (! (im = MIM.imextra[lang][extra]))
2145 if (im.load_status != MIM.LoadStatus.Loaded
2146 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2153 parsers['description'] = function (node)
2155 this.description = node.firstChild.nodeValue;
2157 parsers['variable-list'] = function (node)
2159 for (node = node.firstElement (); node; node = node.nextElement ())
2161 var vname = node.attributes['vname'].nodeValue;
2162 if (this != MIM.im_global)
2164 var vari = get_global_var (vname);
2166 this.domain.Defvar (vname);
2168 vname = Xex.Term.Parse (this.domain, node)
2171 parsers['command-list'] = function (node)
2174 parsers['macro-list'] = function (node)
2176 for (var n = node.firstElement (); n; n = n.nextElement ())
2177 if (n.nodeName == 'xi:include')
2179 var im = include (n);
2181 alert ('inclusion fail');
2183 for (var macro in im.domain.functions)
2185 var func = im.domain.functions[macro];
2186 if (func instanceof Xex.Macro)
2187 im.domain.CopyFunc (this.domain, macro);
2189 n = n.previousSibling;
2190 node.removeChild (n.nextSibling);
2192 Xex.Term.Parse (this.domain, node.firstElement (), null);
2194 parsers['title'] = function (node)
2196 this.title = node.firstChild.nodeValue;
2198 parsers['map-list'] = function (node)
2200 for (node = node.firstElement (); node; node = node.nextElement ())
2202 if (node.nodeName == 'xi:include')
2204 var im = include (node);
2207 alert ('inclusion fail');
2210 for (var mapname in im.map_list)
2211 this.map_list[mapname] = im.map_list[mapname];
2215 var map = Xex.Term.Parse (this.domain, node);
2216 this.map_list[map.name] = map;
2220 parsers['state-list'] = function (node)
2222 this.domain.map_list = this.map_list;
2223 for (node = node.firstElement (); node; node = node.nextElement ())
2225 if (node.nodeName == 'state')
2227 var state = Xex.Term.Parse (this.domain, node);
2229 state.title = this.title;
2230 if (! this.initial_state)
2231 this.initial_state = state;
2232 this.state_list[state.name] = state;
2235 delete this.domain.map_list;
2238 MIM.IM = function (lang, name, extra_id, file)
2242 this.extra_id = extra_id;
2244 this.load_status = MIM.LoadStatus.NotLoaded;
2245 this.domain = new Xex.Domain (this.lang + '-'
2246 + (this.name != 'nil'
2247 ? this.name : this.extra_id),
2248 MIM.im_domain, null);
2254 var node = Xex.Load (null, this.file);
2257 this.load_status = MIM.LoadStatus.Error;
2261 this.initial_state = null;
2262 this.state_list = {};
2263 for (node = node.firstElement (); node; node = node.nextElement ())
2265 var name = node.nodeName;
2266 var parser = parsers[name];
2268 parser.call (this, node);
2270 this.load_status = MIM.LoadStatus.Loaded;
2275 MIM.IM.prototype = proto;
2277 MIM.IC = function (im, target)
2279 if (im.load_status == MIM.LoadStatus.NotLoaded)
2281 if (im.load_status != MIM.LoadStatus.Loaded)
2282 alert ('im:' + im.name + ' error:' + im.load_status);
2284 this.target = target;
2285 this.domain = new Xex.Domain ('context', im.domain, this);
2287 this.range = new Array ();
2288 this.range[0] = this.range[1] = 0;
2290 this.initial_state = this.im.initial_state;
2291 this.keys = new MIM.KeySeq ();
2292 this.marker_positions = new Array ();
2293 this.candidate_table = new MIM.CandidateTable ();
2297 MIM.CandidateTable = function ()
2299 this.table = new Array ();
2302 MIM.CandidateTable.prototype.get = function (pos)
2304 for (var i = 0; i < this.table.length; i++)
2306 var elt = this.table[i];
2307 if (elt.from < pos && pos <= elt.to)
2312 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2314 for (var i = 0; i < this.table.length; i++)
2316 var elt = this.table[i];
2317 if (elt.from < to && elt.to > from)
2321 elt.val = candidates;
2325 this.table.push ({ from: from, to: to, val: candidates });
2328 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2330 var diff = inserted - (to - from);
2333 for (var i = 0; i < this.table.length; i++)
2335 var elt = this.table[i];
2344 MIM.CandidateTable.prototype.clear = function ()
2346 this.table.length = 0;
2349 function detach_candidates (ic)
2351 ic.candidate_table.clear ();
2352 ic.candidates = null;
2353 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2354 | ChangedStatus.CandidateList
2355 | ChangedStatus.CandidateIndex
2356 | ChangedStatus.CandidateShow);
2359 function set_cursor (prefix, pos)
2361 this.cursor_pos = pos;
2362 this.candidates = this.candidate_table.get (pos);
2365 function save_state ()
2367 this.state_var_values = this.domain.SaveValues ();
2368 this.state_preedit = this.preedit;
2369 this.state_key_head = this.key_head;
2370 this.state_pos = this.cursor_pos;
2373 function restore_state ()
2375 this.domain.RestoreValues (this.state_var_values);
2376 this.preedit = this.state_preedit;
2377 set_cursor.call (this, "restore", this.state_pos);
2380 function handle_key ()
2382 var out = this.keymap.Lookup (this.keys, this.key_head);
2385 Xex.Log ('handling ' + this.keys.val[this.key_head]
2386 + ' in ' + this.state.name + ':' + this.keymap.name);
2387 this.key_head = out.index;
2388 if (sub != this.keymap)
2391 restore_state.call (this);
2393 Xex.Log ('submap found');
2394 if (this.keymap.map_actions)
2396 Xex.Log ('taking map actions:');
2397 if (! this.take_actions (this.keymap.map_actions))
2400 else if (this.keymap.submaps)
2402 Xex.Log ('no map actions');
2403 for (var i = this.state_key_head; i < this.key_head; i++)
2405 Xex.Log ('inserting key:' + this.keys.val[i].key);
2406 this.ins (this.keys.val[i].key, null);
2409 if (! this.keymap.submaps)
2411 Xex.Log ('terminal:');
2412 if (this.keymap.branch_actions != null)
2414 Xex.Log ('branch actions:');
2415 if (! this.take_actions (this.keymap.branch_actions))
2418 if (this.keymap != this.state.keymap)
2419 this.shift (this.state);
2424 Xex.Log ('no submap');
2425 var current_state = this.state;
2426 var map = this.keymap;
2428 if (map.branch_actions)
2430 Xex.Log ('branch actions');
2431 if (! this.take_actions (map.branch_actions))
2435 if (map == this.keymap)
2437 Xex.Log ('no state change');
2438 if (map == this.initial_state.keymap
2439 && this.key_head < this.keys.val.length)
2441 Xex.Log ('unhandled');
2444 if (this.keymap != current_state.keymap)
2445 this.shift (current_state);
2446 else if (this.keymap.actions == null)
2447 this.shift (this.initial_state);
2456 this.cursor_pos = 0;
2457 this.candidate_show = false;
2458 this.prev_state = null;
2459 this.title = this.initial_state.title;
2460 this.state_preedit = '';
2461 this.state_key_head = 0;
2462 this.state_var_values = {};
2465 this.keys.val.length = 0;
2466 this.key_unhandled = false;
2467 this.unhandled_key = null;
2468 this.changed = MIM.ChangedStatus.None;
2469 this.error_message = '';
2470 this.title = this.initial_state.title;
2473 this.preedit_saved = '';
2474 this.candidate_table.clear ();
2475 this.candidates = null;
2476 this.candidate_show = false;
2477 for (var elt in this.marker_positions)
2478 this.marker_positions[elt] = 0;
2479 this.shift (this.initial_state);
2482 catch_args: new Array (Xex.CatchTag._mimtag, null),
2484 take_actions: function (actions)
2486 var func_progn = this.domain.GetFunc ('progn');
2487 var func_catch = this.domain.GetFunc ('catch');
2488 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2489 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2490 term = term.Eval (this.domain);
2491 return (! term.IsSymbol || term.val != '@mimtag');
2494 GetSurroundingChar: function (pos)
2498 pos += this.range[0];
2504 pos += this.range[1];
2505 if (pos >= this.target.value.length)
2508 return this.target.value.charCodeAt (pos);
2511 DelSurroundText: function (pos)
2516 pos += this.range[0];
2522 text = this.target.value.substring (0, pos);
2523 if (this.range[0] < this.target.value.length)
2524 text += this.target.value.substring (this.range[0]);
2525 this.target.value = text;
2526 this.range[1] -= this.range[0] - pos;
2527 this.range[0] = pos;
2531 pos += this.range[1];
2532 text = this.target.value.substring (0, this.range[1]);
2533 if (pos >= this.target.value.length)
2534 pos = this.target.value.length;
2536 text += this.target.value.substring (pos);
2537 this.target.value = text;
2541 adjust_markers: function (from, to, inserted)
2543 var diff = inserted - (to - from);
2545 for (var name in this.marker_positions)
2547 var pos = this.marker_positions[name];
2551 this.marker_positions[name] += diff;
2552 else if (pos > from)
2553 this.marker_positions[name] = from;
2556 if (this.cursor_pos >= to)
2557 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2558 else if (this.cursor_pos > from)
2559 set_cursor.call (this, 'adjust', from)
2562 preedit_replace: function (from, to, text, candidates)
2564 this.preedit = (this.preedit.substring (0, from)
2565 + text + this.preedit.substring (to));
2566 this.adjust_markers (from, to, text.length);
2567 this.candidate_table.adjust (from, to, text.length);
2569 this.candidate_table.put (from, from + text.length, candidates)
2572 ins: function (text, candidates)
2574 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2575 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2578 rep: function (old_text, new_text, candidates)
2580 this.preedit_replace (this.cursor_pos - old_text.length,
2581 this.cursor_pos, new_text, candidates);
2582 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2587 var deleted = pos - this.cursor_pos;
2588 if (pos < this.cursor_pos)
2592 this.DelSurroundText (pos);
2593 deleted = - this.cursor_pos;
2596 if (pos < this.cursor_pos)
2597 this.preedit_replace (pos, this.cursor_pos, '', null);
2601 if (pos > this.preedit.length)
2603 this.DelSurroundText (pos - this.preedit.length);
2604 deleted = this.preedit.length - this.cursor_pos;
2605 pos = this.preedit.length;
2607 if (pos > this.cursor_pos)
2608 this.preedit_replace (this.cursor_pos, pos, '', null);
2611 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2617 this.candidate_show = true;
2618 this.changed |= MIM.ChangedStatus.CandidateShow;
2623 this.candidate_show = false;
2624 this.changed |= MIM.ChangedStatus.CandidateShow;
2627 move: function (pos)
2631 else if (pos > this.preedit.length)
2632 pos = this.preedit.length;
2633 if (pos != this.cursor_pos)
2635 set_cursor.call (this, 'move', pos);
2636 this.changed |= MIM.ChangedStatus.Preedit;
2640 pushback: function (n)
2642 if (n instanceof MIM.KeySeq)
2644 if (this.key_head > 0)
2646 if (this.key_head < this.keys.val.length)
2647 this.keys.val.splice (this.key_head,
2648 this.keys.val.length - this.key_head);
2649 for (var i = 0; i < n.val.length; i++)
2650 this.keys.val.push (n.val[i]);
2656 if (this.key_head < 0)
2663 this.key_head = - n;
2664 if (this.key_head > this.keys.val.length)
2665 this.key_head = this.keys.val.length;
2671 if (this.key_head < this.keys.val.length)
2672 this.keys.val.splice (this.key_head, 1);
2677 if (this.preedit.length > 0)
2679 this.candidate_table.clear ();
2680 this.produced += this.preedit;
2681 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2685 shift: function (state)
2689 if (this.prev_state == null)
2691 state = this.prev_state;
2694 if (state == this.initial_state)
2699 this.keys.val.splice (0, this.key_head);
2701 this.prev_state = null;
2706 if (state != this.state)
2707 this.prev_state = this.state;
2709 if (state != this.state && state.enter_actions)
2710 this.take_actions (state.enter_actions);
2711 if (! this.state || this.state.title != state.title)
2712 this.changed |= MIM.ChangedStatus.StateTitle;
2714 this.keymap = state.keymap;
2715 this.state_key_head = this.key_head;
2716 save_state.call (this);
2719 Filter: function (key)
2723 this.key_unhandled = true;
2724 this.unhandled_key = key;
2727 if (key.key == '_reload')
2729 this.changed = MIM.ChangedStatus.None;
2731 this.key_unhandled = false;
2732 this.keys.val.push (key);
2734 while (this.key_head < this.keys.val.length)
2736 if (! handle_key.call (this))
2738 if (this.key_head < this.keys.val.length)
2740 this.unhandled_key = this.keys.val[this.key_head];
2741 this.keys.val.splice (this.key_head, this.key_head + 1);
2743 this.key_unhandled = true;
2749 this.key_unhandled = true;
2753 if (this.key_unhandled)
2755 this.keys.val.length = 0;
2756 this.key_head = this.state_key_head = this.commit_key_head = 0;
2758 return (! this.key_unhandled
2759 && this.produced.length == 0
2760 && this.preedit.length == 0);
2764 MIM.IC.prototype = proto;
2766 var node = Xex.Load (null, "imlist.xml");
2767 for (node = node.firstChild; node; node = node.nextSibling)
2768 if (node.nodeName == 'input-method')
2770 var lang = null, name = null, extra_id = null, file = null;
2772 for (var n = node.firstChild; n; n = n.nextSibling)
2774 if (n.nodeName == 'language')
2775 lang = n.firstChild.nodeValue;
2776 else if (n.nodeName == 'name')
2777 name = n.firstChild.nodeValue;
2778 else if (n.nodeName == 'extra-id')
2779 extra_id = n.firstChild.nodeValue;
2780 else if (n.nodeName == 'filename')
2781 file = n.firstChild.nodeValue;
2783 if (name && name != 'nil')
2785 if (! MIM.imlist[lang])
2786 MIM.imlist[lang] = {};
2787 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2789 else if (extra_id && extra_id != 'nil')
2791 if (! MIM.imextra[lang])
2792 MIM.imextra[lang] = {};
2793 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2796 if (MIM.imextra.t && MIM.imextra.t.global)
2797 MIM.im_global = MIM.imextra.t.global;
2800 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2801 MIM.im_global.load_status = MIM.LoadStatus.Error;
2807 var keys = new Array ();
2809 keys[0x08] = 'backspace';
2810 keys[0x0D] = 'return';
2811 keys[0x1B] = 'escape';
2812 keys[0x20] = 'space';
2813 keys[0x21] = 'pageup';
2814 keys[0x22] = 'pagedown';
2816 keys[0x24] = 'home';
2817 keys[0x25] = 'left';
2819 keys[0x27] = 'right';
2820 keys[0x28] = 'down';
2821 keys[0x2D] = 'insert';
2822 keys[0x2E] = 'delete';
2823 for (var i = 1; i <= 12; i++)
2824 keys[111 + i] = "f" + i;
2825 keys[0x90] = "numlock";
2826 keys[0xF0] = "capslock";
2829 keyids['U+0008'] = 'backspace';
2830 keyids['U+0009'] = 'tab';
2831 keyids['U+0018'] = 'cancel';
2832 keyids['U+001B'] = 'escape';
2833 keyids['U+0020'] = 'space';
2834 keyids['U+007F'] = 'delete';
2837 modifiers.Shift = 1;
2838 modifiers.Control = 1;
2840 modifiers.AltGraph = 1;
2843 MIM.decode_key_event = function (event)
2845 var key = event.keyIdentifier;
2847 if (key) // keydown event of Chrome
2851 var keysym = keyids[key];
2854 else if (key.match(/^U\+([0-9A-Z]+)$/))
2855 key = String.fromCharCode (parseInt (RegExp.$1, 16));
2857 key = key.toLowerCase ();
2858 if (event.shiftKey) key = "S-" + key ;
2859 if (event.altKey) key = "A-" + key ;
2860 if (event.metaKey) key = "M-" + key ;
2861 if (event.ctrlKey) key = "C-" + key ;
2862 if (key.length == 1)
2864 return new MIM.Key (key);
2868 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
2869 : event.charCode ? event.charCode
2873 if (event.type == 'keydown')
2878 if (event.shiftKey) key = "S-" + key ;
2881 key = String.fromCharCode (key);
2883 if (event.altKey) key = "A-" + key ;
2884 if (event.ctrlKey) key = "C-" + key ;
2885 return new MIM.Key (key);
2889 MIM.add_event_listener
2890 = (window.addEventListener
2891 ? function (target, type, listener) {
2892 target.addEventListener (type, listener, false);
2894 : window.attachEvent
2895 ? function (target, type, listener) {
2896 target.attachEvent ('on' + type,
2898 listener.call (target, window.event);
2901 : function (target, type, listener) {
2903 = function (e) { listener.call (target, e || window.event); };
2906 MIM.debug_print = function (event, ic)
2910 if (! MIM.debug_nodes)
2912 MIM.debug_nodes = new Array ();
2913 MIM.debug_nodes['status0'] = document.getElementById ('status0');
2914 MIM.debug_nodes['status1'] = document.getElementById ('status1');
2915 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
2916 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
2917 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
2918 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
2919 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
2920 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
2922 var target = event.target;
2923 var code = event.keyCode;
2924 var ch = event.type == 'keypress' ? event.charCode : 0;
2925 var key = MIM.decode_key_event (event);
2928 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
2929 index = (event.type == 'keydown' ? '0' : '1');
2931 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
2933 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
2934 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
2935 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
2938 MIM.debug_nodes.keypress.innerHTML = '';
2939 MIM.debug_nodes.status1.innerHTML = '';
2940 MIM.debug_nodes.keymap1.innerHTML = '';
2941 MIM.debug_nodes.preedit1.innerHTML = ''
2945 MIM.get_range = function (target, ic)
2948 if (target.selectionStart != null) // for Mozilla
2950 from = target.selectionStart;
2951 to = target.selectionEnd;
2955 var r = document.selection.createRange ();
2956 var rr = r.duplicate ();
2958 rr.moveToElementText (target);
2959 rr.setEndPoint ('EndToEnd', range);
2960 from = rr.text.length - r.text.length;
2961 to = rr.text.length;
2963 if (ic.range[0] == from && ic.range[1] == to
2964 && (to == from || target.value.substring (from, to) == ic.preedit))
2971 MIM.set_caret = function (target, ic)
2973 if (target.setSelectionRange) // Mozilla
2975 var scrollTop = target.scrollTop;
2976 target.setSelectionRange (ic.range[0], ic.range[1]);
2977 target.scrollTop = scrollTop;
2981 var range = target.createTextRange ();
2982 range.moveStart ('character', ic.range[0]);
2983 range.moveEnd ('character', ic.range[1]);
2988 MIM.update = function (target, ic)
2990 var text = target.value;
2991 target.value = (text.substring (0, ic.range[0])
2994 + text.substring (ic.range[1]));
2995 ic.range[0] += ic.produced.length;
2996 ic.range[1] = ic.range[0] + ic.preedit.length;
2997 MIM.set_caret (target, ic);
3000 MIM.reset_ic = function (event)
3002 if (event.target.mim_ic)
3004 var target = event.target;
3005 var ic = target.mim_ic;
3006 if (ic.preedit.length > 0)
3007 event.target.setSelectionRange (ic.range[1], ic.range[1]);
3012 MIM.keydown = function (event)
3014 var target = event.target;
3015 if (target.id == 'log')
3017 if (! (target.type == "text" || target.type == "textarea"))
3020 var ic = target.mim_ic;
3021 if (! ic || ic.im != MIM.current)
3023 target.mim_ic = null;
3024 Xex.Log ('creating IC');
3025 ic = new MIM.IC (MIM.current, target);
3026 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3029 MIM.add_event_listener (target, 'blur', MIM.reset_ic);
3030 MIM.get_range (target, ic)
3034 if (! MIM.get_range (target, ic))
3037 MIM.debug_print (event, ic);
3038 ic.key = MIM.decode_key_event (event);
3041 Xex.Log ("filtering " + ic.key);
3042 var result = ic.Filter (ic.key);
3043 MIM.update (target, ic);
3044 if (! ic.key_unhandled)
3045 event.preventDefault ();
3049 MIM.keypress = function (event)
3051 var target = event.target;
3052 if (target.id == 'log')
3054 if (! (target.type == "text" || target.type == "textarea"))
3057 var ic = target.mim_ic;
3061 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3064 ic.key = MIM.decode_key_event (event);
3071 Xex.Log ("filtering " + ic.key);
3072 var result = ic.Filter (ic.key);
3073 MIM.update (target, ic);
3074 if (! ic.key_unhandled)
3075 event.preventDefault ();
3077 Xex.Log ("error:" + e);
3078 event.preventDefault ();
3080 MIM.debug_print (event, ic);
3086 var lang_category = {
3088 cs: { name: 'Czech' },
3089 da: { name: 'Danish' },
3090 el: { name: 'Greek' },
3091 en: { name: 'English' },
3092 eo: { name: 'Esperanto' },
3093 fr: { name: 'French' },
3094 grc: { name: 'ClassicGreek' },
3095 hr: { name: 'Croatian' },
3096 hy: { name: 'Armenian' },
3097 ka: { name: 'Georgian' },
3098 kk: { name: 'Kazakh' },
3099 ru: { name: 'Russian' },
3100 sk: { name: 'Slovak' },
3101 sr: { name: 'Serbian' },
3102 sv: { name: 'Swedish' },
3103 vi: { name: 'Vietnamese' },
3104 yi: { name: 'Yiddish' } },
3106 ar: { name: 'Arabic' },
3107 dv: { name: 'Divehi' },
3108 fa: { name: 'Persian' },
3109 he: { name: 'Hebrew' },
3110 kk: { name: 'Kazakh' },
3111 ps: { name: 'Pushto' },
3112 ug: { name: 'Uighur' },
3113 yi: { name: 'Yiddish' } },
3115 as: { name: 'Assamese' },
3116 bn: { name: 'Bengali' },
3117 bo: { name: 'Tibetan' },
3118 gu: { name: 'Gujarati' },
3119 hi: { name: 'Hindi' },
3120 kn: { name: 'Kannada' },
3121 ks: { name: 'Kashmiri' },
3122 ml: { name: 'Malayalam' },
3123 mr: { name: 'Marathi' },
3124 ne: { name: 'Nepali' },
3125 or: { name: 'Oriya' },
3126 pa: { name: 'Panjabi' },
3127 sa: { name: 'Sanskirit' },
3128 sd: { name: 'Sindhi' },
3129 si: { name: 'Sinhalese' },
3130 ta: { name: 'Tamil' },
3131 te: { name: 'Telugu' },
3132 ur: { name: 'Urdu' } },
3134 cmc: { name: 'Cham' },
3135 km: { name: 'Khmer'},
3136 lo: { name: 'Lao' },
3137 my: { name: 'Burmese' },
3138 tai: { name: 'Tai Viet' },
3139 th: { name: 'Thai' },
3140 vi: { name: 'Vietanamese' } },
3142 ii: { name: 'Yii' },
3143 ja: { name: 'Japanese' },
3144 ko: { name: 'Korean' },
3145 zh: { name: 'Chinese' } },
3147 am: { name: 'Amharic' },
3148 ath: { name: 'Carrier' },
3149 bla: { name: 'Blackfoot' },
3150 cr: { name: 'Cree' },
3151 eo: { name: 'Esperanto' },
3152 iu: { name: 'Inuktitut' },
3153 nsk: { name: 'Naskapi' },
3154 oj: { name: 'Ojibwe' },
3155 t: { name: 'Generic' } }
3158 function categorize_im ()
3160 var cat, lang, list, name;
3161 for (lang in MIM.imlist)
3164 for (cat in lang_category)
3165 if (lang_category[cat][lang])
3167 list = lang_category[cat][lang].list;
3169 list = lang_category[cat][lang].list = {};
3173 for (name in MIM.imlist[lang])
3174 list[name] = MIM.imlist[lang][name];
3176 for (name in MIM.imlist[lang])
3177 Xex.Log ('no category ' + lang + '-' + name);
3186 clearTimeout (destroy_timer);
3187 destroy_timer = null;
3188 var target = document.getElementById ('mim-menu');
3191 for (; last_target && last_target.menu_level;
3192 last_target = last_target.parentLi)
3193 last_target.style.backgroundColor = 'white';
3194 var nodes = target.getElementsByTagName ('ul');
3195 for (var i = 0; i < nodes.length; i++)
3196 nodes[i].style.visibility = 'hidden';
3197 document.getElementsByTagName ('body')[0].removeChild (target);
3201 function destroy_menu () {
3202 if (! destroy_timer)
3203 destroy_timer = setTimeout (destroy, 1000);
3206 function show_submenu (event)
3210 clearTimeout (destroy_timer);
3211 destroy_timer = null;
3213 var target = event.target;
3214 if (! target.menu_level)
3216 if (last_target && target.parentLi != last_target)
3218 last_target.style.backgroundColor = 'white';
3219 if (target.menu_level < last_target.menu_level)
3221 last_target = last_target.parentLi;
3222 last_target.style.backgroundColor = 'white';
3224 var uls = last_target.getElementsByTagName ('ul');
3225 for (var i = 0; i < uls.length; i++)
3226 uls[i].style.visibility = 'hidden';
3228 last_target = target;
3229 target.style.backgroundColor = 'yellow';
3230 if (target.menu_level < 3)
3232 target.lastChild.style.visibility = 'visible';
3233 target.lastChild.style.left = target.clientWidth + 'px';
3235 event.preventDefault ();
3238 function select_im (event)
3240 var target = event.target;
3243 MIM.current = target.im;
3246 event.preventDefault ();
3249 function create_ul (visibility)
3251 var ul = document.createElement ('ul');
3252 ul.style.position = 'absolute';
3253 ul.style.margin = '0px';
3254 ul.style.padding = '0px';
3255 ul.style.border = '1px solid gray';
3256 ul.style.borderBottom = 'none';
3257 ul.style.top = '-1px';
3258 ul.style.backgroundColor = 'white';
3259 ul.style.visibility = visibility;
3263 function create_li (level, text)
3265 var li = document.createElement ('li');
3266 li.style.position = 'relative';
3267 li.style.margin = '0px';
3268 li.style.padding = '1px';
3269 li.style.borderBottom = '1px solid gray';
3270 li.style.top = '0px';
3271 li.style.listStyle = 'none';
3272 li.menu_level = level;
3273 li.appendChild (document.createTextNode (text));
3279 function create_menu (event)
3281 var target = event.target;
3283 if (! ((target.type == "text" || target.type == "textarea")
3284 && event.which == 1 && event.ctrlKey))
3289 menu = create_ul ('visible');
3290 menu.style.fontFamily = 'sans-serif';
3291 menu.style.fontWeight = 'bold';
3292 menu.id = 'mim-menu';
3293 menu.onclick = select_im;
3294 menu.onmouseover = show_submenu;
3295 menu.onmouseout = destroy_menu;
3296 for (var catname in lang_category)
3298 var cat = lang_category[catname];
3299 var li = create_li (1, catname);
3300 var sub = create_ul ('hidden');
3301 for (var langname in cat)
3303 var lang = cat[langname];
3306 var sub_li = create_li (2, lang.name);
3307 sub_li.parentLi = li;
3308 var subsub = create_ul ('hidden');
3309 for (var name in lang.list)
3311 var im = lang.list[name];
3312 var subsub_li = create_li (3, im.name);
3313 subsub_li.parentLi = sub_li;
3315 subsub.appendChild (subsub_li);
3317 sub_li.appendChild (subsub);
3318 sub.appendChild (sub_li);
3320 li.appendChild (sub);
3321 menu.appendChild (li);
3323 document.mimmenu = menu;
3324 lang_category = null;
3326 menu.style.left = (event.clientX - 10) + "px";
3327 menu.style.top = (event.clientY - 10) + "px";
3328 document.getElementsByTagName ('body')[0].appendChild (menu);
3331 MIM.init = function ()
3333 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3334 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3335 MIM.add_event_listener (window, 'mousedown', create_menu);
3336 if (window.location == 'http://localhost/mim/index.html')
3337 MIM.server = 'http://localhost/mim';
3338 MIM.current = MIM.imlist['vi']['telex'];
3342 MIM.test = function ()
3344 var im = MIM.imlist['t']['latn-post'];
3345 var ic = new MIM.IC (im, null);
3347 ic.Filter (new MIM.Key ('a'));
3348 ic.Filter (new MIM.Key ("'"));
3351 document.getElementById ('text').value = ic.produced + ic.preedit;
3354 document.getElementById ('text').value
3355 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3357 if (e instanceof Xex.ErrTerm)
3365 MIM.init_debug = function ()
3368 Xex.LogNode = document.getElementById ('log');