1 // -* coding: utf-8; -*
6 UnknownError: "unknown-error",
7 WrongArgument: "wrong-argument",
9 InvalidInteger: "invalid-integer",
10 TermTypeInvalid: "term-type-invalid",
11 FunctionConflict: "function-conflict",
12 VariableTypeConflict: "variable-type-conflict",
13 VariableRangeConflict: "variable-range-conflict",
14 VariableWrongRange: "variable-wrong-range",
15 VariableWrongValue: "variable-wrong-value",
17 UnknownFunction: "unknown-function",
18 MacroExpansionError: "macro-expansion-error",
19 NoVariableName: "no-variable-name",
20 NoFunctionName: "no-funcion-name",
23 ArithmeticError: "arithmetic-error",
24 WrongType: "wrong-type",
25 IndexOutOfRange: "index-out-of-range",
26 ValueOutOfRange: "value-out-of-range",
27 NoLoopToBreak: "no-loop-to-break",
28 UncaughtThrow: "uncaught-throw"
31 Xex.Variable = function (domain, name, desc, val, range)
40 Xex.Variable.prototype.clone = function ()
42 return new Xex.Variable (this.domain, this.name, this.desc,
43 this.val, this.range);
46 Xex.Variable.prototype.Equals = function (obj)
48 return ((obj instanceof Xex.Variable)
49 && obj.name == this.name);
52 Xex.Variable.prototype.SetValue = function (term)
58 Xex.Function = function (name, with_var, min_args, max_args)
61 this.with_var = with_var;
62 this.min_args = min_args;
63 this.max_args = max_args;
66 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
69 this.with_var = with_var;
70 this.min_args = min_args;
71 this.max_args = max_args;
72 this.builtin = builtin;
75 Xex.Subrountine.prototype.Call = function (domain, vari, args)
77 newargs = new Array ();
78 for (var i = 0; i < args.length; i++)
80 newargs[i] = args[i].Eval (domain);
84 return this.builtin (domain, vari, newargs)
87 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
90 this.with_var = with_var;
91 this.min_args = min_args;
92 this.max_args = max_args;
93 this.builtin = builtin;
96 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
98 return this.builtin (domain, vari, args)
101 Xex.Lambda = function (name, min_args, max_args, args, body)
104 this.min_args = min_args;
105 this.max_args = max_args;
110 Xex.Lambda.prototype.Call = function (domain, vari, args)
112 var current = domain.bindings;
113 var result = Xex.Zero;
114 var limit = max_args >= 0 ? args.length : args.length - 1;
118 for (i = 0; i < limit; i++)
120 result = args[i].Eval (domain);
121 if (domain.Thrown ())
123 domain.Bind (this.args[i], result);
127 var list = new Array ();
128 for (i = 0; i < args[limit].length; i++)
130 result = args[limit].Eval (domain);
131 if (domain.Thrown ())
135 domain.Bind (this.args[limit], list);
138 domain.Catch (Xex.CatchTag.Return);
139 for (var term in this.body)
141 result = term.Eval (domain);
142 if (domain.Thrown ())
149 domain.UnboundTo (current);
154 Xex.Macro = function (name, min_args, max_args, args, body)
157 this.min_args = min_args;
158 this.max_args = max_args;
163 Xex.Macro.prototype.Call = function (domain, vari, args)
165 var current = domain.bindings;
166 var result = Xex.Zero;
170 for (i = 0; i < args.length; i++)
171 domain.Bind (this.args[i], args[i]);
173 domain.Catch (Xex.CatchTag.Return);
174 for (var term in body)
176 result = term.Eval (domain);
177 if (domain.Thrown ())
184 domain.UnboundTo (current);
189 Xex.Bindings = function (vari)
192 this.old_value = vari.val;
195 Xex.Bindings.prototype.UnboundTo = function (boundary)
197 for (var b = this; b != boundary; b = b.next)
198 b.vari.val = b.old_value;
202 Xex.Bind = function (bindings, vari, val)
204 var b = new Xex.Bindings (vari);
215 Xex.Domain = function (name, parent, context)
218 this.context = context;
221 if (name != 'basic' && ! parent)
222 parent = Xex.BasicDomain
223 this.parent = parent;
230 for (elt in parent.termtypes)
231 this.termtypes[elt] = parent.termtypes[elt];
232 for (elt in parent.functions)
233 this.functions[elt] = parent.functions[elt];
234 for (elt in parent.variables)
235 this.variables[elt] = parent.variables[elt];
238 this.call_stack = new Array ();
239 this.bindings = null;
240 this.catch_stack = new Array ();
241 this.catch_count = 0;
245 Xex.Domain.prototype = {
246 CallStackCount: function () { return this.call_stack.length; },
247 CallStackPush: function (term) { this.call_stack.push (term); },
248 CallStackPop: function () { this.call_stack.pop (); },
249 Bind: function (vari, val)
251 this.bindings = Xex.Bind (this.bindings, vari, val);
253 UnboundTo: function (boundary)
256 this.bindings = this.bindings.UnboundTo (boundary);
258 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
261 this.catch_stack.pop ();
262 if (this.catch_count > this.catch_stack.length)
267 if (this.catch_count < this.catch_stack.length)
269 this.caught = (this.catch_count == this.catch_stack.length - 1);
275 ThrowReturn: function ()
277 for (var i = this.catch_stack.length - 1; i >= 0; i--)
280 if (this.catch_stack[i] == Xex.CatchTag.Return)
284 ThrowBreak: function ()
286 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
287 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
288 "No surrounding loop to break");
291 ThrowSymbol: function (tag)
293 var i = this.catch_count;
294 for (var j = this.catch_stack.length - 1; j >= 0; j--)
297 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
299 this.catch_count = i;
303 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
304 "No corresponding catch: " + tag);
306 DefType: function (obj)
309 if (this.termtypes[type])
310 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
311 "Already defined: " + type);
312 if (this.functions[type])
313 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
314 "Already defined as a funciton or a macro: "
316 this.termtypes[type] = obj.Parser;
318 DefSubr: function (builtin, name, with_var, min_args, max_args)
320 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
323 DefSpecial: function (builtin, name, with_var, min_args, max_args)
325 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
328 Defun: function (name, min_args, max_args, args, body)
330 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
333 DefunByFunc: function (func) { this.functions[func.name] = func; },
334 Defmacro: function (name, min_args, max_args, args, body)
336 this.functions[name] = new Xex.Macro (name, min_args, max_args,
339 DefAlias: function (alias, fname)
341 var func = this.functions[fname];
344 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
345 if (this.termtypes[alias])
346 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
347 "Already defined as a term type: " + alias);
348 if (this.functions[alias])
349 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
350 "Already defined as a function: " + alias);
351 this.functions[alias] = func;
353 Defvar: function (name, desc, val, range)
355 var vari = new Xex.Variable (this, name, desc, val, range);
356 this.variables[name] = vari;
359 GetFunc: function (name)
361 var func = this.functions[name];
363 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
364 "Unknown function: " + name);
367 CopyFunc: function (domain, name)
369 var func = this.functions[name];
370 domain.DefunByFunc (func);
373 CopyFuncAll: function (domain)
375 for (var elt in this.functions)
376 domain.DefunByFunc (this.functions[elt]);
378 GetVarCreate: function (name)
380 var vari = this.variables[name];
382 vari = this.variables[name] = new Xex.Variable (this, name, null,
386 GetVar: function (name) { return this.variables[name]; },
387 SaveValues: function ()
390 for (var elt in this.variables)
392 //if (! this.variables[elt].val)
393 //alert ('unknown value of ' + elt);
395 values[elt] = this.variables[elt].val.Clone ();
399 RestoreValues: function (values)
404 var vari = this.variables[name];
405 vari.val = values[name];
410 Xex.Term = function (type) { this.type = type; }
411 Xex.Term.prototype = {
412 IsTrue: function () { return true; },
413 Eval: function (domain) { return this.Clone (); },
414 Clone: function (domain) { return this; },
415 Equals: function (obj)
417 return (this.type == obj.type
419 && obj.val == this.val);
421 Matches: function (obj) { return this.Equals (obj); },
422 toString: function ()
424 if (this.val != undefined)
425 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
426 return '<' + this.type + '/>';
428 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
429 "Not an integer"); },
430 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
434 Node.prototype.firstElement = function ()
436 for (var n = this.firstChild; n; n = n.nextSibling)
442 Node.prototype.nextElement = function ()
444 for (var n = this.nextSibling; n; n = n.nextSibling)
451 function parse_defvar (domain, node)
453 var name = node.attributes['vname'].nodeValue;
455 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
456 var vari = domain.variables[name];
457 var desc, val, range;
460 desc = vari.description;
464 node = node.firstElement ();
465 if (node && node.nodeName == 'description')
467 desc = node.firstChild.nodeValue;
468 node = node.nextElement ();
472 val = Xex.Term.Parse (domain, node);
473 node = node.nextElement ();
474 if (node && node.nodeName == 'possible-values')
475 for (node = node.firstElement (); node; node = node.nextElement ())
478 if (node.nodeName == 'range')
481 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
482 'Range not allowed for ' + name);
484 for (var n = node.firstElement (); n; n = n.nextElement ())
486 var v = Xex.Term.Parse (domain, n);
488 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
489 'Invalid range value: ' + val);
495 pval = Xex.Term.Parse (domain, node);
496 if (val.type != pval.type)
497 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
498 'Invalid possible value: ' + pval);
501 range = new Array ();
507 domain.Defvar (name, desc, val, range);
511 function parse_defun_head (domain, node)
513 var name = node.attributes['fname'].nodeValue;
515 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
516 var args = new Array ();
517 var nfixed = 0, noptional = 0, nrest = 0;
519 node = node.firstElement ();
520 if (node && node.nodeName == 'args')
523 for (n = n.firstElement (); n; n = n.nextElement ())
525 if (n.nodeName == 'fixed')
527 else if (n.nodeName == 'optional')
529 else if (n.nodeName == 'rest')
532 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
535 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
536 for (n = node.firstElement (); n; n = n.nextElement ())
537 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
539 args.min_args = nfixed;
540 args.max_args = nrest == 0 ? nfixed + noptional : -1;
542 if (node.nodeName == 'defun')
543 domain.Defun (name, args, null);
545 domain.Defmacro (name, args, null);
549 function parse_defun_body (domain, node)
551 var name = node.attributes['fname'].nodeValue;
552 var func = domain.GetFunc (name);
554 for (node = node.firstElement (); node; node = node.nextElement ())
555 if (node.nodeName != 'description' && node.nodeName != 'args')
557 body = Xex.Term.Parse (domain, node, null);
561 Xex.Term.Parse = function (domain, node, stop)
563 if (arguments.length == 2)
565 var name = node.nodeName;
566 var parser = domain.termtypes[name];
569 return parser (domain, node);
570 if (name == 'defun' || name == 'defmacro')
572 name = parse_defun_head (domain, node);
573 parse_defun_body (domain, node);
574 return new Xex.StrTerm (name);
576 if (name == 'defvar')
578 name = parse_defvar (domain, node);
579 return new Xex.StrTerm (name);
581 return new Xex.Funcall.prototype.Parser (domain, node);
583 for (var n = node; n && n != stop; n = n.nextElement ())
584 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
585 parse_defun_head (domain, n);
587 for (var n = node; n && n != stop; n = n.nextElement ())
589 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
590 parse_defun_body (domain, n);
591 else if (n.nodeName == 'defvar')
592 parse_defvar (domain, n);
596 terms = new Array ();
597 terms.push (Xex.Term.Parse (domain, n));
604 Xex.Varref = function (vname)
610 var proto = new Xex.Term ('varref');
612 proto.Clone = function () { return new Xex.Varref (this.val); }
613 proto.Eval = function (domain)
615 if (! this.vari || this.vari.domain != domain)
616 this.vari = domain.GetVarCreate (this.val);
617 return this.vari.val;
620 proto.Parser = function (domain, node)
622 return new Xex.Varref (node.attributes['vname'].nodeValue);
625 Xex.Varref.prototype = proto;
628 var null_args = new Array ();
630 Xex.Funcall = function (func, vari, args)
634 this.args = args || null_args;
638 var proto = new Xex.Term ('funcall');
640 proto.Parser = function (domain, node)
642 var fname = node.nodeName;
645 if (fname == 'funcall')
646 fname = node.attributes['fname'].nodeValue;
647 var func = domain.GetFunc (fname);
649 attr = node.attributes['vname'];
650 vari = attr != undefined ? domain.GetVarCreate (attr.nodeValue) : false;
651 var args = Xex.Term.Parse (domain, node.firstElement (), null);
652 return new Xex.Funcall (func, vari, args);
655 proto.New = function (domain, fname, vname, args)
657 var func = domain.GetFunc (fname);
658 var vari = vname ? domain.GetVarCreate (vname) : null;
659 var funcall = new Xex.Funcall (func, vari, args);
660 if (func instanceof Xex.Macro)
661 funcall = funcall.Eval (domain);
665 proto.Eval = function (domain)
667 return this.func.Call (domain, this.vari, this.args);
670 proto.Clone = function ()
672 return new Xex.Funcall (this.func, this.vari, this.args);
675 proto.Equals = function (obj)
677 return (obj.type == 'funcall'
678 && obj.func == this.func
679 && obj.vari.Equals (this.vari)
680 && obj.args.length == this.func.length);
683 proto.toString = function ()
686 var len = this.args.length;
688 return '<' + this.func.name + '/>';
689 for (var i = 0; i < len; i++)
690 arglist += this.args[i].toString ();
691 return '<' + this.func.name + '>' + arglist + '</' + this.func.name + '>';
694 Xex.Funcall.prototype = proto;
697 Xex.ErrTerm = function (ename, message, stack)
700 this.message = message;
705 var proto = new Xex.Term ('error');
707 proto.IsError = true;
709 proto.Parser = function (domain, node)
711 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
712 node.innerText, false);
715 proto.CallStack = function () { return stack; }
717 proto.SetCallStack = function (value) { statck = value; }
719 proto.Clone = function ()
721 return new Xex.ErrTerm (ename, message, false);
724 proto.Equals = function (obj)
727 && obj.ename == ename && obj.message == message
728 && (obj.stack ? (stack && stack.length == obj.stack.length)
732 proto.Matches = function (obj)
734 return (obj.IsError && obj.ename == ename);
737 proto.toString = function ()
739 return '<error ename="' + this.ename + '">' + this.message + '</error>';
742 Xex.ErrTerm.prototype = proto;
745 Xex.IntTerm = function (num) { this.val = num; };
747 var proto = new Xex.Term ('integer');
749 proto.Intval = function () { return this.val; };
750 proto.IsTrue = function () { return this.val != 0; }
751 proto.Clone = function () { return new Xex.IntTerm (this.val); }
752 proto.Parser = function (domain, node)
754 var str = node.firstChild.nodeValue;
756 if (str.charAt (0) == '?' && str.length == 2)
757 return new Xex.IntTerm (str.charCodeAt (1));
758 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
760 Xex.IntTerm.prototype = proto;
763 Xex.StrTerm = function (str) { this.val = str; };
765 var proto = new Xex.Term ('string');
767 proto.Strval = function () { return this.val; };
768 proto.IsTrue = function () { return this.val.length > 0; }
769 proto.Clone = function () { return new Xex.StrTerm (this.val); }
770 proto.Parser = function (domain, node)
772 return new Xex.StrTerm (node.firstChild.nodeValue);
774 Xex.StrTerm.prototype = proto;
777 Xex.SymTerm = function (str) { this.val = str; };
779 var proto = new Xex.Term ('symbol');
780 proto.IsSymbol = true;
781 proto.IsTrue = function () { return this.val != 'nil'; }
782 proto.Clone = function () { return new Xex.SymTerm (this.val); }
783 proto.Parser = function (domain, node)
785 return new Xex.SymTerm (node.firstChild.nodeValue);
787 Xex.SymTerm.prototype = proto;
790 Xex.LstTerm = function (list) { this.val = list; };
792 var proto = new Xex.Term ('list');
794 proto.IsTrue = function () { return this.val.length > 0; }
795 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
797 proto.Equals = function (obj)
799 if (obj.type != 'list' || obj.val.length != this.val.length)
801 var i, len = this.val.length;
802 for (i = 0; i < len; i++)
803 if (! this.val[i].Equals (obj.val[i]))
808 proto.Parser = function (domain, node)
810 var list = Xex.Term.Parse (domain, node.firstElement (), null);
811 return new Xex.LstTerm (list);
814 proto.toString = function ()
816 var len = this.val.length;
821 for (var i = 0; i < len; i++)
822 str += this.val[i].toString ();
823 return str + '</list>';
825 Xex.LstTerm.prototype = proto;
829 var basic = new Xex.Domain ('basic', null, null);
831 function Fset (domain, vari, args)
833 return vari.SetValue (args[0]);
836 function maybe_set_intvar (vari, n)
838 var term = new Xex.IntTerm (n);
840 vari.SetValue (term);
844 function Fset (domain, vari, args)
847 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
848 'No variable name to set');
849 vari.SetValue (args[0]);
853 function maybe_set_intvar (vari, n)
855 var term = new IntTerm (n);
857 vari.SetValue (term);
861 function Fadd (domain, vari, args)
863 var n = vari ? vari.val.Intval () : 0;
864 var len = args.length;
866 for (var i = 0; i < len; i++)
867 n += args[i].Intval ();
868 return maybe_set_intvar (vari, n);
871 function Fmul (domain, vari, args)
873 var n = vari ? vari.val.Intval () : 1;
874 for (var i = 0; i < args.length; i++)
876 return maybe_set_intvar (vari, n);
879 function Fsub (domain, vari, args)
885 n = args[0].Intval ();
890 n = vari.val.Intval ();
893 while (i < args.length)
894 n -= args[i++].Intval ();
895 return maybe_set_intvar (vari, n);
898 function Fdiv (domain, vari, args)
904 n = args[0].Intval ();
909 n = vari.val.Intval ();
912 while (i < args.length)
913 n /= args[i++].Intval ();
914 return maybe_set_intvar (vari, n);
917 function Fmod (domain, vari, args)
919 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
922 function Flogior (domain, vari, args)
924 var n = vari == null ? 0 : vari.val;
925 for (var i = 0; i < args.length; i++)
927 return maybe_set_intvar (vari, n);
930 function Fand (domain, vari, args)
932 var len = args.length;
933 for (var i = 0; i < len; i++)
935 var result = args[i].Eval (domain);
936 if (domain.Thrown ())
938 if (! result.IsTrue ())
944 function For (domain, vari, args)
946 var len = args.length;
947 for (var i = 0; i < len; i++)
949 var result = args[i].Eval (domain);
950 if (domain.Thrown ())
952 if (result.IsTrue ())
958 function Feq (domain, vari, args)
960 for (var i = 1; i < args.length; i++)
961 if (! args[i - 1].Equals (args[i]))
966 function Flt (domain, vari, args)
968 var n = args[0].Intval;
970 for (var i = 1; i < args.length; i++)
972 var n1 = args[i].Intval;
980 function Fle (domain, vari, args)
982 var n = args[0].Intval;
983 for (var i = 1; i < args.length; i++)
985 var n1 = args[i].Intval;
993 function Fgt (domain, vari, args)
995 var n = args[0].Intval;
996 for (var i = 1; i < args.length; i++)
998 var n1 = args[i].Intval;
1006 function Fge (domain, vari, args)
1008 var n = args[0].Intval;
1009 for (var i = 1; i < args.Length; i++)
1011 var n1 = args[i].Intval;
1019 function Fprogn (domain, vari, args)
1021 var result = Xex.One;
1022 var len = args.length;
1024 for (var i = 0; i < len; i++)
1026 result = args[i].Eval (domain);
1027 if (domain.Thrown ())
1033 function Fif (domain, vari, args)
1035 var result = args[0].Eval (domain);
1037 if (domain.Thrown ())
1039 if (result.IsTrue ())
1040 return args[1].Eval (domain);
1041 if (args.length == 2)
1043 return args[2].Eval (domain);
1046 function Fcond (domain, vari, args)
1048 for (var i = 0; i < args.length; i++)
1050 var list = args[i].val;
1051 var result = list.val[0].Eval (doamin);
1052 if (result.isTrue ())
1054 for (var j = 1; j < list.val.length; j++)
1057 result = list.val[j].Eval (domain);
1059 if (domain.Thrown ())
1068 function eval_terms (domain, terms, idx)
1070 var result = Xex.Zero;
1071 domain.caught = false;
1072 for (var i = idx; i < terms.length; i++)
1074 result = terms[i].Eval (domain);
1075 if (domain.Thrown ())
1081 function Fcatch (domain, vari, args)
1086 if (args[0].IsError)
1089 result = eval_terms (domain, args, 1);
1091 if (e instanceof Xex.ErrTerm)
1093 if (! args[0].Matches (e))
1101 else if (args[0].IsSymbol)
1104 domain.Catch (args[0].val);
1105 result = eval_terms (domain, args, 1);
1109 vari.SetValue (result);
1117 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1118 "Not a symbol nor an error: " + args[0]);
1121 function Fthrow (domain, vari, args)
1123 if (args[0].IsSymbl)
1125 domain.ThrowSymbol (args[0]);
1126 return (args[args.length - 1]);
1128 if (args[0].IsError)
1132 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1133 "Not a symbol nor an error:" + args[0]);
1136 Xex.BasicDomain = basic;
1138 basic.DefSubr (Fset, "set", true, 1, 1);
1139 if (basic.functions['='])
1140 alert (basic.functions['=']);
1141 basic.DefAlias ("=", "set");
1142 //basic.DefSubr (Fnot, "not", false, 1, 1);
1143 //basic.DefAlias ("!", "not");
1144 basic.DefSubr (Fadd, "add", true, 1, -1);
1145 basic.DefSubr (Fmul, "mul", true, 1, -1);
1146 basic.DefAlias ("*", "mul");
1147 basic.DefSubr (Fsub, "sub", true, 1, -1);
1148 basic.DefAlias ("-", "sub");
1149 basic.DefSubr (Fdiv, "div", true, 1, -1);
1150 basic.DefAlias ("/", "div");
1151 basic.DefSubr (Fmod, "mod", true, 1, 2);
1152 basic.DefAlias ("%", "mod");
1153 basic.DefSubr (Flogior, "logior", true, 1, -1);
1154 basic.DefAlias ('|', "logior");
1155 //basic.DefSubr (Flogand, "logand", true, 1, -1);
1156 //basic.DefAlias ("&", "logand");
1157 //basic.DefSubr (Flsh, "lsh", true, 1, 2);
1158 //basic.DefAlias ("<<", "lsh");
1159 //basic.DefSubr (Frsh, "rsh", true, 1, 2);
1160 //basic.DefAlias (">>", "rsh");
1161 basic.DefSubr (Feq, "eq", false, 2, -1);
1162 basic.DefAlias ("==", "eq");
1163 //basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1164 //basic.DefAlias ("!=", "noteq");
1165 basic.DefSubr (Flt, "lt", false, 2, -1);
1166 basic.DefAlias ("<", "lt");
1167 basic.DefSubr (Fle, "le", false, 2, -1);
1168 basic.DefAlias ("<=", "le");
1169 basic.DefSubr (Fgt, "gt", false, 2, -1);
1170 basic.DefAlias (">", "gt");
1171 basic.DefSubr (Fge, "ge", false, 2, -1);
1172 basic.DefAlias (">=", "ge");
1173 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1175 //basic.DefSubr (Fappend, "append", true, 0, -1);
1176 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1177 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1178 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1179 //basic.DefSubr (Fins, "ins", true, 2, 2);
1180 //basic.DefSubr (Fdel, "del", true, 2, 2);
1181 //basic.DefSubr (Feval, "eval", false, 1, 1);
1182 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1183 //basic.DefSubr (Freturn, "return", false, 0, 1);
1184 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1186 basic.DefSpecial (Fand, "and", false, 1, -1);
1187 basic.DefAlias ("&&", "and");
1188 basic.DefSpecial (For, "or", false, 1, -1);
1189 basic.DefAlias ("||", "or");
1190 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1191 basic.DefAlias ("expr", "progn");
1192 basic.DefSpecial (Fif, "if", false, 2, 3);
1193 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1194 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1195 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1196 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1197 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1198 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1199 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1200 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1202 basic.DefType (Xex.Funcall.prototype);
1203 basic.DefType (Xex.Varref.prototype);
1204 basic.DefType (Xex.ErrTerm.prototype);
1205 basic.DefType (Xex.IntTerm.prototype);
1206 basic.DefType (Xex.StrTerm.prototype);
1207 basic.DefType (Xex.SymTerm.prototype);
1208 basic.DefType (Xex.LstTerm.prototype);
1212 Xex.Zero = new Xex.IntTerm (0);
1213 Xex.One = new Xex.IntTerm (1);
1214 Xex.nil = new Xex.SymTerm ('nil');
1216 Xex.Load = function (server, file)
1218 var obj = new XMLHttpRequest ();
1219 var url = server ? server + '/' + file : file;
1220 obj.open ('GET', url, false);
1221 obj.overrideMimeType ('text/xml');
1223 return obj.responseXML.firstChild;
1227 // URL of the input method server.
1228 server: "http://www.m17n.org/common/mim-js",
1229 // Boolean flag to tell if MIM is active or not.
1231 // Boolean flag to tell if MIM is running in debug mode or not.
1233 // List of main input methods.
1235 // List of extra input methods;
1237 // Global input method data
1239 // Currently selected input method.
1243 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1250 CandidateIndex:0x10,
1252 Preedit: 0x06, // PreeditText | CursorPos
1253 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1275 ParseError: "parse-error"
1280 var keysyms = new Array ();
1281 keysyms["bs"] = "backspace";
1282 keysyms["lf"] = "linefeed";
1283 keysyms["cr"] = keysyms["enter"] = "return";
1284 keysyms["esc"] = "escape";
1285 keysyms["spc"] = "space";
1286 keysyms["del"] = "delete";
1288 function decode_keysym (str) {
1289 var parts = str.split ("-");
1290 var len = parts.length, i;
1291 var has_modifier = len > 1;
1293 for (i = 0; i < len - 1; i++)
1294 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1296 var key = parts[len - 1];
1299 key = keysyms[key.toLowerCase ()];
1305 for (i = 1; i < len - 1; i++)
1306 str += '-' + parts[i];
1315 parts = new Array ();
1322 MIM.Key = function (val)
1325 this.has_modifier = false;
1326 if (typeof val == 'string' || val instanceof String)
1328 this.key = decode_keysym (val);
1330 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1331 if (this.key instanceof Array)
1333 this.key = this.key[0];
1334 this.has_modifier = true;
1337 else if (typeof val == 'number' || val instanceof Number)
1338 this.key = String.fromCharCode (val);
1340 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1343 MIM.Key.prototype.toString = function () { return this.key; };
1347 MIM.KeySeq = function (seq)
1349 this.val = new Array ();
1350 this.has_modifier = false;
1356 var len = seq.val.length;
1357 for (var i = 0; i < len; i++)
1360 if (v.type != 'string' && v.type != 'integer'
1361 && v.type != 'symbol')
1362 throw new Xex.ErrTerm (MIM.Error.ParseError,
1363 "Invalid key: " + v);
1364 var key = new MIM.Key (v.val);
1365 this.val.push (key);
1366 if (key.has_modifier)
1367 this.has_modifier = true;
1372 var len = seq.val.length;
1373 for (var i = 0; i < len; i++)
1374 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1377 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1381 var proto = new Xex.Term ('keyseq');
1382 proto.Clone = function () { return this; }
1383 proto.Parser = function (domain, node)
1385 var seq = new Array ();
1386 for (node = node.firstChild; node; node = node.nextSibling)
1387 if (node.nodeType == 1)
1389 var term = Xex.Term.Parse (domain, node);
1390 return new MIM.KeySeq (term);
1392 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1394 proto.toString = function ()
1396 var len = this.val.length;
1400 var str = '<keyseq>';
1401 for (var i = 0; i < len; i++)
1405 else if (this.has_modifier)
1407 str += this.val[i].toString ();
1409 return str + '</keyseq>';
1412 MIM.KeySeq.prototype = proto;
1416 MIM.Marker = function () { }
1417 MIM.Marker.prototype = new Xex.Term ('marker');
1418 MIM.Marker.prototype.CharAt = function (ic)
1420 var p = this.Position (ic);
1422 return ic.GetSurroundingChar (p);
1423 else if (p >= ic.preedit.length)
1424 return ic.GetSurroundingChar (p - ic.preedit.length);
1425 return ic.preedit.charCodeAt (p);
1428 MIM.NamedMarker = function (name) { this.val = name; }
1429 MIM.NamedMarker.prototype = new MIM.Marker ();
1430 MIM.NamedMarker.prototype.Position = function (ic)
1432 var p = ic.marker_positions[this.val];
1433 return (p == undefined ? 0 : p);
1435 MIM.NamedMarker.prototype.Mark = function (ic)
1437 ic.marker_positions[this.val] = ic.cursor_pos;
1440 MIM.PredefinedMarker = function (name) { this.val = name; }
1441 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1442 MIM.PredefinedMarker.prototype.Position = function (ic)
1444 if (typeof this.pos == 'number')
1446 return this.pos (ic);
1449 var predefined = { }
1451 function def_predefined (name, position)
1453 predefined[name] = new MIM.PredefinedMarker (name);
1454 predefined[name].pos = position;
1457 def_predefined ('@<', 0);
1458 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1459 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1460 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1461 def_predefined ('@[', function (ic) {
1462 if (ic.cursor_pos > 0)
1464 var pos = ic.cursor_pos;
1465 return ic.preedit.FindProp ('candidates', pos - 1).from;
1469 def_predefined ('@]', function (ic) {
1470 if (ic.cursor_pos < ic.preedit.length - 1)
1472 var pos = ic.cursor_pos;
1473 return ic.preedit.FindProp ('candidates', pos).to;
1475 return ic.preedit.length;
1477 for (var i = 0; i < 10; i++)
1478 def_predefined ("@" + i, i);
1479 predefined['@first'] = predefined['@<'];
1480 predefined['@last'] = predefined['@>'];
1481 predefined['@previous'] = predefined['@-'];
1482 predefined['@next'] = predefined['@+'];
1483 predefined['@previous-candidate-change'] = predefined['@['];
1484 predefined['@next-candidate-change'] = predefined['@]'];
1486 MIM.SurroundMarker = function (name)
1489 this.distance = parseInt (name.slice (2));
1490 if (isNaN (this.distance))
1491 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1493 MIM.SurroundMarker.prototype = new MIM.Marker ();
1494 MIM.SurroundMarker.prototype.Position = function (ic)
1496 return ic.cursor_pos + this.distance;
1499 MIM.Marker.prototype.Parser = function (domain, node)
1501 var name = node.firstChild.nodeValue;
1502 if (name.charAt (0) == '@')
1504 var n = predefined[name];
1507 if (name.charAt (1) == '-')
1508 return new MIM.SurroundMarker (name);
1509 throw new Xex.ErrTerm (MIM.Error.ParseError,
1510 "Invalid marker: " + name);
1512 return new MIM.NamedMarker (name);
1516 MIM.Selector = function (name)
1520 MIM.Selector.prototype = new Xex.Term ('selector');
1524 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1525 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1526 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1527 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1528 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1529 selectors["@["] = selectors["@previous-candidate-change"]
1530 = new MIM.Selector ('@[');
1531 selectors["@]"] = selectors["@next-candidate-change"]
1532 = new MIM.Selector ('@]');
1534 MIM.Selector.prototype.Parser = function (domain, node)
1536 var name = node.firstChild.nodeValue;
1537 var s = selectors[name];
1539 throw new Xex.ErrTerm (MIM.Error.ParseError,
1540 "Invalid selector: " + name);
1545 MIM.Rule = function (keyseq, actions)
1547 this.keyseq = keyseq;
1548 this.actions = actions;
1550 MIM.Rule.prototype = new Xex.Term ('rule');
1551 MIM.Rule.prototype.Parser = function (domain, node)
1554 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1556 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1557 var keyseq = Xex.Term.Parse (domain, n);
1558 if (keyseq.type != 'keyseq')
1559 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1560 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1561 return new MIM.Rule (keyseq, actions);
1563 MIM.Rule.prototype.toString = function ()
1568 MIM.Map = function (name)
1571 this.rules = new Array ();
1575 var proto = new Xex.Term ('map');
1577 proto.Parser = function (domain, node)
1579 var name = node.attributes['mname'].nodeValue;
1581 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1582 var map = new MIM.Map (name);
1583 for (var n = node.firstChild; n; n = n.nextSibling)
1584 if (n.nodeType == 1)
1585 map.rules.push (Xex.Term.Parse (domain, n));
1589 proto.toString = function ()
1591 var str = '<map mname="' + this.name + '">';
1592 var len = this.rules.length;
1593 for (i = 0; i < len; i++)
1594 str += this.rules[i];
1595 return str + '</map>';
1598 MIM.Map.prototype = proto;
1601 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1603 MIM.Action = function (domain, terms)
1605 var args = new Array ();
1606 args.push (Xex.CatchTag_.mimtag);
1607 for (var i = 0; i < terms.length; i++)
1608 args.push (terms[i]);
1609 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1612 MIM.Action.prototype.Run = function (domain)
1614 var result = this.action.Eval (domain);
1615 if (result.type == 'error')
1617 domain.context.Error = result.toString ();
1620 return (result != Xex.CatchTag._mimtag);
1623 MIM.Keymap = function ()
1626 this.submaps = null;
1627 this.actions = null;
1633 function add_rule (keymap, rule)
1635 var keyseq = rule.keyseq;
1636 var len = keyseq.val.length;
1639 for (var i = 0; i < len; i++)
1641 var key = keyseq.val[i];
1645 if (! keymap.submaps)
1646 keymap.submaps = {};
1648 sub = keymap.submaps[key.key];
1650 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1654 keymap.actions = rule.actions;
1657 proto.Add = function (map)
1659 var rules = map.rules;
1660 var len = rules.length;
1662 for (var i = 0; i < len; i++)
1663 add_rule (this, rules[i]);
1665 proto.Lookup = function (keys, index)
1669 if (index < keys.val.length && this.submaps
1670 && (sub = this.submaps[keys.val[index].key]))
1673 return sub.Lookup (keys, index);
1675 return { map: this, index: index };
1678 MIM.Keymap.prototype = proto;
1681 MIM.State = function (name)
1684 this.keymap = new MIM.Keymap ();
1688 var proto = new Xex.Term ('state');
1690 proto.Parser = function (domain, node)
1692 var map_list = domain.map_list;
1693 var name = node.attributes['sname'].nodeValue;
1695 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1696 var state = new MIM.State (name);
1697 for (node = node.firstElement (); node; node = node.nextElement ())
1699 if (node.nodeName == 'title')
1700 state.title = node.firstChild.nodeValue;
1703 var n = node.firstElement ();
1704 if (node.nodeName == 'branch')
1706 state.keymap.Add (map_list[node.attributes['mname'].nodeValue]);
1707 state.keymap.actions = Xex.Term.Parse (domain, n, null);
1709 else if (node.nodeName == 'state-hook')
1710 state.enter_actions = Xex.Term.Parse (domain, n, null);
1711 else if (node.nodeName == 'catch-all-branch')
1712 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1718 proto.toString = function ()
1720 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1723 MIM.State.prototype = proto;
1726 MIM.im_domain = new Xex.Domain ('input-method', null, null);
1727 MIM.im_domain.DefType (MIM.KeySeq.prototype);
1728 MIM.im_domain.DefType (MIM.Marker.prototype);
1729 MIM.im_domain.DefType (MIM.Selector.prototype);
1730 MIM.im_domain.DefType (MIM.Rule.prototype);
1731 MIM.im_domain.DefType (MIM.Map.prototype);
1732 MIM.im_domain.DefType (MIM.State.prototype);
1735 var im_domain = MIM.im_domain;
1737 function Finsert (domain, vari, args)
1740 if (args[0].type == 'integer')
1741 text = String.fromCharCode (args[0].val);
1744 domain.context.insert (text, null);
1747 function Finsert_candidates (domain, vari, args)
1749 var ic = domain.context;
1750 var candidates = new MIM.Candidates (args, column);
1751 ic.insert (candidates.Current (), candidates);
1755 function Fdelete (domain, vari, args)
1757 var ic = domain.context;
1758 var pos = args[0].IsInt ? args[0].Intval : args[0].Position (ic);
1760 return new Xex.Term (ic.del (pos));
1763 function Fselect (domain, vari, args)
1765 var ic = domain.context;
1766 var can = ic.candidates;
1770 var candidate = can.Current ();
1772 ic.del (ic.cursor_pos - candidate.length);
1773 candidate = can.Select (args[0]);
1774 ic.insert (candidate, can);
1779 function Fchar_at (domain, vari, args)
1781 return new Xex.Term (args[0].CharAt (domain.context));
1784 function Fmove (domain, vari, args)
1786 var ic = domain.context;
1787 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
1792 function Fmark (domain, vari, args)
1794 args[0].Mark (domain.context);
1798 function Fpushback (domain, vari, args)
1800 var arg = (args[0].IsInt ? args[0].Intval
1801 : args[0].IsStr ? new KeySeq (args[0])
1803 domain.context.pushback (arg)
1807 function Fundo (domain, vari, args)
1809 var ic = domain.context;
1810 var n = args.length == 0 ? -2 : args[0].val;
1812 ic.keys.val.splice (ic.keys.length + n, -n);
1814 ic.keys.val.splice (n, ic.keys.length);
1819 function Fcommit (domain, vari, args)
1821 domain.context.commit ();
1825 function Funhandle (domain, vari, args)
1827 domain.context.commit ();
1828 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
1831 function Fshift (domain, vari, args)
1833 var ic = domain.context;
1834 var state_name = args[0].val;
1835 var state = ic.im.state_list[state_name];
1837 throw ("Unknown state: " + state_name);
1842 function Fsurrounding_flag (domain, vari, args)
1844 return new Xex.IntTerm (-1);
1847 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1848 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
1849 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1850 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1851 //im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
1852 //im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
1853 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1854 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1855 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1856 //im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1857 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1858 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1859 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1860 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1861 //im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
1862 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1863 //im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
1864 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
1869 function get_global_var (vname)
1871 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
1872 MIM.im_global.Load ()
1873 return MIM.im_global.domain.variables[vname];
1876 function include (node)
1878 node = node.firstElement ();
1879 if (node.nodeName != 'tags')
1882 var lang = null, name = null, extra = null;
1883 for (node = node.firstElement (); node; node = node.nextElement ())
1885 if (node.nodeName == 'language')
1886 lang = node.firstChild.nodeValue;
1887 else if (node.nodeName == 'name')
1888 name = node.firstChild.nodeValue;
1889 else if (node.nodeName == 'extra-id')
1890 extra = node.firstChild.nodeValue;
1892 if (! lang || ! MIM.imlist[lang])
1896 if (! name || ! (im = MIM.imlist[lang][name]))
1901 if (! (im = MIM.imextra[lang][extra]))
1904 if (im.load_status != MIM.LoadStatus.Loaded
1905 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
1912 parsers['description'] = function (node)
1914 this.description = node.firstChild.nodeValue;
1916 parsers['variable-list'] = function (node)
1918 for (node = node.firstElement (); node; node = node.nextElement ())
1920 var vname = node.attributes['vname'].nodeValue;
1921 if (this != MIM.im_global)
1923 var vari = get_global_var (vname);
1925 this.domain.Defvar (vname);
1927 vname = Xex.Term.Parse (this.domain, node)
1928 MIM.log ('var:' + this.domain.variables[vname.val].name);
1931 parsers['command-list'] = function (node)
1934 parsers['macro-list'] = function (node)
1936 for (var n = node.firstElement (); n; n = n.nextElement ())
1937 if (n.nodeName == 'xi:include')
1939 var im = include (n);
1941 alert ('inclusion fail');
1943 for (var macro in im.domain.functions)
1945 var func = im.domain.functions[macro];
1946 if (func instanceof Xex.Macro)
1947 im.domain.CopyFunc (this.domain, macro);
1949 n = n.previousSibling;
1950 node.removeChild (n.nextSibling);
1952 Xex.Term.Parse (this.domain, node.firstElement (), null);
1954 parsers['title'] = function (node)
1956 this.title = node.firstChild.nodeValue;
1958 parsers['map-list'] = function (node)
1960 for (node = node.firstElement (); node; node = node.nextElement ())
1962 if (node.nodeName == 'xi:include')
1964 var im = include (node);
1967 alert ('inclusion fail');
1970 for (var mapname in im.map_list)
1971 this.map_list[mapname] = im.map_list[mapname];
1975 var map = Xex.Term.Parse (this.domain, node);
1976 this.map_list[map.name] = map;
1980 parsers['state-list'] = function (node)
1982 this.domain.map_list = this.map_list;
1983 for (node = node.firstElement (); node; node = node.nextElement ())
1985 if (node.nodeName == 'state')
1987 var state = Xex.Term.Parse (this.domain, node);
1989 state.title = this.title;
1990 if (! this.initial_state)
1991 this.initial_state = state;
1992 this.state_list[state.name] = state;
1995 delete this.domain.map_list;
1998 MIM.IM = function (lang, name, extra_id, file)
2002 this.extra_id = extra_id;
2004 this.load_status = MIM.LoadStatus.NotLoaded;
2005 this.domain = new Xex.Domain (this.lang + '-'
2006 + (this.name != 'nil'
2007 ? this.name : this.extra_id),
2008 MIM.im_domain, null);
2014 var node = Xex.Load (null, this.file);
2017 this.load_status = MIM.LoadStatus.Error;
2021 this.initial_state = null;
2022 this.state_list = {};
2023 for (node = node.firstElement (); node; node = node.nextElement ())
2025 var name = node.nodeName;
2026 var parser = parsers[name];
2028 parser.call (this, node);
2030 this.load_status = MIM.LoadStatus.Loaded;
2035 MIM.IM.prototype = proto;
2037 MIM.IC = function (im, target)
2039 if (im.load_status == MIM.LoadStatus.NotLoaded)
2041 if (im.load_status != MIM.LoadStatus.Loaded)
2042 alert ('im:' + im.name + ' error:' + im.load_status);
2044 this.target = target;
2045 this.domain = new Xex.Domain ('context', im.domain, this);
2051 MIM.CandidateTable = function ()
2053 this.table = new Array ();
2056 MIM.CandidateTable.prototype.get = function (from)
2058 for (var i = 0; i < this.table.length; i++)
2060 var elt = this.table[i];
2061 if (elt.from <= from && elt.to > from)
2066 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2068 for (var i = 0; i < this.table.length; i++)
2070 var elt = this.table[i];
2071 if (elt.from >= from && elt.from < to
2072 || elt.to >= from && elt.to < to)
2076 elt.val = candidates;
2080 this.table.push ({ from: from, to: to, val: candidates });
2083 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2085 var diff = inserted - (to - from);
2086 for (var i = 0; i < this.table.length; i++)
2088 var elt = this.table[i];
2097 MIM.CandidateTable.prototype.clear = function ()
2099 this.table.length = 0;
2102 function Block (index, term)
2106 this.Data = term.val;
2107 else if (term.IsList)
2109 this.Data = new Array ();
2110 for (var i = 0; i < term.val.length; i++)
2111 this.Data.push (term.val[i].val);
2115 Block.prototype.Count = function () { return this.Data.length; }
2116 Block.prototype.get = function (i)
2118 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
2121 function fill_group (start)
2123 var nitems = this.group.length;
2125 var b = this.blocks[r];
2127 if (start < b.Index)
2128 while (start < b.Index)
2129 b = this.blocks[--r];
2131 while (start >= b.Index + b.Count ())
2132 b = this.blocks[++r];
2135 var count = b.Count ();
2137 for (var i = 0; i < nitems; i++, start++)
2142 if (r == this.blocks.Length)
2148 this.group[i] = b.get (start);
2153 function Candidates (candidates, column)
2155 this.column = column;
2159 this.blocks = new Array ();
2161 for (var i = 0; i < candidates.length; i++)
2163 var block = new Block (this.total, candidates[i]);
2164 this.blocks.push (block);
2165 this.total += block.Count ();
2169 Candidates.prototype.Column = function ()
2171 return (this.column > 0 ? this.index % this.column
2172 : this.index - this.blocks[this.row].Index);
2175 Candidates.prototype.GroupLength = function ()
2177 if (this.column > 0)
2179 var start = this.index - (this.index % this.column);
2180 return (start + this.column <= this.total ? this.column
2181 : this.total - start);
2183 return this.blocks[this.row].Count;
2186 Candidates.prototype.Current = function ()
2188 var b = this.blocks[this.row];
2189 return b.get (this.index - b.Index);
2192 Candidates.prototype.PrevGroup = function ()
2194 var col = this.Column ();
2196 if (this.column > 0)
2198 this.index -= this.column;
2199 if (this.index >= 0)
2200 nitems = this.column;
2203 var lastcol = (this.total - 1) % this.column;
2204 this.index = (col < lastcol ? this.total - lastcol + col
2206 this.row = this.blocks.length - 1;
2207 nitems = lastcol + 1;
2209 while (this.blocks[this.row].Index > this.index)
2214 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
2215 nitems = this.blocks[this.row].Count ();
2216 this.index = (this.blocks[this.row].Index
2217 + (col < nitems ? col : nitems - 1));
2222 Candidates.prototype.NextGroup = function ()
2224 var col = this.Column ();
2226 if (this.column > 0)
2228 this.index += this.column - col;
2229 if (this.index < this.total)
2231 if (this.index + col >= this.total)
2233 nitems = this.total - this.index;
2234 this.index = this.total - 1;
2238 nitems = this.column;
2247 while (this.blocks[this.row].Index > this.index)
2252 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
2253 nitems = this.blocks[this.row].Count ();
2254 this.index = (this.blocks[this.row].Index
2255 + (col < nitems ? col : nitems - 1));
2260 Candidates.prototype.Prev = function ()
2262 if (this.index == 0)
2264 this.index = this.total - 1;
2265 this.row = this.blocks.length - 1;
2270 if (this.blocks[this.row].Index > this.index)
2275 Candidates.prototype.Next = function ()
2278 if (this.index == this.total)
2285 var b = this.blocks[this.row];
2286 if (this.index == b.Index + b.Count ())
2291 Candidates.prototype.First = function ()
2293 this.index -= this.Column ();
2294 while (this.blocks[this.row].Index > this.index)
2298 Candidates.prototype.Last = function ()
2300 var b = this.blocks[this.row];
2301 if (this.column > 0)
2303 if (this.index + 1 < this.total)
2305 this.index += this.column - this.Column () + 1;
2306 while (b.Index + b.Count () <= this.index)
2307 b = this.blocks[++this.row];
2311 this.index = b.Index + b.Count () - 1;
2314 Candidates.prototype.Select = function (selector)
2316 if (selector.type == 'selector')
2318 switch (selector.val)
2320 case '@<': this.First (); break;
2321 case '@>': this.Last (); break;
2322 case '@-': this.Prev (); break;
2323 case '@+': this.Next (); break;
2324 case '@[': this.PrevGroup (); break;
2325 case '@]': this.NextGroup (); break;
2328 return this.Current ();
2331 if (this.column > 0)
2333 col = this.index % this.column;
2334 start = this.index - col;
2335 end = start + this.column;
2339 start = this.blocks[this.row].Index;
2340 col = this.index - start;
2341 end = start + this.blocks[this.row].Count;
2343 if (end > this.total)
2345 this.index += selector.val - col;
2346 if (this.index >= end)
2347 this.index = end - 1;
2348 if (this.column > 0)
2350 if (selector.val > col)
2351 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2355 while (this.blocks[this.row].Index > this.index)
2358 return this.Current ();
2361 function detach_candidates (ic)
2363 ic.candidate_table.clear ();
2364 ic.candidates = null;
2365 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2366 | ChangedStatus.CandidateList
2367 | ChangedStatus.CandidateIndex
2368 | ChangedStatus.CandidateShow);
2371 function set_cursor (prefix, pos)
2373 this.cursor_pos = pos;
2375 this.candidates = this.candidate_table.get (pos - 1);
2377 this.candidates = null;
2380 function save_state ()
2382 this.state_var_values = this.domain.SaveValues ();
2383 this.state_preedit = this.preedit;
2384 this.state_key_head = this.key_head;
2385 this.state_pos = this.cursor_pos;
2388 function restore_state ()
2390 this.domain.RestoreValues (this.state_var_values);
2391 this.preedit = this.state_preedit;
2392 set_cursor.call (this, "restore", this.state_pos);
2395 function handle_key ()
2397 var out = this.keymap.Lookup (this.keys, this.key_head);
2399 var branch_actions = this.state.keymap.actions;
2401 MIM.log ('handling ' + this.keys.val[this.key_head]
2402 + ' in ' + this.state.name + ':' + this.keymap.name);
2403 this.key_head = out.index;
2404 if (sub != this.keymap)
2407 restore_state.call (this);
2409 MIM.log ('submap found');
2410 if (this.keymap.actions)
2412 MIM.log ('taking map actions:');
2413 if (! this.take_actions (this.keymap.actions))
2416 else if (this.keymap.submaps)
2418 MIM.log ('no map actions');
2419 for (var i = this.state_key_head; i < this.key_head; i++)
2421 MIM.log ('inserting key:' + this.keys.val[i].key);
2422 this.insert (this.keys.val[i].key, null);
2425 if (! this.keymap.submaps)
2427 MIM.log ('terminal:');
2428 if (this.keymap.branch_actions != null)
2430 MIM.log ('branch actions:');
2431 if (! this.take_actions (branch_actions))
2434 if (this.keymap != this.state.keymap)
2435 this.shift (this.state);
2440 MIM.log ('no submap');
2441 var current_state = this.state;
2442 var map = this.keymap;
2446 MIM.log ('branch actions');
2447 if (! this.take_actions (this.keymap.branch_actions))
2451 if (map == this.keymap)
2453 MIM.log ('no state change');
2454 if (map == this.initial_state.keymap
2455 && this.key_head < this.keys.val.length)
2457 MIM.log ('unhandled');
2460 if (this.keymap != current_state.keymap)
2461 this.shift (current_state);
2462 else if (this.keymap.actions == null)
2463 this.shift (this.initial_state);
2472 this.produced = null;
2474 this.preedit_saved = '';
2475 this.cursor_pos = 0;
2476 this.marker_positions = {};
2477 this.candidates = null;
2478 this.candidate_show = false;
2480 this.prev_state = null;
2481 this.initial_state = this.im.initial_state;
2482 this.title = this.initial_state.title;
2483 this.state_preedit = '';
2484 this.state_key_head = 0;
2485 this.state_var_values = {};
2488 this.keys = new MIM.KeySeq ();
2490 this.key_unhandled = false;
2491 this.unhandled_key = null;
2492 this.changed = MIM.ChangedStatus.None;
2493 this.error_message = '';
2494 this.title = this.initial_state.title;
2497 this.preedit_saved = '';
2498 this.marker_positions = {};
2499 this.candidate_table = new MIM.CandidateTable ();
2500 this.candidates = null;
2501 this.candidate_show = false;
2502 this.shift (this.initial_state);
2505 catch_args: new Array (Xex.CatchTag._mimtag, null),
2507 take_actions: function (actions)
2509 var func_progn = this.domain.GetFunc ('progn');
2510 var func_catch = this.domain.GetFunc ('catch');
2511 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2512 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2513 term = term.Eval (this.domain);
2514 return (! term.IsSymbol || term.val != '@mimtag');
2517 GetSurroundingChar: function (pos)
2519 if (pos < 0 ? this.caret_pos < - pos : this.target.value.length < pos)
2521 return this.target.value.charCodeAt (this.caret_pos + pos);
2524 adjust_markers: function (from, to, inserted)
2526 var diff = inserted - (to - from);
2528 for (var m in this.marker_positions)
2529 if (this.marker_positions[m] > from)
2530 this.marker_positions[m] = (this.marker_positions[m] >= to
2531 ? pos + diff : from);
2532 if (this.cursor_pos >= to)
2533 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2534 else if (this.cursor_pos > from)
2535 set_cursor.call (this, 'adjust', from)
2538 preedit_replace: function (from, to, text, candidates)
2540 this.preedit = (this.preedit.substring (0, from)
2541 + text + this.preedit.substring (to));
2542 this.adjust_markers (from, to, text.length);
2543 this.candidate_table.adjust (from, to, text.length);
2545 this.candidate_table.put (from, from + text.length, candidates)
2548 insert: function (text, candidates)
2550 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2551 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2556 var deleted = pos - this.cursor_pos;
2559 this.DelSurroundText (pos);
2562 else if (pos > this.preedit.length)
2564 this.DelSurroundText (pos - this.preedit.length);
2565 pos = this.preedit.length;
2567 if (pos < this.cursor_pos)
2568 this.preedit = (this.predit.substring (0, pos)
2569 + this.preedit.substring (this.cursor_pos));
2571 this.preedit = (this.preedit.substring (0, this.cursor_pos)
2572 + this.predit.substring (pos));
2578 this.candidate_show = true;
2579 this.changed |= MIM.ChangedStatus.CandidateShow;
2584 this.candidate_show = false;
2585 this.changed |= MIM.ChangedStatus.CandidateShow;
2588 move: function (pos)
2592 else if (pos > this.preedit.length)
2593 pos = this.preedit.length;
2594 if (pos != this.cursor_pos)
2596 set_cursor.call (this, 'move', pos);
2597 this.changed |= MIM.ChangedStatus.Preedit;
2601 pushback: function (n)
2603 if (n instanceof MIM.KeySeq)
2605 if (this.key_head > 0)
2607 if (this.key_head < this.keys.val.length)
2608 this.keys.val.splice (this.key_head,
2609 this.keys.val.length - this.key_head);
2610 for (var i = 0; i < n.val.length; i++)
2611 this.keys.val.push (n.val[i]);
2617 if (this.key_head < 0)
2624 this.key_head = - n;
2625 if (this.key_head > this.keys.val.length)
2626 this.key_head = this.keys.val.length;
2632 if (this.key_head < this.keys.val.length)
2633 this.keys.val.splice (this.key_head, 1);
2638 if (this.preedit.length > 0)
2640 this.candidate_table.clear ();
2641 this.produced += this.preedit;
2642 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2646 shift: function (state)
2650 MIM.log ("shifting back to previous");
2651 if (this.prev_state == null)
2653 state = this.prev_state;
2656 MIM.log ("shifting to " + state.name);
2658 if (state == this.initial_state)
2663 this.keys.val.splice (0, this.key_head);
2665 this.prev_state = null;
2670 if (state != this.state)
2671 this.prev_state = this.state;
2673 if (state != this.state && state.enter_actions)
2674 this.take_actions (state.enter_actions);
2675 if (! this.state || this.state.title != state.title)
2676 this.changed |= MIM.ChangedStatus.StateTitle;
2678 this.keymap = state.keymap;
2679 this.state_key_head = this.key_head;
2680 save_state.call (this);
2683 Filter: function (key)
2687 this.key_unhandled = true;
2688 this.unhandled_key = key;
2691 if (key.key == '_reload')
2693 this.changed = MIM.ChangedStatus.None;
2695 this.key_unhandled = false;
2696 this.keys.val.push (key);
2698 while (this.key_head < this.keys.val.length)
2700 if (! handle_key.call (this))
2702 if (this.key_head < this.keys.val.length)
2704 this.unhandled_key = this.keys.val[this.key_head];
2705 this.keys.val.splice (this.key_head, this.key_head + 1);
2707 this.key_unhandled = true;
2713 this.key_unhandled = true;
2717 if (this.key_unhandled)
2719 this.keys.val.length = 0;
2720 this.key_head = this.state_key_head = this.commit_key_head = 0;
2722 return (! this.key_unhandled
2723 && this.produced.length == 0
2724 && this.preedit.length == 0);
2728 MIM.IC.prototype = proto;
2730 var node = Xex.Load (null, "imlist.xml");
2731 for (node = node.firstChild; node; node = node.nextSibling)
2732 if (node.nodeName == 'input-method')
2734 var lang = null, name = null, extra_id = null, file = null;
2736 for (var n = node.firstChild; n; n = n.nextSibling)
2738 if (n.nodeName == 'language')
2739 lang = n.firstChild.nodeValue;
2740 else if (n.nodeName == 'name')
2741 name = n.firstChild.nodeValue;
2742 else if (n.nodeName == 'extra-id')
2743 extra_id = n.firstChild.nodeValue;
2744 else if (n.nodeName == 'filename')
2745 file = n.firstChild.nodeValue;
2747 if (name && name != 'nil')
2749 if (! MIM.imlist[lang])
2750 MIM.imlist[lang] = {};
2751 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2753 else if (extra_id && extra_id != 'nil')
2755 if (! MIM.imextra[lang])
2756 MIM.imextra[lang] = {};
2757 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2760 if (MIM.imextra.t && MIM.imextra.t.global)
2761 MIM.im_global = MIM.imextra.t.global;
2764 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2765 MIM.im_global.load_status = MIM.LoadStatus.Error;
2771 var keys = new Array ();
2773 keys[0x08] = 'backspace';
2774 keys[0x0D] = 'return';
2775 keys[0x1B] = 'escape';
2776 keys[0x20] = 'space';
2777 keys[0x21] = 'pageup';
2778 keys[0x22] = 'pagedown';
2780 keys[0x24] = 'home';
2781 keys[0x25] = 'left';
2783 keys[0x27] = 'right';
2784 keys[0x28] = 'down';
2785 keys[0x2D] = 'insert';
2786 keys[0x2E] = 'delete';
2787 for (var i = 1; i <= 12; i++)
2788 keys[111 + i] = "f" + i;
2789 keys[0x90] = "numlock";
2790 keys[0xF0] = "capslock";
2792 MIM.decode_key_event = function (event)
2794 var key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
2795 : event.charCode ? event.charCode
2799 if (event.type == 'keydown')
2804 if (event.shiftKey) key = "S-" + key ;
2807 key = String.fromCharCode (key);
2808 if (event.altKey) key = "A-" + key ;
2809 if (event.ctrlKey) key = "C-" + key ;
2810 return new MIM.Key (key);
2814 MIM.add_event_listener
2815 = (window.addEventListener
2816 ? function (target, type, listener) {
2817 target.addEventListener (type, listener, false);
2819 : window.attachEvent
2820 ? function (target, type, listener) {
2821 target.attachEvent ('on' + type,
2823 listener.call (target, window.event);
2826 : function (target, type, listener) {
2828 = function (e) { listener.call (target, e || window.event); };
2831 MIM.log = function (msg)
2833 var node = document.getElementById ('log');
2834 node.value = msg + "\n" + node.value;
2837 MIM.debug_print = function (event, ic)
2841 if (! MIM.debug_nodes)
2843 MIM.debug_nodes = new Array ();
2844 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
2845 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
2846 MIM.debug_nodes['status0'] = document.getElementById ('status0');
2847 MIM.debug_nodes['status1'] = document.getElementById ('status1');
2848 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
2849 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
2850 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
2851 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
2853 var target = event.target;
2854 var code = event.keyCode;
2855 var ch = event.type == 'keydown' ? 0 : event.charCode;
2856 var key = MIM.decode_key_event (event);
2859 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + " : " + key;
2860 index = (event.type == 'keydown' ? '0' : '1');
2862 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
2864 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
2865 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
2866 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
2869 MIM.get_range = function (target, range)
2871 if (target.selectionStart != null) // for Mozilla
2873 range[0] = target.selectionStart;
2874 range[1] = target.selectionEnd;
2878 var r = document.selection.createRange ();
2879 var rr = r.duplicate ();
2881 rr.moveToElementText (target);
2882 rr.setEndPoint ('EndToEnd', range);
2883 range[0] = rr.text.length - r.text.length;
2884 range[1] = rr.text.length;
2888 MIM.set_caret = function (target, ic)
2890 if (target.setSelectionRange) // Mozilla
2892 var scrollTop = target.scrollTop;
2893 target.setSelectionRange (ic.spot, ic.spot + ic.preedit.length);
2894 target.scrollTop = scrollTop;
2898 var range = target.createTextRange ();
2899 range.moveStart ('character', ic.spot);
2900 range.moveEnd ('character', ic.spot + ic.preedit.length);
2906 var range = new Array ();
2908 MIM.check_range = function (target, ic)
2910 MIM.get_range (target, range);
2911 if (range[0] != ic.spot || range[1] - range[0] != ic.preedit.length
2912 || target.value.substring (range[0], range[1]) != ic.preedit)
2914 MIM.log ('reset:' + ic.spot + '-' + (ic.spot + ic.preedit.length)
2915 + '/' + range[0] + '-' + range[1]);
2918 target.value = (target.value.substring (0, range[0])
2919 + target.value.substring (range[1]));
2924 MIM.update = function (target, ic)
2926 var text = target.value;
2927 target.value = (text.substring (0, ic.spot)
2930 + text.substring (ic.spot));
2931 ic.spot += ic.produced.length;
2932 MIM.set_caret (target, ic);
2935 MIM.reset_ic = function (event)
2937 if (event.target.mim_ic)
2939 var ic = event.target.mim_ic;
2940 var pos = ic.spot + ic.preedit.length;
2943 event.target.setSelectionRange (pos, pos);
2947 MIM.keydown = function (event)
2949 var target = event.target;
2950 if (! (target.type == "text" || target.type == "textarea"))
2953 var ic = target.mim_ic;
2954 if (! ic || ic.im != MIM.current)
2956 MIM.log ('creating IC');
2957 ic = new MIM.IC (MIM.current, target);
2959 MIM.add_event_listener (target, 'blur', MIM.reset_ic);
2961 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2963 MIM.check_range (target, ic);
2964 MIM.debug_print (event, ic);
2965 ic.key = MIM.decode_key_event (event);
2968 MIM.keypress = function (event)
2970 if (! (event.target.type == "text" || event.target.type == "textarea"))
2973 var ic = event.target.mim_ic;
2977 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2980 ic.key = MIM.decode_key_event (event);
2987 MIM.log ("filtering " + ic.key);
2988 var result = ic.Filter (ic.key);
2989 MIM.update (event.target, ic);
2990 if (! ic.key_unhandled)
2991 event.preventDefault ();
2993 MIM.debug_print (event, ic);
2998 MIM.select_im = function (event)
3000 var target = event.target.parentNode;
3001 while (target.tagName != "SELECT")
3002 target = target.parentNode;
3005 for (var lang in MIM.imlist)
3006 for (var name in MIM.imlist[lang])
3007 if (idx++ == target.selectedIndex)
3009 im = MIM.imlist[lang][name];
3012 document.getElementsByTagName ('body')[0].removeChild (target);
3013 target.target.focus ();
3014 if (im && im != MIM.current)
3017 MIM.log ('select IM: ' + im.name);
3021 MIM.destroy_menu = function (event)
3023 if (event.target.tagName == "SELECT")
3024 document.getElementsByTagName ('body')[0].removeChild (event.target);
3027 MIM.select_menu = function (event)
3029 var target = event.target;
3031 if (! ((target.type == "text" || target.type == "textarea")
3032 && event.which == 1 && event.ctrlKey))
3035 var sel = document.createElement ('select');
3036 sel.onclick = MIM.select_im;
3037 sel.onmouseout = MIM.destroy_menu;
3038 sel.style.position='absolute';
3039 sel.style.left = (event.clientX - 10) + "px";
3040 sel.style.top = (event.clientY - 10) + "px";
3041 sel.target = target;
3043 for (var lang in MIM.imlist)
3044 for (var name in MIM.imlist[lang])
3046 var option = document.createElement ('option');
3047 var imname = lang + "-" + name;
3048 option.appendChild (document.createTextNode (imname));
3049 option.value = imname;
3050 sel.appendChild (option);
3051 if (MIM.imlist[lang][name] == MIM.current)
3052 sel.selectedIndex = idx;
3056 document.getElementsByTagName ('body')[0].appendChild (sel);
3059 MIM.test = function ()
3061 var im = MIM.imlist['t']['latn-post'];
3062 var ic = new MIM.IC (im, null);
3064 ic.Filter (new MIM.Key ('a'));
3065 ic.Filter (new MIM.Key ("'"));
3068 document.getElementById ('text').value = ic.produced + ic.preedit;
3071 document.getElementById ('text').value
3072 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3074 if (e instanceof Xex.ErrTerm)
3082 MIM.init = function ()
3084 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3085 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3086 MIM.add_event_listener (window, 'mousedown', MIM.select_menu);
3087 if (window.location == 'http://localhost/mim/index.html')
3088 MIM.server = 'http://localhost/mim';
3089 MIM.current = MIM.imlist['vi']['telex'];
3092 MIM.init_debug = function ()