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 (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 MIM.log ('defmacro:' + name);
574 parse_defun_body (domain, node);
575 return new Xex.StrTerm (name);
577 if (name == 'defvar')
579 name = parse_defvar (domain, node);
580 MIM.log ('defvar:' + name);
581 return new Xex.StrTerm (name);
583 return new Xex.Funcall.prototype.Parser (domain, node);
585 for (var n = node; n && n != stop; n = n.nextElement ())
587 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
589 var name = parse_defun_head (domain, n);
590 MIM.log ('defmacro:' + name);
594 for (var n = node; n && n != stop; n = n.nextElement ())
596 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
597 parse_defun_body (domain, n);
598 else if (n.nodeName == 'defvar')
599 parse_defvar (domain, n);
603 terms = new Array ();
604 terms.push (Xex.Term.Parse (domain, n));
611 Xex.Varref = function (vname)
617 var proto = new Xex.Term ('varref');
619 proto.Clone = function () { return new Xex.Varref (this.val); }
620 proto.Eval = function (domain)
622 if (! this.vari || this.vari.domain != domain)
623 this.vari = domain.GetVarCreate (this.val);
624 return this.vari.val;
627 proto.Parser = function (domain, node)
629 return new Xex.Varref (node.attributes['vname'].nodeValue);
632 Xex.Varref.prototype = proto;
635 var null_args = new Array ();
637 Xex.Funcall = function (func, vari, args)
641 this.args = args || null_args;
645 var proto = new Xex.Term ('funcall');
647 proto.Parser = function (domain, node)
649 var fname = node.nodeName;
652 if (fname == 'funcall')
653 fname = node.attributes['fname'].nodeValue;
654 var func = domain.GetFunc (fname);
656 attr = node.attributes['vname'];
657 vari = attr != undefined ? domain.GetVarCreate (attr.nodeValue) : false;
658 var args = Xex.Term.Parse (domain, node.firstElement (), null);
659 return new Xex.Funcall (func, vari, args);
662 proto.New = function (domain, fname, vname, args)
664 var func = domain.GetFunc (fname);
665 var vari = vname ? domain.GetVarCreate (vname) : null;
666 var funcall = new Xex.Funcall (func, vari, args);
667 if (func instanceof Xex.Macro)
668 funcall = funcall.Eval (domain);
672 proto.Eval = function (domain)
674 return this.func.Call (domain, this.vari, this.args);
677 proto.Clone = function ()
679 return new Xex.Funcall (this.func, this.vari, this.args);
682 proto.Equals = function (obj)
684 return (obj.type == 'funcall'
685 && obj.func == this.func
686 && obj.vari.Equals (this.vari)
687 && obj.args.length == this.func.length);
690 proto.toString = function ()
693 var len = this.args.length;
695 return '<' + this.func.name + '/>';
696 for (var i = 0; i < len; i++)
697 arglist += this.args[i].toString ();
698 return '<' + this.func.name + '>' + arglist + '</' + this.func.name + '>';
701 Xex.Funcall.prototype = proto;
704 Xex.ErrTerm = function (ename, message, stack)
707 this.message = message;
712 var proto = new Xex.Term ('error');
714 proto.IsError = true;
716 proto.Parser = function (domain, node)
718 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
719 node.innerText, false);
722 proto.CallStack = function () { return stack; }
724 proto.SetCallStack = function (value) { statck = value; }
726 proto.Clone = function ()
728 return new Xex.ErrTerm (ename, message, false);
731 proto.Equals = function (obj)
734 && obj.ename == ename && obj.message == message
735 && (obj.stack ? (stack && stack.length == obj.stack.length)
739 proto.Matches = function (obj)
741 return (obj.IsError && obj.ename == ename);
744 proto.toString = function ()
746 return '<error ename="' + this.ename + '">' + this.message + '</error>';
749 Xex.ErrTerm.prototype = proto;
752 Xex.IntTerm = function (num) { this.val = num; };
754 var proto = new Xex.Term ('integer');
756 proto.Intval = function () { return this.val; };
757 proto.IsTrue = function () { return this.val != 0; }
758 proto.Clone = function () { return new Xex.IntTerm (this.val); }
759 proto.Parser = function (domain, node)
761 var str = node.firstChild.nodeValue;
763 if (str.charAt (0) == '?' && str.length == 2)
764 return new Xex.IntTerm (str.charCodeAt (1));
765 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
767 Xex.IntTerm.prototype = proto;
770 Xex.StrTerm = function (str) { this.val = str; };
772 var proto = new Xex.Term ('string');
774 proto.Strval = function () { return this.val; };
775 proto.IsTrue = function () { return this.val.length > 0; }
776 proto.Clone = function () { return new Xex.StrTerm (this.val); }
777 proto.Parser = function (domain, node)
779 return new Xex.StrTerm (node.firstChild.nodeValue);
781 Xex.StrTerm.prototype = proto;
784 Xex.SymTerm = function (str) { this.val = str; };
786 var proto = new Xex.Term ('symbol');
787 proto.IsSymbol = true;
788 proto.IsTrue = function () { return this.val != 'nil'; }
789 proto.Clone = function () { return new Xex.SymTerm (this.val); }
790 proto.Parser = function (domain, node)
792 return new Xex.SymTerm (node.firstChild.nodeValue);
794 Xex.SymTerm.prototype = proto;
797 Xex.LstTerm = function (list) { this.val = list; };
799 var proto = new Xex.Term ('list');
801 proto.IsTrue = function () { return this.val.length > 0; }
802 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
804 proto.Equals = function (obj)
806 if (obj.type != 'list' || obj.val.length != this.val.length)
808 var i, len = this.val.length;
809 for (i = 0; i < len; i++)
810 if (! this.val[i].Equals (obj.val[i]))
815 proto.Parser = function (domain, node)
817 var list = Xex.Term.Parse (domain, node.firstElement (), null);
818 return new Xex.LstTerm (list);
821 proto.toString = function ()
823 var len = this.val.length;
828 for (var i = 0; i < len; i++)
829 str += this.val[i].toString ();
830 return str + '</list>';
832 Xex.LstTerm.prototype = proto;
836 var basic = new Xex.Domain ('basic', null, null);
838 function Fset (domain, vari, args)
840 return vari.SetValue (args[0]);
843 function maybe_set_intvar (vari, n)
845 var term = new Xex.IntTerm (n);
847 vari.SetValue (term);
851 function Fset (domain, vari, args)
854 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
855 'No variable name to set');
856 vari.SetValue (args[0]);
860 function maybe_set_intvar (vari, n)
862 var term = new IntTerm (n);
864 vari.SetValue (term);
868 function Fadd (domain, vari, args)
870 var n = vari ? vari.val.Intval () : 0;
871 var len = args.length;
873 for (var i = 0; i < len; i++)
874 n += args[i].Intval ();
875 return maybe_set_intvar (vari, n);
878 function Fmul (domain, vari, args)
880 var n = vari ? vari.val.Intval () : 1;
881 for (var i = 0; i < args.length; i++)
883 return maybe_set_intvar (vari, n);
886 function Fsub (domain, vari, args)
892 n = args[0].Intval ();
897 n = vari.val.Intval ();
900 while (i < args.length)
901 n -= args[i++].Intval ();
902 return maybe_set_intvar (vari, n);
905 function Fdiv (domain, vari, args)
911 n = args[0].Intval ();
916 n = vari.val.Intval ();
919 while (i < args.length)
920 n /= args[i++].Intval ();
921 return maybe_set_intvar (vari, n);
924 function Fmod (domain, vari, args)
926 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
929 function Flogior (domain, vari, args)
931 var n = vari == null ? 0 : vari.val;
932 for (var i = 0; i < args.length; i++)
934 return maybe_set_intvar (vari, n);
937 function Fand (domain, vari, args)
939 var len = args.length;
940 for (var i = 0; i < len; i++)
942 var result = args[i].Eval (domain);
943 if (domain.Thrown ())
945 if (! result.IsTrue ())
951 function For (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 Feq (domain, vari, args)
967 for (var i = 1; i < args.length; i++)
968 if (! args[i - 1].Equals (args[i]))
973 function Flt (domain, vari, args)
975 var n = args[0].Intval;
977 for (var i = 1; i < args.length; i++)
979 var n1 = args[i].Intval;
987 function Fle (domain, vari, args)
989 var n = args[0].Intval;
990 for (var i = 1; i < args.length; i++)
992 var n1 = args[i].Intval;
1000 function Fgt (domain, vari, args)
1002 var n = args[0].Intval;
1003 for (var i = 1; i < args.length; i++)
1005 var n1 = args[i].Intval;
1013 function Fge (domain, vari, args)
1015 var n = args[0].Intval;
1016 for (var i = 1; i < args.Length; i++)
1018 var n1 = args[i].Intval;
1026 function Fprogn (domain, vari, args)
1028 var result = Xex.One;
1029 var len = args.length;
1031 for (var i = 0; i < len; i++)
1033 result = args[i].Eval (domain);
1034 if (domain.Thrown ())
1040 function Fif (domain, vari, args)
1042 var result = args[0].Eval (domain);
1044 if (domain.Thrown ())
1046 if (result.IsTrue ())
1047 return args[1].Eval (domain);
1048 if (args.length == 2)
1050 return args[2].Eval (domain);
1053 function Fcond (domain, vari, args)
1055 for (var i = 0; i < args.length; i++)
1057 var list = args[i].val;
1058 var result = list.val[0].Eval (doamin);
1059 if (result.isTrue ())
1061 for (var j = 1; j < list.val.length; j++)
1064 result = list.val[j].Eval (domain);
1066 if (domain.Thrown ())
1075 function eval_terms (domain, terms, idx)
1077 var result = Xex.Zero;
1078 domain.caught = false;
1079 for (var i = idx; i < terms.length; i++)
1081 result = terms[i].Eval (domain);
1082 if (domain.Thrown ())
1088 function Fcatch (domain, vari, args)
1093 if (args[0].IsError)
1096 result = eval_terms (domain, args, 1);
1098 if (e instanceof Xex.ErrTerm)
1100 if (! args[0].Matches (e))
1108 else if (args[0].IsSymbol)
1111 domain.Catch (args[0].val);
1112 result = eval_terms (domain, args, 1);
1116 vari.SetValue (result);
1124 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1125 "Not a symbol nor an error: " + args[0]);
1128 function Fthrow (domain, vari, args)
1130 if (args[0].IsSymbl)
1132 domain.ThrowSymbol (args[0]);
1133 return (args[args.length - 1]);
1135 if (args[0].IsError)
1139 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1140 "Not a symbol nor an error:" + args[0]);
1143 Xex.BasicDomain = basic;
1145 basic.DefSubr (Fset, "set", true, 1, 1);
1146 if (basic.functions['='])
1147 alert (basic.functions['=']);
1148 basic.DefAlias ("=", "set");
1149 //basic.DefSubr (Fnot, "not", false, 1, 1);
1150 //basic.DefAlias ("!", "not");
1151 basic.DefSubr (Fadd, "add", true, 1, -1);
1152 basic.DefSubr (Fmul, "mul", true, 1, -1);
1153 basic.DefAlias ("*", "mul");
1154 basic.DefSubr (Fsub, "sub", true, 1, -1);
1155 basic.DefAlias ("-", "sub");
1156 basic.DefSubr (Fdiv, "div", true, 1, -1);
1157 basic.DefAlias ("/", "div");
1158 basic.DefSubr (Fmod, "mod", true, 1, 2);
1159 basic.DefAlias ("%", "mod");
1160 basic.DefSubr (Flogior, "logior", true, 1, -1);
1161 basic.DefAlias ('|', "logior");
1162 //basic.DefSubr (Flogand, "logand", true, 1, -1);
1163 //basic.DefAlias ("&", "logand");
1164 //basic.DefSubr (Flsh, "lsh", true, 1, 2);
1165 //basic.DefAlias ("<<", "lsh");
1166 //basic.DefSubr (Frsh, "rsh", true, 1, 2);
1167 //basic.DefAlias (">>", "rsh");
1168 basic.DefSubr (Feq, "eq", false, 2, -1);
1169 basic.DefAlias ("==", "eq");
1170 //basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1171 //basic.DefAlias ("!=", "noteq");
1172 basic.DefSubr (Flt, "lt", false, 2, -1);
1173 basic.DefAlias ("<", "lt");
1174 basic.DefSubr (Fle, "le", false, 2, -1);
1175 basic.DefAlias ("<=", "le");
1176 basic.DefSubr (Fgt, "gt", false, 2, -1);
1177 basic.DefAlias (">", "gt");
1178 basic.DefSubr (Fge, "ge", false, 2, -1);
1179 basic.DefAlias (">=", "ge");
1180 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1182 //basic.DefSubr (Fappend, "append", true, 0, -1);
1183 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1184 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1185 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1186 //basic.DefSubr (Fins, "ins", true, 2, 2);
1187 //basic.DefSubr (Fdel, "del", true, 2, 2);
1188 //basic.DefSubr (Feval, "eval", false, 1, 1);
1189 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1190 //basic.DefSubr (Freturn, "return", false, 0, 1);
1191 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1193 basic.DefSpecial (Fand, "and", false, 1, -1);
1194 basic.DefAlias ("&&", "and");
1195 basic.DefSpecial (For, "or", false, 1, -1);
1196 basic.DefAlias ("||", "or");
1197 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1198 basic.DefAlias ("expr", "progn");
1199 basic.DefSpecial (Fif, "if", false, 2, 3);
1200 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1201 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1202 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1203 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1204 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1205 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1206 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1207 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1209 basic.DefType (Xex.Funcall.prototype);
1210 basic.DefType (Xex.Varref.prototype);
1211 basic.DefType (Xex.ErrTerm.prototype);
1212 basic.DefType (Xex.IntTerm.prototype);
1213 basic.DefType (Xex.StrTerm.prototype);
1214 basic.DefType (Xex.SymTerm.prototype);
1215 basic.DefType (Xex.LstTerm.prototype);
1219 Xex.Zero = new Xex.IntTerm (0);
1220 Xex.One = new Xex.IntTerm (1);
1221 Xex.nil = new Xex.SymTerm ('nil');
1223 Xex.Load = function (server, file)
1225 var obj = new XMLHttpRequest ();
1226 var url = server ? server + '/' + file : file;
1227 obj.open ('GET', url, false);
1228 obj.overrideMimeType ('text/xml');
1230 return obj.responseXML.firstChild;
1234 // URL of the input method server.
1235 server: "http://www.m17n.org/common/mim-js",
1236 // Boolean flag to tell if MIM is active or not.
1238 // Boolean flag to tell if MIM is running in debug mode or not.
1240 // List of main input methods.
1242 // List of extra input methods;
1244 // Global input method data
1246 // Currently selected input method.
1250 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1257 CandidateIndex:0x10,
1259 Preedit: 0x06, // PreeditText | CursorPos
1260 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1282 ParseError: "parse-error"
1287 var keysyms = new Array ();
1288 keysyms["bs"] = "backspace";
1289 keysyms["lf"] = "linefeed";
1290 keysyms["cr"] = keysyms["enter"] = "return";
1291 keysyms["esc"] = "escape";
1292 keysyms["spc"] = "space";
1293 keysyms["del"] = "delete";
1295 function decode_keysym (str) {
1296 var parts = str.split ("-");
1297 var len = parts.length, i;
1298 var has_modifier = len > 1;
1300 for (i = 0; i < len - 1; i++)
1301 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1303 var key = parts[len - 1];
1306 key = keysyms[key.toLowerCase ()];
1312 for (i = 1; i < len - 1; i++)
1313 str += '-' + parts[i];
1322 parts = new Array ();
1329 MIM.Key = function (val)
1332 this.has_modifier = false;
1333 if (typeof val == 'string' || val instanceof String)
1335 this.key = decode_keysym (val);
1337 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1338 if (this.key instanceof Array)
1340 this.key = this.key[0];
1341 this.has_modifier = true;
1344 else if (typeof val == 'number' || val instanceof Number)
1345 this.key = String.fromCharCode (val);
1347 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1350 MIM.Key.prototype.toString = function () { return this.key; };
1354 MIM.KeySeq = function (seq)
1356 this.val = new Array ();
1357 this.has_modifier = false;
1363 var len = seq.val.length;
1364 for (var i = 0; i < len; i++)
1367 if (v.type != 'string' && v.type != 'integer'
1368 && v.type != 'symbol')
1369 throw new Xex.ErrTerm (MIM.Error.ParseError,
1370 "Invalid key: " + v);
1371 var key = new MIM.Key (v.val);
1372 this.val.push (key);
1373 if (key.has_modifier)
1374 this.has_modifier = true;
1379 var len = seq.val.length;
1380 for (var i = 0; i < len; i++)
1381 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1384 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1388 var proto = new Xex.Term ('keyseq');
1389 proto.Clone = function () { return this; }
1390 proto.Parser = function (domain, node)
1392 var seq = new Array ();
1393 for (node = node.firstChild; node; node = node.nextSibling)
1394 if (node.nodeType == 1)
1396 var term = Xex.Term.Parse (domain, node);
1397 return new MIM.KeySeq (term);
1399 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1401 proto.toString = function ()
1403 var len = this.val.length;
1407 var str = '<keyseq>';
1408 for (var i = 0; i < len; i++)
1412 else if (this.has_modifier)
1414 str += this.val[i].toString ();
1416 return str + '</keyseq>';
1419 MIM.KeySeq.prototype = proto;
1423 MIM.Marker = function () { }
1424 MIM.Marker.prototype = new Xex.Term ('marker');
1425 MIM.Marker.prototype.CharAt = function (ic)
1427 var p = this.Position (ic);
1429 return ic.GetSurroundingChar (p);
1430 else if (p >= ic.preedit.length)
1431 return ic.GetSurroundingChar (p - ic.preedit.length);
1432 return ic.preedit.charCodeAt (p);
1435 MIM.NamedMarker = function (name) { this.val = name; }
1436 MIM.NamedMarker.prototype = new MIM.Marker ();
1437 MIM.NamedMarker.prototype.Position = function (ic)
1439 var p = ic.marker_positions[this.val];
1440 return (p == undefined ? 0 : p);
1442 MIM.NamedMarker.prototype.Mark = function (ic)
1444 ic.marker_positions[this.val] = ic.cursor_pos;
1447 MIM.PredefinedMarker = function (name) { this.val = name; }
1448 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1449 MIM.PredefinedMarker.prototype.Position = function (ic)
1451 if (typeof this.pos == 'number')
1453 return this.pos (ic);
1456 var predefined = { }
1458 function def_predefined (name, position)
1460 predefined[name] = new MIM.PredefinedMarker (name);
1461 predefined[name].pos = position;
1464 def_predefined ('@<', 0);
1465 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1466 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1467 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1468 def_predefined ('@[', function (ic) {
1469 if (ic.cursor_pos > 0)
1471 var pos = ic.cursor_pos;
1472 return ic.preedit.FindProp ('candidates', pos - 1).from;
1476 def_predefined ('@]', function (ic) {
1477 if (ic.cursor_pos < ic.preedit.length - 1)
1479 var pos = ic.cursor_pos;
1480 return ic.preedit.FindProp ('candidates', pos).to;
1482 return ic.preedit.length;
1484 for (var i = 0; i < 10; i++)
1485 def_predefined ("@" + i, i);
1486 predefined['@first'] = predefined['@<'];
1487 predefined['@last'] = predefined['@>'];
1488 predefined['@previous'] = predefined['@-'];
1489 predefined['@next'] = predefined['@+'];
1490 predefined['@previous-candidate-change'] = predefined['@['];
1491 predefined['@next-candidate-change'] = predefined['@]'];
1493 MIM.SurroundMarker = function (name)
1496 this.distance = parseInt (name.slice (2));
1497 if (isNaN (this.distance))
1498 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1500 MIM.SurroundMarker.prototype = new MIM.Marker ();
1501 MIM.SurroundMarker.prototype.Position = function (ic)
1503 return ic.cursor_pos + this.distance;
1506 MIM.Marker.prototype.Parser = function (domain, node)
1508 var name = node.firstChild.nodeValue;
1509 if (name.charAt (0) == '@')
1511 var n = predefined[name];
1514 if (name.charAt (1) == '-')
1515 return new MIM.SurroundMarker (name);
1516 throw new Xex.ErrTerm (MIM.Error.ParseError,
1517 "Invalid marker: " + name);
1519 return new MIM.NamedMarker (name);
1523 MIM.Selector = function (name)
1527 MIM.Selector.prototype = new Xex.Term ('selector');
1531 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1532 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1533 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1534 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1535 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1536 selectors["@["] = selectors["@previous-candidate-change"]
1537 = new MIM.Selector ('@[');
1538 selectors["@]"] = selectors["@next-candidate-change"]
1539 = new MIM.Selector ('@]');
1541 MIM.Selector.prototype.Parser = function (domain, node)
1543 var name = node.firstChild.nodeValue;
1544 var s = selectors[name];
1546 throw new Xex.ErrTerm (MIM.Error.ParseError,
1547 "Invalid selector: " + name);
1552 MIM.Rule = function (keyseq, actions)
1554 this.keyseq = keyseq;
1555 this.actions = actions;
1557 MIM.Rule.prototype = new Xex.Term ('rule');
1558 MIM.Rule.prototype.Parser = function (domain, node)
1561 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1563 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1564 var keyseq = Xex.Term.Parse (domain, n);
1565 if (keyseq.type != 'keyseq')
1566 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1567 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1568 return new MIM.Rule (keyseq, actions);
1570 MIM.Rule.prototype.toString = function ()
1575 MIM.Map = function (name)
1578 this.rules = new Array ();
1582 var proto = new Xex.Term ('map');
1584 proto.Parser = function (domain, node)
1586 var name = node.attributes['mname'].nodeValue;
1588 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1589 var map = new MIM.Map (name);
1590 for (var n = node.firstChild; n; n = n.nextSibling)
1591 if (n.nodeType == 1)
1592 map.rules.push (Xex.Term.Parse (domain, n));
1596 proto.toString = function ()
1598 var str = '<map mname="' + this.name + '">';
1599 var len = this.rules.length;
1600 for (i = 0; i < len; i++)
1601 str += this.rules[i];
1602 return str + '</map>';
1605 MIM.Map.prototype = proto;
1608 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1610 MIM.Action = function (domain, terms)
1612 var args = new Array ();
1613 args.push (Xex.CatchTag_.mimtag);
1614 for (var i = 0; i < terms.length; i++)
1615 args.push (terms[i]);
1616 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1619 MIM.Action.prototype.Run = function (domain)
1621 var result = this.action.Eval (domain);
1622 if (result.type == 'error')
1624 domain.context.Error = result.toString ();
1627 return (result != Xex.CatchTag._mimtag);
1630 MIM.Keymap = function ()
1633 this.submaps = null;
1634 this.actions = null;
1640 function add_rule (keymap, rule)
1642 var keyseq = rule.keyseq;
1643 var len = keyseq.val.length;
1646 for (var i = 0; i < len; i++)
1648 var key = keyseq.val[i];
1652 if (! keymap.submaps)
1653 keymap.submaps = {};
1655 sub = keymap.submaps[key.key];
1657 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1661 keymap.actions = rule.actions;
1664 proto.Add = function (map)
1666 var rules = map.rules;
1667 var len = rules.length;
1669 for (var i = 0; i < len; i++)
1670 add_rule (this, rules[i]);
1672 proto.Lookup = function (keys, index)
1676 if (index < keys.val.length && this.submaps
1677 && (sub = this.submaps[keys.val[index].key]))
1680 return sub.Lookup (keys, index);
1682 return { map: this, index: index };
1685 MIM.Keymap.prototype = proto;
1688 MIM.State = function (name)
1691 this.keymap = new MIM.Keymap ();
1695 var proto = new Xex.Term ('state');
1697 proto.Parser = function (domain, node)
1699 var map_list = domain.map_list;
1700 var name = node.attributes['sname'].nodeValue;
1702 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1703 var state = new MIM.State (name);
1704 for (node = node.firstElement (); node; node = node.nextElement ())
1706 if (node.nodeName == 'title')
1707 state.title = node.firstChild.nodeValue;
1710 var n = node.firstElement ();
1711 if (node.nodeName == 'branch')
1713 state.keymap.Add (map_list[node.attributes['mname'].nodeValue]);
1714 state.keymap.actions = Xex.Term.Parse (domain, n, null);
1716 else if (node.nodeName == 'state-hook')
1717 state.enter_actions = Xex.Term.Parse (domain, n, null);
1718 else if (node.nodeName == 'catch-all-branch')
1719 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1725 proto.toString = function ()
1727 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1730 MIM.State.prototype = proto;
1733 MIM.im_domain = new Xex.Domain ('input-method', null, null);
1734 MIM.im_domain.DefType (MIM.KeySeq.prototype);
1735 MIM.im_domain.DefType (MIM.Marker.prototype);
1736 MIM.im_domain.DefType (MIM.Selector.prototype);
1737 MIM.im_domain.DefType (MIM.Rule.prototype);
1738 MIM.im_domain.DefType (MIM.Map.prototype);
1739 MIM.im_domain.DefType (MIM.State.prototype);
1742 var im_domain = MIM.im_domain;
1744 function Finsert (domain, vari, args)
1747 if (args[0].type == 'integer')
1748 text = String.fromCharCode (args[0].val);
1751 domain.context.insert (text, null);
1754 function Finsert_candidates (domain, vari, args)
1756 var ic = domain.context;
1757 var candidates = new Candidates (args, column);
1758 ic.insert (candidates.Current (), candidates);
1762 function Fdelete (domain, vari, args)
1764 var ic = domain.context;
1765 var pos = args[0].IsInt ? args[0].Intval : args[0].Position (ic);
1767 return new Xex.Term (ic.del (pos));
1770 function Fselect (domain, vari, args)
1772 var ic = domain.context;
1773 var can = ic.candidates;
1777 var candidate = can.Current ();
1779 ic.del (ic.cursor_pos - candidate.length);
1780 candidate = can.Select (args[0]);
1781 ic.insert (candidate, can);
1786 function Fchar_at (domain, vari, args)
1788 return new Xex.Term (args[0].CharAt (domain.context));
1791 function Fmove (domain, vari, args)
1793 var ic = domain.context;
1794 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
1799 function Fmark (domain, vari, args)
1801 args[0].Mark (domain.context);
1805 function Fpushback (domain, vari, args)
1807 var arg = (args[0].IsInt ? args[0].Intval
1808 : args[0].IsStr ? new KeySeq (args[0])
1810 domain.context.pushback (arg)
1814 function Fundo (domain, vari, args)
1816 var ic = domain.context;
1817 var n = args.length == 0 ? -2 : args[0].val;
1819 ic.keys.val.splice (ic.keys.length + n, -n);
1821 ic.keys.val.splice (n, ic.keys.length);
1826 function Fcommit (domain, vari, args)
1828 domain.context.commit ();
1832 function Funhandle (domain, vari, args)
1834 domain.context.commit ();
1835 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
1838 function Fshift (domain, vari, args)
1840 var ic = domain.context;
1841 var state_name = args[0].val;
1842 var state = ic.im.state_list[state_name];
1844 throw ("Unknown state: " + state_name);
1849 function Fsurrounding_flag (domain, vari, args)
1851 return new Xex.IntTerm (-1);
1854 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
1855 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
1856 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
1857 im_domain.DefSubr (Fselect, "select", false, 1, 1);
1858 //im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
1859 //im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
1860 im_domain.DefSubr (Fmove, "move", false, 1, 1);
1861 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
1862 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
1863 //im_domain.DefSubr (Fpop, "pop", false, 0, 0);
1864 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
1865 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
1866 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
1867 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
1868 //im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
1869 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
1870 //im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
1871 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
1876 function get_global_var (vname)
1878 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
1879 MIM.im_global.Load ()
1880 return MIM.im_global.domain.variables[vname];
1883 function include (node)
1885 node = node.firstElement ();
1886 if (node.nodeName != 'tags')
1889 var lang = null, name = null, extra = null;
1890 for (node = node.firstElement (); node; node = node.nextElement ())
1892 if (node.nodeName == 'language')
1893 lang = node.firstChild.nodeValue;
1894 else if (node.nodeName == 'name')
1895 name = node.firstChild.nodeValue;
1896 else if (node.nodeName == 'extra-id')
1897 extra = node.firstChild.nodeValue;
1899 if (! lang || ! MIM.imlist[lang])
1903 if (! name || ! (im = MIM.imlist[lang][name]))
1908 if (! (im = MIM.imextra[lang][extra]))
1911 if (im.load_status != MIM.LoadStatus.Loaded
1912 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
1919 parsers['description'] = function (node)
1921 this.description = node.firstChild.nodeValue;
1923 parsers['variable-list'] = function (node)
1925 for (node = node.firstElement (); node; node = node.nextElement ())
1927 var vname = node.attributes['vname'].nodeValue;
1928 if (this != MIM.im_global)
1930 var vari = get_global_var (vname);
1932 this.domain.Defvar (vname);
1934 Xex.Term.Parse (this.domain, node);
1937 parsers['command-list'] = function (node)
1940 parsers['macro-list'] = function (node)
1942 for (var n = node.firstElement (); n; n = n.nextElement ())
1943 if (n.nodeName == 'xi:include')
1945 var im = include (n);
1947 alert ('inclusion fail');
1949 for (var macro in im.domain.functions)
1951 var func = im.domain.functions[macro];
1952 if (func instanceof Xex.Macro)
1953 im.domain.CopyFunc (this.domain, macro);
1955 n = n.previousSibling;
1956 node.removeChild (n.nextSibling);
1958 Xex.Term.Parse (this.domain, node.firstElement (), null);
1960 parsers['title'] = function (node)
1962 this.title = node.firstChild.nodeValue;
1964 parsers['map-list'] = function (node)
1966 for (node = node.firstElement (); node; node = node.nextElement ())
1968 if (node.nodeName == 'xi:include')
1970 var im = include (node);
1973 alert ('inclusion fail');
1976 for (var mapname in im.map_list)
1977 this.map_list[mapname] = im.map_list[mapname];
1981 var map = Xex.Term.Parse (this.domain, node);
1982 this.map_list[map.name] = map;
1986 parsers['state-list'] = function (node)
1988 this.domain.map_list = this.map_list;
1989 for (node = node.firstElement (); node; node = node.nextElement ())
1991 if (node.nodeName == 'state')
1993 var state = Xex.Term.Parse (this.domain, node);
1995 state.title = this.title;
1996 if (! this.initial_state)
1997 this.initial_state = state;
1998 this.state_list[state.name] = state;
2001 delete this.domain.map_list;
2004 MIM.IM = function (lang, name, extra_id, file)
2008 this.extra_id = extra_id;
2010 this.load_status = MIM.LoadStatus.NotLoaded;
2011 this.domain = new Xex.Domain (this.lang + '-'
2012 + (this.name != 'nil'
2013 ? this.name : this.extra_id),
2014 MIM.im_domain, null);
2020 var node = Xex.Load (null, this.file);
2023 this.load_status = MIM.LoadStatus.Error;
2027 this.initial_state = null;
2028 this.state_list = {};
2029 for (node = node.firstElement (); node; node = node.nextElement ())
2031 var name = node.nodeName;
2032 var parser = parsers[name];
2034 parser.call (this, node);
2036 this.load_status = MIM.LoadStatus.Loaded;
2037 MIM.log ('loading done: ' + this.lang + '-' + this.name + '-' + this.extra_id);
2042 MIM.IM.prototype = proto;
2044 MIM.IC = function (im, target)
2046 if (im.load_status == MIM.LoadStatus.NotLoaded)
2048 if (im.load_status != MIM.LoadStatus.Loaded)
2049 alert ('im:' + im.name + ' error:' + im.load_status);
2051 this.target = target;
2052 this.domain = new Xex.Domain ('context', im.domain, this);
2058 MIM.CandidateTable = function ()
2060 this.table = new Array ();
2063 MIM.CandidateTable.prototype.get = function (from)
2065 for (var i = 0; i < this.table.length; i++)
2067 var elt = this.table[i];
2068 if (elt.from <= from && elt.to > from)
2073 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2075 for (var i = 0; i < this.table.length; i++)
2077 var elt = this.table[i];
2078 if (elt.from >= from && elt.from < to
2079 || elt.to >= from && elt.to < to)
2083 elt.val = candidates;
2087 this.table.push ({ from: from, to: to, val: candidates });
2090 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2092 var diff = inserted - (to - from);
2093 for (var i = 0; i < this.table.length; i++)
2095 var elt = this.table[i];
2104 MIM.CandidateTable.prototype.clear = function ()
2106 this.table.length = 0;
2109 function Block (index, term)
2113 this.Data = term.val;
2114 else if (term.IsList)
2116 this.Data = new Array ();
2117 for (var i = 0; i < term.val.length; i++)
2118 this.Data.push (term.val[i].val);
2122 Block.prototype.Count = function () { return this.Data.length; }
2123 Block.prototype.get = function (i)
2125 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
2128 function fill_group (start)
2130 var nitems = this.group.length;
2132 var b = this.blocks[r];
2134 if (start < b.Index)
2135 while (start < b.Index)
2136 b = this.blocks[--r];
2138 while (start >= b.Index + b.Count ())
2139 b = this.blocks[++r];
2142 var count = b.Count ();
2144 for (var i = 0; i < nitems; i++, start++)
2149 if (r == this.blocks.Length)
2155 this.group[i] = b.get (start);
2160 function Candidates (candidates, column)
2162 this.column = column;
2166 this.blocks = new Array ();
2168 for (var i = 0; i < candidates.length; i++)
2170 var block = new Block (this.total, candidates[i]);
2171 this.blocks.push (block);
2172 this.total += block.Count ();
2176 Candidates.prototype.Column = function ()
2178 return (this.column > 0 ? this.index % this.column
2179 : this.index - this.blocks[this.row].Index);
2182 Candidates.prototype.GroupLength = function ()
2184 if (this.column > 0)
2186 var start = this.index - (this.index % this.column);
2187 return (start + this.column <= this.total ? this.column
2188 : this.total - start);
2190 return this.blocks[this.row].Count;
2193 Candidates.prototype.Current = function ()
2195 var b = this.blocks[this.row];
2196 return b.get (this.index - b.Index);
2199 Candidates.prototype.PrevGroup = function ()
2201 var col = this.Column ();
2203 if (this.column > 0)
2205 this.index -= this.column;
2206 if (this.index >= 0)
2207 nitems = this.column;
2210 var lastcol = (this.total - 1) % this.column;
2211 this.index = (col < lastcol ? this.total - lastcol + col
2213 this.row = this.blocks.length - 1;
2214 nitems = lastcol + 1;
2216 while (this.blocks[this.row].Index > this.index)
2221 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
2222 nitems = this.blocks[this.row].Count ();
2223 this.index = (this.blocks[this.row].Index
2224 + (col < nitems ? col : nitems - 1));
2229 Candidates.prototype.NextGroup = function ()
2231 var col = this.Column ();
2233 if (this.column > 0)
2235 this.index += this.column - col;
2236 if (this.index < this.total)
2238 if (this.index + col >= this.total)
2240 nitems = this.total - this.index;
2241 this.index = this.total - 1;
2245 nitems = this.column;
2254 while (this.blocks[this.row].Index > this.index)
2259 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
2260 nitems = this.blocks[this.row].Count ();
2261 this.index = (this.blocks[this.row].Index
2262 + (col < nitems ? col : nitems - 1));
2267 Candidates.prototype.Prev = function ()
2269 if (this.index == 0)
2271 this.index = this.total - 1;
2272 this.row = this.blocks.length - 1;
2277 if (this.blocks[this.row].Index > this.index)
2282 Candidates.prototype.Next = function ()
2285 if (this.index == this.total)
2292 var b = this.blocks[this.row];
2293 if (this.index == b.Index + b.Count ())
2298 Candidates.prototype.First = function ()
2300 this.index -= this.Column ();
2301 while (this.blocks[this.row].Index > this.index)
2305 Candidates.prototype.Last = function ()
2307 var b = this.blocks[this.row];
2308 if (this.column > 0)
2310 if (this.index + 1 < this.total)
2312 this.index += this.column - this.Column () + 1;
2313 while (b.Index + b.Count () <= this.index)
2314 b = this.blocks[++this.row];
2318 this.index = b.Index + b.Count () - 1;
2321 Candidates.prototype.Select = function (selector)
2323 if (selector.type == 'selector')
2325 switch (selector.val)
2327 case '@<': this.First (); break;
2328 case '@>': this.Last (); break;
2329 case '@-': this.Prev (); break;
2330 case '@+': this.Next (); break;
2331 case '@[': this.PrevGroup (); break;
2332 case '@]': this.NextGroup (); break;
2335 return this.Current ();
2338 if (this.column > 0)
2340 col = this.index % this.column;
2341 start = this.index - col;
2342 end = start + this.column;
2346 start = this.blocks[this.row].Index;
2347 col = this.index - start;
2348 end = start + this.blocks[this.row].Count;
2350 if (end > this.total)
2352 this.index += selector.val - col;
2353 if (this.index >= end)
2354 this.index = end - 1;
2355 if (this.column > 0)
2357 if (selector.val > col)
2358 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2362 while (this.blocks[this.row].Index > this.index)
2365 return this.Current ();
2368 function detach_candidates (ic)
2370 ic.candidate_table.clear ();
2371 ic.candidates = null;
2372 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2373 | ChangedStatus.CandidateList
2374 | ChangedStatus.CandidateIndex
2375 | ChangedStatus.CandidateShow);
2378 function set_cursor (prefix, pos)
2380 this.cursor_pos = pos;
2382 this.candidates = this.candidate_table.get (pos - 1);
2384 this.candidates = null;
2387 function save_state ()
2389 this.state_var_values = this.domain.SaveValues ();
2390 this.state_preedit = this.preedit;
2391 this.state_key_head = this.key_head;
2392 this.state_pos = this.cursor_pos;
2395 function restore_state ()
2397 this.domain.RestoreValues (this.state_var_values);
2398 this.preedit = this.state_preedit;
2399 set_cursor.call (this, "restore", this.state_pos);
2402 function handle_key ()
2404 var out = this.keymap.Lookup (this.keys, this.key_head);
2406 var branch_actions = this.state.keymap.actions;
2408 MIM.log ('handling ' + this.keys.val[this.key_head]
2409 + ' in ' + this.state.name + ':' + this.keymap.name);
2410 this.key_head = out.index;
2411 if (sub != this.keymap)
2414 restore_state.call (this);
2416 MIM.log ('submap found');
2417 if (this.keymap.actions)
2419 MIM.log ('taking map actions:');
2420 if (! this.take_actions (this.keymap.actions))
2423 else if (this.keymap.submaps)
2425 MIM.log ('no map actions');
2426 for (var i = this.state_key_head; i < this.key_head; i++)
2428 MIM.log ('inserting key:' + this.keys.val[i].key);
2429 this.insert (this.keys.val[i].key, null);
2432 if (! this.keymap.submaps)
2434 MIM.log ('terminal:');
2435 if (this.keymap.branch_actions != null)
2437 MIM.log ('branch actions:');
2438 if (! this.take_actions (branch_actions))
2441 if (this.keymap != this.state.keymap)
2442 this.shift (this.state);
2447 MIM.log ('no submap');
2448 var current_state = this.state;
2449 var map = this.keymap;
2453 MIM.log ('branch actions');
2454 if (! this.take_actions (this.keymap.branch_actions))
2458 if (map == this.keymap)
2460 MIM.log ('no state change');
2461 if (map == this.initial_state.keymap
2462 && this.key_head < this.keys.val.length)
2464 MIM.log ('unhandled');
2467 if (this.keymap != current_state.keymap)
2468 this.shift (current_state);
2469 else if (this.keymap.actions == null)
2470 this.shift (this.initial_state);
2479 this.produced = null;
2481 this.preedit_saved = '';
2482 this.cursor_pos = 0;
2483 this.marker_positions = {};
2484 this.candidates = null;
2485 this.candidate_show = false;
2487 this.prev_state = null;
2488 this.initial_state = this.im.initial_state;
2489 this.title = this.initial_state.title;
2490 this.state_preedit = '';
2491 this.state_key_head = 0;
2492 this.state_var_values = {};
2495 this.keys = new MIM.KeySeq ();
2497 this.key_unhandled = false;
2498 this.unhandled_key = null;
2499 this.changed = MIM.ChangedStatus.None;
2500 this.error_message = '';
2501 this.title = this.initial_state.title;
2504 this.preedit_saved = '';
2505 this.marker_positions = {};
2506 this.candidate_table = new MIM.CandidateTable ();
2507 this.candidates = null;
2508 this.candidate_show = false;
2509 this.shift (this.initial_state);
2512 catch_args: new Array (Xex.CatchTag._mimtag, null),
2514 take_actions: function (actions)
2516 var func_progn = this.domain.GetFunc ('progn');
2517 var func_catch = this.domain.GetFunc ('catch');
2518 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2519 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2520 term = term.Eval (this.domain);
2521 return (! term.IsSymbol || term.val != '@mimtag');
2524 GetSurroundingChar: function (pos)
2526 if (pos < 0 ? this.caret_pos < - pos : this.target.value.length < pos)
2528 return this.target.value.charCodeAt (this.caret_pos + pos);
2531 adjust_markers: function (from, to, inserted)
2533 var diff = inserted - (to - from);
2535 for (var m in this.marker_positions)
2536 if (this.marker_positions[m] > from)
2537 this.marker_positions[m] = (this.marker_positions[m] >= to
2538 ? pos + diff : from);
2539 if (this.cursor_pos >= to)
2540 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2541 else if (this.cursor_pos > from)
2542 set_cursor.call (this, 'adjust', from)
2545 preedit_replace: function (from, to, text, candidates)
2547 this.preedit = (this.preedit.substring (0, from)
2548 + text + this.preedit.substring (to));
2549 this.adjust_markers (from, to, text.length);
2550 this.candidate_table.adjust (from, to, text.length);
2552 this.candidate_table.put (from, from + text.length, candidates)
2555 insert: function (text, candidates)
2557 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2558 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2563 var deleted = pos - this.cursor_pos;
2566 this.DelSurroundText (pos);
2569 else if (pos > this.preedit.length)
2571 this.DelSurroundText (pos - this.preedit.length);
2572 pos = this.preedit.length;
2574 if (pos < this.cursor_pos)
2575 this.preedit = (this.predit.substring (0, pos)
2576 + this.preedit.substring (this.cursor_pos));
2578 this.preedit = (this.preedit.substring (0, this.cursor_pos)
2579 + this.predit.substring (pos));
2585 this.candidate_show = true;
2586 this.changed |= MIM.ChangedStatus.CandidateShow;
2591 this.candidate_show = false;
2592 this.changed |= MIM.ChangedStatus.CandidateShow;
2595 move: function (pos)
2599 else if (pos > this.preedit.length)
2600 pos = this.preedit.length;
2601 if (pos != this.cursor_pos)
2603 set_cursor.call (this, 'move', pos);
2604 this.changed |= MIM.ChangedStatus.Preedit;
2608 pushback: function (n)
2610 if (n instanceof MIM.KeySeq)
2612 if (this.key_head > 0)
2614 if (this.key_head < this.keys.val.length)
2615 this.keys.val.splice (this.key_head,
2616 this.keys.val.length - this.key_head);
2617 for (var i = 0; i < n.val.length; i++)
2618 this.keys.val.push (n.val[i]);
2624 if (this.key_head < 0)
2631 this.key_head = - n;
2632 if (this.key_head > this.keys.val.length)
2633 this.key_head = this.keys.val.length;
2639 if (this.key_head < this.keys.val.length)
2640 this.keys.val.splice (this.key_head, 1);
2645 if (this.preedit.length > 0)
2647 this.candidate_table.clear ();
2648 this.produced += this.preedit;
2649 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2653 shift: function (state)
2657 MIM.log ("shifting back to previous");
2658 if (this.prev_state == null)
2660 state = this.prev_state;
2663 MIM.log ("shifting to " + state.name);
2665 if (state == this.initial_state)
2670 this.keys.val.splice (0, this.key_head);
2672 this.prev_state = null;
2677 if (state != this.state)
2678 this.prev_state = this.state;
2680 if (state != this.state && state.enter_actions)
2681 this.take_actions (state.enter_actions);
2682 if (! this.state || this.state.title != state.title)
2683 this.changed |= MIM.ChangedStatus.StateTitle;
2685 this.keymap = state.keymap;
2686 this.state_key_head = this.key_head;
2687 save_state.call (this);
2690 Filter: function (key)
2694 this.key_unhandled = true;
2695 this.unhandled_key = key;
2698 if (key.key == '_reload')
2700 this.changed = MIM.ChangedStatus.None;
2702 this.key_unhandled = false;
2703 this.keys.val.push (key);
2705 while (this.key_head < this.keys.val.length)
2707 if (! handle_key.call (this))
2709 if (this.key_head < this.keys.val.length)
2711 this.unhandled_key = this.keys.val[this.key_head];
2712 this.keys.val.splice (this.key_head, this.key_head + 1);
2714 this.key_unhandled = true;
2720 this.key_unhandled = true;
2724 if (this.key_unhandled)
2726 this.keys.val.length = 0;
2727 this.key_head = this.state_key_head = this.commit_key_head = 0;
2729 return (! this.key_unhandled
2730 && this.produced.length == 0
2731 && this.preedit.length == 0);
2735 MIM.IC.prototype = proto;
2737 var node = Xex.Load (null, "imlist.xml");
2738 for (node = node.firstChild; node; node = node.nextSibling)
2739 if (node.nodeName == 'input-method')
2741 var lang = null, name = null, extra_id = null, file = null;
2743 for (var n = node.firstChild; n; n = n.nextSibling)
2745 if (n.nodeName == 'language')
2746 lang = n.firstChild.nodeValue;
2747 else if (n.nodeName == 'name')
2748 name = n.firstChild.nodeValue;
2749 else if (n.nodeName == 'extra-id')
2750 extra_id = n.firstChild.nodeValue;
2751 else if (n.nodeName == 'filename')
2752 file = n.firstChild.nodeValue;
2754 if (name && name != 'nil')
2756 if (! MIM.imlist[lang])
2757 MIM.imlist[lang] = {};
2758 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2760 else if (extra_id && extra_id != 'nil')
2762 if (! MIM.imextra[lang])
2763 MIM.imextra[lang] = {};
2764 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2767 if (MIM.imextra.t && MIM.imextra.t.global)
2768 MIM.im_global = MIM.imextra.t.global;
2771 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2772 MIM.im_global.load_status = MIM.LoadStatus.Error;
2778 var keys = new Array ();
2780 keys[0x08] = 'backspace';
2781 keys[0x0D] = 'return';
2782 keys[0x1B] = 'escape';
2783 keys[0x20] = 'space';
2784 keys[0x21] = 'pageup';
2785 keys[0x22] = 'pagedown';
2787 keys[0x24] = 'home';
2788 keys[0x25] = 'left';
2790 keys[0x27] = 'right';
2791 keys[0x28] = 'down';
2792 keys[0x2D] = 'insert';
2793 keys[0x2E] = 'delete';
2794 for (var i = 1; i <= 12; i++)
2795 keys[111 + i] = "f" + i;
2796 keys[0x90] = "numlock";
2797 keys[0xF0] = "capslock";
2799 MIM.decode_key_event = function (event)
2801 var key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
2802 : event.charCode ? event.charCode
2806 if (event.type == 'keydown')
2811 if (event.shiftKey) key = "S-" + key ;
2814 key = String.fromCharCode (key);
2815 if (event.altKey) key = "A-" + key ;
2816 if (event.ctrlKey) key = "C-" + key ;
2817 return new MIM.Key (key);
2821 MIM.add_event_listener
2822 = (window.addEventListener
2823 ? function (target, type, listener) {
2824 target.addEventListener (type, listener, false);
2826 : window.attachEvent
2827 ? function (target, type, listener) {
2828 target.attachEvent ('on' + type,
2830 listener.call (target, window.event);
2833 : function (target, type, listener) {
2835 = function (e) { listener.call (target, e || window.event); };
2838 MIM.log = function (msg)
2840 var node = document.getElementById ('log');
2841 node.value = msg + "\n" + node.value;
2844 MIM.debug_print = function (event, ic)
2848 if (! MIM.debug_nodes)
2850 MIM.debug_nodes = new Array ();
2851 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
2852 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
2853 MIM.debug_nodes['status0'] = document.getElementById ('status0');
2854 MIM.debug_nodes['status1'] = document.getElementById ('status1');
2855 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
2856 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
2857 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
2858 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
2860 var target = event.target;
2861 var code = event.keyCode;
2862 var ch = event.type == 'keydown' ? 0 : event.charCode;
2863 var key = MIM.decode_key_event (event);
2866 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + " : " + key;
2867 index = (event.type == 'keydown' ? '0' : '1');
2869 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
2871 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
2872 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
2873 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
2876 MIM.get_range = function (target, range)
2878 if (target.selectionStart != null) // for Mozilla
2880 range[0] = target.selectionStart;
2881 range[1] = target.selectionEnd;
2885 var r = document.selection.createRange ();
2886 var rr = r.duplicate ();
2888 rr.moveToElementText (target);
2889 rr.setEndPoint ('EndToEnd', range);
2890 range[0] = rr.text.length - r.text.length;
2891 range[1] = rr.text.length;
2895 MIM.set_caret = function (target, ic)
2897 if (target.setSelectionRange) // Mozilla
2899 var scrollTop = target.scrollTop;
2900 target.setSelectionRange (ic.spot, ic.spot + ic.preedit.length);
2901 target.scrollTop = scrollTop;
2905 var range = target.createTextRange ();
2906 range.moveStart ('character', ic.spot);
2907 range.moveEnd ('character', ic.spot + ic.preedit.length);
2913 var range = new Array ();
2915 MIM.check_range = function (target, ic)
2917 MIM.get_range (target, range);
2918 if (range[0] != ic.spot || range[1] - range[0] != ic.preedit.length
2919 || target.value.substring (range[0], range[1]) != ic.preedit)
2921 MIM.log ('reset:' + ic.spot + '-' + (ic.spot + ic.preedit.length)
2922 + '/' + range[0] + '-' + range[1]);
2925 target.value = (target.value.substring (0, range[0])
2926 + target.value.substring (range[1]));
2931 MIM.update = function (target, ic)
2933 var text = target.value;
2934 target.value = (text.substring (0, ic.spot)
2937 + text.substring (ic.spot));
2938 ic.spot += ic.produced.length;
2939 MIM.set_caret (target, ic);
2942 MIM.reset_ic = function (event)
2944 if (event.target.mim_ic)
2946 var ic = event.target.mim_ic;
2947 var pos = ic.spot + ic.preedit.length;
2950 event.target.setSelectionRange (pos, pos);
2954 MIM.keydown = function (event)
2956 var target = event.target;
2957 if (! (target.type == "text" || target.type == "textarea"))
2960 var ic = target.mim_ic;
2961 if (! ic || ic.im != MIM.current)
2963 MIM.log ('creating IC');
2964 ic = new MIM.IC (MIM.current, target);
2966 MIM.add_event_listener (target, 'blur', MIM.reset_ic);
2968 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2970 MIM.check_range (target, ic);
2971 MIM.debug_print (event, ic);
2972 ic.key = MIM.decode_key_event (event);
2975 MIM.keypress = function (event)
2977 if (! (event.target.type == "text" || event.target.type == "textarea"))
2980 var ic = event.target.mim_ic;
2984 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2987 ic.key = MIM.decode_key_event (event);
2994 MIM.log ("filtering " + ic.key);
2995 var result = ic.Filter (ic.key);
2996 MIM.update (event.target, ic);
2997 if (! ic.key_unhandled)
2998 event.preventDefault ();
3000 MIM.debug_print (event, ic);
3005 MIM.select_im = function (event)
3007 var target = event.target.parentNode;
3008 while (target.tagName != "SELECT")
3009 target = target.parentNode;
3012 for (var lang in MIM.imlist)
3013 for (var name in MIM.imlist[lang])
3014 if (idx++ == target.selectedIndex)
3016 im = MIM.imlist[lang][name];
3019 document.getElementsByTagName ('body')[0].removeChild (target);
3020 target.target.focus ();
3021 if (im && im != MIM.current)
3024 MIM.log ('select IM: ' + im.name);
3028 MIM.destroy_menu = function (event)
3030 if (event.target.tagName == "SELECT")
3031 document.getElementsByTagName ('body')[0].removeChild (event.target);
3034 MIM.select_menu = function (event)
3036 var target = event.target;
3038 if (! ((target.type == "text" || target.type == "textarea")
3039 && event.which == 1 && event.ctrlKey))
3042 var sel = document.createElement ('select');
3043 sel.onclick = MIM.select_im;
3044 sel.onmouseout = MIM.destroy_menu;
3045 sel.style.position='absolute';
3046 sel.style.left = (event.clientX - 10) + "px";
3047 sel.style.top = (event.clientY - 10) + "px";
3048 sel.target = target;
3050 for (var lang in MIM.imlist)
3051 for (var name in MIM.imlist[lang])
3053 var option = document.createElement ('option');
3054 var imname = lang + "-" + name;
3055 option.appendChild (document.createTextNode (imname));
3056 option.value = imname;
3057 sel.appendChild (option);
3058 if (MIM.imlist[lang][name] == MIM.current)
3059 sel.selectedIndex = idx;
3063 document.getElementsByTagName ('body')[0].appendChild (sel);
3066 MIM.test = function ()
3068 var im = MIM.imlist['t']['latn-post'];
3069 var ic = new MIM.IC (im, null);
3071 ic.Filter (new MIM.Key ('a'));
3072 ic.Filter (new MIM.Key ("'"));
3075 document.getElementById ('text').value = ic.produced + ic.preedit;
3078 document.getElementById ('text').value
3079 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3081 if (e instanceof Xex.ErrTerm)
3089 MIM.init = function ()
3091 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3092 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3093 MIM.add_event_listener (window, 'mousedown', MIM.select_menu);
3094 if (window.location == 'http://localhost/mim/index.html')
3095 MIM.server = 'http://localhost/mim';
3096 MIM.current = MIM.imlist['vi']['telex'];
3099 MIM.init_debug = function ()