1 // -* coding: utf-8; -*
5 Log: function (arg, indent, cont)
10 Xex.LogNode.value = '';
14 if (indent != undefined)
15 for (var i = 0; i <= indent; i++)
18 Xex.LogNode.value += "\n";
19 Xex.LogNode.value += str + arg;
20 Xex.LogNode.scrollTop = Xex.LogNode.scrollHeight;
26 UnknownError: "unknown-error",
27 WrongArgument: "wrong-argument",
29 InvalidInteger: "invalid-integer",
30 TermTypeInvalid: "term-type-invalid",
31 FunctionConflict: "function-conflict",
32 VariableTypeConflict: "variable-type-conflict",
33 VariableRangeConflict: "variable-range-conflict",
34 VariableWrongRange: "variable-wrong-range",
35 VariableWrongValue: "variable-wrong-value",
37 UnknownFunction: "unknown-function",
38 MacroExpansionError: "macro-expansion-error",
39 NoVariableName: "no-variable-name",
40 NoFunctionName: "no-funcion-name",
43 ArithmeticError: "arithmetic-error",
44 WrongType: "wrong-type",
45 IndexOutOfRange: "index-out-of-range",
46 ValueOutOfRange: "value-out-of-range",
47 NoLoopToBreak: "no-loop-to-break",
48 UncaughtThrow: "uncaught-throw"
51 Xex.Variable = function (domain, name, desc, val, range)
60 Xex.Variable.prototype.clone = function ()
62 return new Xex.Variable (this.domain, this.name, this.desc,
63 this.val, this.range);
66 Xex.Variable.prototype.Equals = function (obj)
68 return ((obj instanceof Xex.Variable)
69 && obj.name == this.name);
72 Xex.Variable.prototype.SetValue = function (term)
78 Xex.Function = function (name, with_var, min_args, max_args)
81 this.with_var = with_var;
82 this.min_args = min_args;
83 this.max_args = max_args;
86 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
89 this.with_var = with_var;
90 this.min_args = min_args;
91 this.max_args = max_args;
92 this.builtin = builtin;
95 Xex.Subrountine.prototype.Call = function (domain, vari, args)
97 var newargs = new Array ();
98 for (var i = 0; i < args.length; i++)
100 newargs[i] = args[i].Eval (domain);
101 if (domain.Thrown ())
104 return this.builtin (domain, vari, newargs)
107 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
110 this.with_var = with_var;
111 this.min_args = min_args;
112 this.max_args = max_args;
113 this.builtin = builtin;
116 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
118 return this.builtin (domain, vari, args)
121 Xex.Lambda = function (name, min_args, max_args, args, body)
124 this.min_args = min_args;
125 this.max_args = max_args;
130 Xex.Lambda.prototype.Call = function (domain, vari, args)
132 var current = domain.bindings;
133 var result = Xex.Zero;
134 var limit = max_args >= 0 ? args.length : args.length - 1;
138 for (i = 0; i < limit; i++)
140 result = args[i].Eval (domain);
141 if (domain.Thrown ())
143 domain.Bind (this.args[i], result);
147 var list = new Array ();
148 for (i = 0; i < args[limit].length; i++)
150 result = args[limit].Eval (domain);
151 if (domain.Thrown ())
155 domain.Bind (this.args[limit], list);
158 domain.Catch (Xex.CatchTag.Return);
159 for (var term in this.body)
161 result = term.Eval (domain);
162 if (domain.Thrown ())
169 domain.UnboundTo (current);
174 Xex.Macro = function (name, min_args, max_args, args, body)
177 this.min_args = min_args;
178 this.max_args = max_args;
183 Xex.Macro.prototype.Call = function (domain, vari, args)
185 var current = domain.bindings;
186 var result = Xex.Zero;
190 for (i = 0; i < args.length; i++)
191 domain.Bind (this.args[i], args[i]);
193 domain.Catch (Xex.CatchTag.Return);
194 for (var i in this.body)
196 result = this.body[i].Eval (domain);
197 if (domain.Thrown ())
204 domain.UnboundTo (current);
209 Xex.Bindings = function (vari)
212 this.old_value = vari.val;
215 Xex.Bindings.prototype.UnboundTo = function (boundary)
217 for (var b = this; b != boundary; b = b.next)
218 b.vari.val = b.old_value;
222 Xex.Bind = function (bindings, vari, val)
224 var b = new Xex.Bindings (vari);
235 Xex.Domain = function (name, parent, context)
238 this.context = context;
241 if (name != 'basic' && ! parent)
242 parent = Xex.BasicDomain
243 this.parent = parent;
250 for (elt in parent.termtypes)
251 this.termtypes[elt] = parent.termtypes[elt];
252 for (elt in parent.functions)
253 this.functions[elt] = parent.functions[elt];
254 for (elt in parent.variables)
256 var vari = parent.variables[elt];
257 this.variables[elt] = new Xex.Variable (this, vari.name, vari.desc,
258 vari.val, vari.range);
262 this.call_stack = new Array ();
263 this.bindings = null;
264 this.catch_stack = new Array ();
265 this.catch_count = 0;
269 Xex.Domain.prototype = {
270 CallStackCount: function () { return this.call_stack.length; },
271 CallStackPush: function (term) { this.call_stack.push (term); },
272 CallStackPop: function () { this.call_stack.pop (); },
273 Bind: function (vari, val)
275 this.bindings = Xex.Bind (this.bindings, vari, val);
277 UnboundTo: function (boundary)
280 this.bindings = this.bindings.UnboundTo (boundary);
282 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
285 this.catch_stack.pop ();
286 if (this.catch_count > this.catch_stack.length)
291 if (this.catch_count < this.catch_stack.length)
293 this.caught = (this.catch_count == this.catch_stack.length - 1);
299 ThrowReturn: function ()
301 for (var i = this.catch_stack.length - 1; i >= 0; i--)
304 if (this.catch_stack[i] == Xex.CatchTag.Return)
308 ThrowBreak: function ()
310 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
311 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
312 "No surrounding loop to break");
315 ThrowSymbol: function (tag)
317 var i = this.catch_count;
318 for (var j = this.catch_stack.length - 1; j >= 0; j--)
321 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
323 this.catch_count = i;
327 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
328 "No corresponding catch: " + tag);
330 DefType: function (obj)
333 if (this.termtypes[type])
334 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
335 "Already defined: " + type);
336 if (this.functions[type])
337 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
338 "Already defined as a funciton or a macro: "
340 this.termtypes[type] = obj.Parser;
342 DefSubr: function (builtin, name, with_var, min_args, max_args)
344 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
347 DefSpecial: function (builtin, name, with_var, min_args, max_args)
349 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
352 Defun: function (name, min_args, max_args, args, body)
354 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
357 DefunByFunc: function (func) { this.functions[func.name] = func; },
358 Defmacro: function (name, min_args, max_args, args, body)
360 this.functions[name] = new Xex.Macro (name, min_args, max_args,
363 DefAlias: function (alias, fname)
365 var func = this.functions[fname];
368 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
369 if (this.termtypes[alias])
370 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
371 "Already defined as a term type: " + alias);
372 if (this.functions[alias])
373 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
374 "Already defined as a function: " + alias);
375 this.functions[alias] = func;
377 Defvar: function (name, desc, val, range)
379 var vari = new Xex.Variable (this, name, desc, val, range);
380 this.variables[name] = vari;
383 GetFunc: function (name)
385 var func = this.functions[name];
387 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
388 "Unknown function: " + name);
391 CopyFunc: function (domain, name)
393 var func = this.functions[name];
394 domain.DefunByFunc (func);
397 CopyFuncAll: function (domain)
399 for (var elt in this.functions)
400 domain.DefunByFunc (this.functions[elt]);
402 GetVarCreate: function (name)
404 var vari = this.variables[name];
406 vari = this.variables[name] = new Xex.Variable (this, name, null,
410 GetVar: function (name) { return this.variables[name]; },
411 SaveValues: function ()
414 for (var elt in this.variables)
415 values[elt] = this.variables[elt].val.Clone ();
418 RestoreValues: function (values)
423 var vari = this.variables[name];
424 vari.val = values[name];
429 Xex.Term = function (type) { this.type = type; }
430 Xex.Term.prototype = {
431 IsTrue: function () { return true; },
432 Eval: function (domain) { return this.Clone (); },
433 Clone: function (domain) { return this; },
434 Equals: function (obj)
436 return (this.type == obj.type
438 && obj.val == this.val);
440 Matches: function (obj) { return this.Equals (obj); },
441 toString: function ()
443 if (this.val != undefined)
444 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
445 return '<' + this.type + '/>';
447 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
448 "Not an integer"); },
449 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
453 Node.prototype.firstElement = function ()
455 for (var n = this.firstChild; n; n = n.nextSibling)
461 Node.prototype.nextElement = function ()
463 for (var n = this.nextSibling; n; n = n.nextSibling)
470 function parse_defvar (domain, node)
472 var name = node.attributes['vname'].nodeValue;
474 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
475 var vari = domain.variables[name];
476 var desc, val, range;
479 desc = vari.description;
483 node = node.firstElement ();
484 if (node && node.nodeName == 'description')
486 desc = node.firstChild.nodeValue;
487 node = node.nextElement ();
491 val = Xex.Term.Parse (domain, node);
492 node = node.nextElement ();
493 if (node && node.nodeName == 'possible-values')
494 for (node = node.firstElement (); node; node = node.nextElement ())
497 if (node.nodeName == 'range')
500 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
501 'Range not allowed for ' + name);
503 for (var n = node.firstElement (); n; n = n.nextElement ())
505 var v = Xex.Term.Parse (domain, n);
507 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
508 'Invalid range value: ' + val);
514 pval = Xex.Term.Parse (domain, node);
515 if (val.type != pval.type)
516 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
517 'Invalid possible value: ' + pval);
520 range = new Array ();
526 domain.Defvar (name, desc, val, range);
530 function parse_defun_head (domain, node)
532 var name = node.attributes['fname'].nodeValue;
534 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
535 var args = new Array ();
536 var nfixed = 0, noptional = 0, nrest = 0;
538 node = node.firstElement ();
539 if (node && node.nodeName == 'args')
542 for (n = n.firstElement (); n; n = n.nextElement ())
544 if (n.nodeName == 'fixed')
546 else if (n.nodeName == 'optional')
548 else if (n.nodeName == 'rest')
551 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
554 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
555 for (n = node.firstElement (); n; n = n.nextElement ())
556 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
558 args.min_args = nfixed;
559 args.max_args = nrest == 0 ? nfixed + noptional : -1;
561 if (node.nodeName == 'defun')
562 domain.Defun (name, args, null);
564 domain.Defmacro (name, args, null);
568 function parse_defun_body (domain, node)
570 var name = node.attributes['fname'].nodeValue;
571 var func = domain.GetFunc (name);
573 for (node = node.firstElement (); node; node = node.nextElement ())
574 if (node.nodeName != 'description' && node.nodeName != 'args')
576 body = Xex.Term.Parse (domain, node, null);
580 Xex.Term.Parse = function (domain, node, stop)
582 if (arguments.length == 2)
584 var name = node.nodeName;
585 var parser = domain.termtypes[name];
588 return parser (domain, node);
589 if (name == 'defun' || name == 'defmacro')
591 name = parse_defun_head (domain, node);
592 parse_defun_body (domain, node);
593 return new Xex.StrTerm (name);
595 if (name == 'defvar')
597 name = parse_defvar (domain, node);
598 return new Xex.StrTerm (name);
600 return new Xex.Funcall.prototype.Parser (domain, node);
602 for (var n = node; n && n != stop; n = n.nextElement ())
603 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
604 parse_defun_head (domain, n);
606 for (var n = node; n && n != stop; n = n.nextElement ())
608 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
609 parse_defun_body (domain, n);
610 else if (n.nodeName == 'defvar')
611 parse_defvar (domain, n);
615 terms = new Array ();
616 terms.push (Xex.Term.Parse (domain, n));
623 Xex.Varref = function (vname)
629 var proto = new Xex.Term ('varref');
631 proto.Clone = function () { return new Xex.Varref (this.val); }
632 proto.Eval = function (domain)
634 var vari = domain.GetVarCreate (this.val);
635 Xex.Log (this.ToString () + '=>' + vari.val, domain.depth);
639 proto.Parser = function (domain, node)
641 return new Xex.Varref (node.attributes['vname'].nodeValue);
644 proto.ToString = function ()
646 return '<varref vname="' + this.val + '"/>';
649 Xex.Varref.prototype = proto;
652 var null_args = new Array ();
654 Xex.Funcall = function (func, vname, args)
658 this.args = args || null_args;
662 var proto = new Xex.Term ('funcall');
664 proto.Parser = function (domain, node)
666 var fname = node.nodeName;
669 if (fname == 'funcall')
670 fname = node.attributes['fname'].nodeValue;
671 var func = domain.GetFunc (fname);
673 attr = node.attributes['vname'];
674 vname = attr != undefined ? attr.nodeValue : null;
675 var args = Xex.Term.Parse (domain, node.firstElement (), null);
676 return new Xex.Funcall (func, vname, args);
679 proto.New = function (domain, fname, vname, args)
681 var func = domain.GetFunc (fname);
682 var funcall = new Xex.Funcall (func, vname, args);
683 if (func instanceof Xex.Macro)
684 funcall = funcall.Eval (domain);
688 proto.Eval = function (domain)
690 if (! (this.func instanceof Xex.Subrountine))
691 Xex.Log (this, domain.depth);
694 vari = domain.GetVarCreate (this.vname);
698 result = this.func.Call (domain, vari, this.args);
700 Xex.Log (this + ' => ' + result, --domain.depth);
705 proto.Clone = function ()
707 return new Xex.Funcall (this.func, this.vari, this.args);
710 proto.Equals = function (obj)
712 return (obj.type == 'funcall'
713 && obj.func == this.func
714 && obj.vari.Equals (this.vari)
715 && obj.args.length == this.func.length);
718 proto.toString = function ()
721 var len = this.args.length;
722 var str = '<' + this.func.name;
724 str += ' vname="' + this.vari.name + '"';
727 if (this.func instanceof Xex.Subrountine)
728 for (var i = 0; i < len; i++)
729 arglist += this.args[i].toString ();
731 for (var i = 0; i < len; i++)
733 return str + '>' + arglist + '</' + this.func.name + '>';
736 Xex.Funcall.prototype = proto;
739 Xex.ErrTerm = function (ename, message, stack)
742 this.message = message;
747 var proto = new Xex.Term ('error');
749 proto.IsError = true;
751 proto.Parser = function (domain, node)
753 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
754 node.innerText, false);
757 proto.CallStack = function () { return stack; }
759 proto.SetCallStack = function (value) { statck = value; }
761 proto.Clone = function ()
763 return new Xex.ErrTerm (ename, message, false);
766 proto.Equals = function (obj)
769 && obj.ename == ename && obj.message == message
770 && (obj.stack ? (stack && stack.length == obj.stack.length)
774 proto.Matches = function (obj)
776 return (obj.IsError && obj.ename == ename);
779 proto.toString = function ()
781 return '<error ename="' + this.ename + '">' + this.message + '</error>';
784 Xex.ErrTerm.prototype = proto;
787 Xex.IntTerm = function (num) { this.val = num; };
789 var proto = new Xex.Term ('integer');
791 proto.Intval = function () { return this.val; };
792 proto.IsTrue = function () { return this.val != 0; }
793 proto.Clone = function () { return new Xex.IntTerm (this.val); }
794 proto.Parser = function (domain, node)
796 var str = node.firstChild.nodeValue;
798 if (str.charAt (0) == '?' && str.length == 2)
799 return new Xex.IntTerm (str.charCodeAt (1));
800 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
802 Xex.IntTerm.prototype = proto;
805 Xex.StrTerm = function (str) { this.val = str; };
807 var proto = new Xex.Term ('string');
809 proto.Strval = function () { return this.val; };
810 proto.IsTrue = function () { return this.val.length > 0; }
811 proto.Clone = function () { return new Xex.StrTerm (this.val); }
812 proto.Parser = function (domain, node)
814 return new Xex.StrTerm (node.firstChild ? node.firstChild.nodeValue : '');
816 Xex.StrTerm.prototype = proto;
819 Xex.SymTerm = function (str) { this.val = str; };
821 var proto = new Xex.Term ('symbol');
822 proto.IsSymbol = true;
823 proto.IsTrue = function () { return this.val != 'nil'; }
824 proto.Clone = function () { return new Xex.SymTerm (this.val); }
825 proto.Parser = function (domain, node)
827 return new Xex.SymTerm (node.firstChild.nodeValue);
829 Xex.SymTerm.prototype = proto;
832 Xex.LstTerm = function (list) { this.val = list; };
834 var proto = new Xex.Term ('list');
836 proto.IsTrue = function () { return this.val.length > 0; }
837 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
839 proto.Equals = function (obj)
841 if (obj.type != 'list' || obj.val.length != this.val.length)
843 var i, len = this.val.length;
844 for (i = 0; i < len; i++)
845 if (! this.val[i].Equals (obj.val[i]))
850 proto.Parser = function (domain, node)
852 var list = Xex.Term.Parse (domain, node.firstElement (), null);
853 return new Xex.LstTerm (list);
856 proto.toString = function ()
858 var len = this.val.length;
863 for (var i = 0; i < len; i++)
864 str += this.val[i].toString ();
865 return str + '</list>';
867 Xex.LstTerm.prototype = proto;
871 var basic = new Xex.Domain ('basic', null, null);
873 function Fset (domain, vari, args)
876 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
877 'No variable name to set');
878 vari.SetValue (args[0]);
882 function Fnot (domain, vari, args)
884 return (args[0].IsTrue () ? Xex.Zero : Xex.One);
887 function maybe_set_intvar (vari, n)
889 var term = new Xex.IntTerm (n);
891 vari.SetValue (term);
895 function Fadd (domain, vari, args)
897 var n = vari ? vari.val.Intval () : 0;
898 var len = args.length;
900 for (var i = 0; i < len; i++)
901 n += args[i].Intval ();
902 return maybe_set_intvar (vari, n);
905 function Fmul (domain, vari, args)
907 var n = vari ? vari.val.Intval () : 1;
908 for (var i = 0; i < args.length; i++)
910 return maybe_set_intvar (vari, n);
913 function Fsub (domain, vari, args)
919 n = args[0].Intval ();
924 n = vari.val.Intval ();
927 while (i < args.length)
928 n -= args[i++].Intval ();
929 return maybe_set_intvar (vari, n);
932 function Fdiv (domain, vari, args)
938 n = args[0].Intval ();
943 n = vari.val.Intval ();
946 while (i < args.length)
947 n /= args[i++].Intval ();
948 return maybe_set_intvar (vari, n);
951 function Fmod (domain, vari, args)
953 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
956 function Flogior (domain, vari, args)
958 var n = vari == null ? 0 : vari.val;
959 for (var i = 0; i < args.length; i++)
961 return maybe_set_intvar (vari, n);
964 function Flogand (domain, vari, args)
969 Xex.Log ("logand arg args[0]" + args[0]);
970 n = args[0].Intval ()
975 Xex.Log ("logand arg var " + vari);
976 n = vari.val.Intval ();
979 while (n > 0 && i < args.length)
981 Xex.Log ("logand arg " + args[i]);
984 return maybe_set_intvar (vari, n);
987 function Flsh (domain, vari, args)
989 return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
992 function Frsh (domain, vari, args)
994 return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
997 function Fand (domain, vari, args)
999 var len = args.length;
1000 for (var i = 0; i < len; i++)
1002 var result = args[i].Eval (domain);
1003 if (domain.Thrown ())
1005 if (! result.IsTrue ())
1011 function For (domain, vari, args)
1013 var len = args.length;
1014 for (var i = 0; i < len; i++)
1016 var result = args[i].Eval (domain);
1017 if (domain.Thrown ())
1019 if (result.IsTrue ())
1025 function Feq (domain, vari, args)
1027 for (var i = 1; i < args.length; i++)
1028 if (! args[i - 1].Equals (args[i]))
1033 function Fnoteq (domain, vari, args)
1035 return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1038 function Flt (domain, vari, args)
1040 var n = args[0].Intval ();
1042 for (var i = 1; i < args.length; i++)
1044 var n1 = args[i].Intval ();
1052 function Fle (domain, vari, args)
1054 var n = args[0].Intval ();
1055 for (var i = 1; i < args.length; i++)
1057 var n1 = args[i].Intval ();
1065 function Fgt (domain, vari, args)
1067 var n = args[0].Intval ();
1068 for (var i = 1; i < args.length; i++)
1070 var n1 = args[i].Intval ();
1078 function Fge (domain, vari, args)
1080 var n = args[0].Intval ();
1081 for (var i = 1; i < args.Length; i++)
1083 var n1 = args[i].Intval ();
1091 function Fprogn (domain, vari, args)
1093 var result = Xex.One;
1094 var len = args.length;
1096 for (var i = 0; i < len; i++)
1098 result = args[i].Eval (domain);
1099 if (domain.Thrown ())
1105 function Fif (domain, vari, args)
1107 var result = args[0].Eval (domain);
1109 if (domain.Thrown ())
1111 if (result.IsTrue ())
1112 return args[1].Eval (domain);
1113 if (args.length == 2)
1115 return args[2].Eval (domain);
1118 function Fcond (domain, vari, args)
1120 for (var i = 0; i < args.length; i++)
1123 var result = list.val[0].Eval (domain);
1124 if (result.IsTrue ())
1126 for (var j = 1; j < list.val.length; j++)
1129 result = list.val[j].Eval (domain);
1131 if (domain.Thrown ())
1140 function eval_terms (domain, terms, idx)
1142 var result = Xex.Zero;
1143 domain.caught = false;
1144 for (var i = idx; i < terms.length; i++)
1146 result = terms[i].Eval (domain);
1147 if (domain.Thrown ())
1153 function Fcatch (domain, vari, args)
1158 if (args[0].IsError)
1161 result = eval_terms (domain, args, 1);
1163 if (e instanceof Xex.ErrTerm)
1165 if (! args[0].Matches (e))
1173 else if (args[0].IsSymbol)
1176 domain.Catch (args[0].val);
1177 result = eval_terms (domain, args, 1);
1181 vari.SetValue (result);
1189 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1190 "Not a symbol nor an error: " + args[0]);
1193 function Fthrow (domain, vari, args)
1195 if (args[0].IsSymbl)
1197 domain.ThrowSymbol (args[0]);
1198 return (args[args.length - 1]);
1200 if (args[0].IsError)
1204 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1205 "Not a symbol nor an error:" + args[0]);
1208 Xex.BasicDomain = basic;
1210 basic.DefSubr (Fset, "set", true, 1, 1);
1211 basic.DefAlias ("=", "set");
1212 basic.DefSubr (Fnot, "not", false, 1, 1);
1213 basic.DefAlias ("!", "not");
1214 basic.DefSubr (Fadd, "add", true, 1, -1);
1215 basic.DefSubr (Fmul, "mul", true, 1, -1);
1216 basic.DefAlias ("*", "mul");
1217 basic.DefSubr (Fsub, "sub", true, 1, -1);
1218 basic.DefAlias ("-", "sub");
1219 basic.DefSubr (Fdiv, "div", true, 1, -1);
1220 basic.DefAlias ("/", "div");
1221 basic.DefSubr (Fmod, "mod", true, 1, 2);
1222 basic.DefAlias ("%", "mod");
1223 basic.DefSubr (Flogior, "logior", true, 1, -1);
1224 basic.DefAlias ('|', "logior");
1225 basic.DefSubr (Flogand, "logand", true, 1, -1);
1226 basic.DefAlias ("&", "logand");
1227 basic.DefSubr (Flsh, "lsh", true, 1, 2);
1228 basic.DefAlias ("<<", "lsh");
1229 basic.DefSubr (Frsh, "rsh", true, 1, 2);
1230 basic.DefAlias (">>", "rsh");
1231 basic.DefSubr (Feq, "eq", false, 2, -1);
1232 basic.DefAlias ("==", "eq");
1233 basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1234 basic.DefAlias ("!=", "noteq");
1235 basic.DefSubr (Flt, "lt", false, 2, -1);
1236 basic.DefAlias ("<", "lt");
1237 basic.DefSubr (Fle, "le", false, 2, -1);
1238 basic.DefAlias ("<=", "le");
1239 basic.DefSubr (Fgt, "gt", false, 2, -1);
1240 basic.DefAlias (">", "gt");
1241 basic.DefSubr (Fge, "ge", false, 2, -1);
1242 basic.DefAlias (">=", "ge");
1243 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1245 //basic.DefSubr (Fappend, "append", true, 0, -1);
1246 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1247 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1248 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1249 //basic.DefSubr (Fins, "ins", true, 2, 2);
1250 //basic.DefSubr (Fdel, "del", true, 2, 2);
1251 //basic.DefSubr (Feval, "eval", false, 1, 1);
1252 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1253 //basic.DefSubr (Freturn, "return", false, 0, 1);
1254 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1256 basic.DefSpecial (Fand, "and", false, 1, -1);
1257 basic.DefAlias ("&&", "and");
1258 basic.DefSpecial (For, "or", false, 1, -1);
1259 basic.DefAlias ("||", "or");
1260 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1261 basic.DefAlias ("expr", "progn");
1262 basic.DefSpecial (Fif, "if", false, 2, 3);
1263 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1264 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1265 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1266 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1267 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1268 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1269 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1270 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1272 basic.DefType (Xex.Funcall.prototype);
1273 basic.DefType (Xex.Varref.prototype);
1274 basic.DefType (Xex.ErrTerm.prototype);
1275 basic.DefType (Xex.IntTerm.prototype);
1276 basic.DefType (Xex.StrTerm.prototype);
1277 basic.DefType (Xex.SymTerm.prototype);
1278 basic.DefType (Xex.LstTerm.prototype);
1282 Xex.Zero = new Xex.IntTerm (0);
1283 Xex.One = new Xex.IntTerm (1);
1284 Xex.nil = new Xex.SymTerm ('nil');
1286 Xex.Load = function (server, file)
1288 var obj = new XMLHttpRequest ();
1289 var url = server ? server + '/' + file : file;
1290 obj.open ('GET', url, false);
1291 obj.overrideMimeType ('text/xml');
1293 return obj.responseXML.firstChild;
1297 // URL of the input method server.
1298 server: "http://www.m17n.org/common/mim-js",
1299 // Boolean flag to tell if MIM is active or not.
1301 // Boolean flag to tell if MIM is running in debug mode or not.
1303 // List of main input methods.
1305 // List of extra input methods;
1307 // Global input method data
1309 // Currently selected input method.
1313 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1320 CandidateIndex:0x10,
1322 Preedit: 0x06, // PreeditText | CursorPos
1323 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1345 ParseError: "parse-error"
1350 var keysyms = new Array ();
1351 keysyms["bs"] = "backspace";
1352 keysyms["lf"] = "linefeed";
1353 keysyms["cr"] = keysyms["enter"] = "return";
1354 keysyms["esc"] = "escape";
1355 keysyms["spc"] = "space";
1356 keysyms["del"] = "delete";
1358 function decode_keysym (str) {
1359 if (str.length == 1)
1361 var parts = str.split ("-");
1362 var len = parts.length, i;
1363 var has_modifier = len > 1;
1365 for (i = 0; i < len - 1; i++)
1366 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1368 var key = parts[len - 1];
1371 key = keysyms[key.toLowerCase ()];
1377 for (i = 1; i < len - 1; i++)
1378 str += '-' + parts[i];
1387 parts = new Array ();
1394 MIM.Key = function (val)
1397 if (val instanceof Xex.Term)
1399 else if (typeof val == 'string' || val instanceof String)
1401 this.key = decode_keysym (val);
1403 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1404 if (this.key instanceof Array)
1406 this.key = this.key[0];
1407 this.has_modifier = true;
1410 else if (typeof val == 'number' || val instanceof Number)
1411 this.key = String.fromCharCode (val);
1413 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1416 MIM.Key.prototype.toString = function () { return this.key; };
1418 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1419 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1420 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1424 MIM.KeySeq = function (seq)
1426 this.val = new Array ();
1432 var len = seq.val.length;
1433 for (var i = 0; i < len; i++)
1435 var v = seq.val[i], key;
1436 if (v.type == 'symbol' || v.type == 'string')
1437 key = new MIM.Key (v);
1438 else if (v.type == 'integer')
1439 key = new MIM.Key (v.val);
1441 throw new Xex.ErrTerm (MIM.Error.ParseError,
1442 "Invalid key: " + v);
1443 this.val.push (key);
1444 if (key.has_modifier)
1445 this.has_modifier = true;
1450 var len = seq.val.length;
1451 for (var i = 0; i < len; i++)
1452 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1455 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1459 var proto = new Xex.Term ('keyseq');
1460 proto.Clone = function () { return this; }
1461 proto.Parser = function (domain, node)
1463 var seq = new Array ();
1464 for (node = node.firstChild; node; node = node.nextSibling)
1465 if (node.nodeType == 1)
1467 var term = Xex.Term.Parse (domain, node);
1468 return new MIM.KeySeq (term);
1470 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1472 proto.toString = function ()
1474 var len = this.val.length;
1478 var str = '<keyseq>';
1479 for (var i = 0; i < len; i++)
1483 else if (this.has_modifier)
1485 str += this.val[i].toString ();
1487 return str + '</keyseq>';
1490 MIM.KeySeq.prototype = proto;
1494 MIM.Marker = function () { }
1495 MIM.Marker.prototype = new Xex.Term ('marker');
1496 MIM.Marker.prototype.CharAt = function (ic)
1498 var p = this.Position (ic);
1499 if (p < 0 || p >= ic.preedit.length)
1501 return ic.preedit.charCodeAt (p);
1504 MIM.FloatingMarker = function (name) { this.val = name; };
1505 var proto = new MIM.Marker ();
1506 MIM.FloatingMarker.prototype = proto;
1507 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1508 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1510 MIM.PredefinedMarker = function (name) { this.val = name; }
1511 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1512 MIM.PredefinedMarker.prototype.Position = function (ic)
1514 if (typeof this.pos == 'number')
1516 return this.pos (ic);
1519 var predefined = { }
1521 function def_predefined (name, position)
1523 predefined[name] = new MIM.PredefinedMarker (name);
1524 predefined[name].pos = position;
1527 def_predefined ('@<', 0);
1528 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1529 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1530 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1531 def_predefined ('@[', function (ic) {
1532 if (ic.cursor_pos > 0)
1534 var pos = ic.cursor_pos;
1535 return ic.preedit.FindProp ('candidates', pos - 1).from;
1539 def_predefined ('@]', function (ic) {
1540 if (ic.cursor_pos < ic.preedit.length - 1)
1542 var pos = ic.cursor_pos;
1543 return ic.preedit.FindProp ('candidates', pos).to;
1545 return ic.preedit.length;
1547 for (var i = 0; i < 10; i++)
1548 def_predefined ("@" + i, i);
1549 predefined['@first'] = predefined['@<'];
1550 predefined['@last'] = predefined['@>'];
1551 predefined['@previous'] = predefined['@-'];
1552 predefined['@next'] = predefined['@+'];
1553 predefined['@previous-candidate-change'] = predefined['@['];
1554 predefined['@next-candidate-change'] = predefined['@]'];
1556 MIM.SurroundMarker = function (name)
1559 this.distance = parseInt (name.slice (1));
1560 if (isNaN (this.distance))
1561 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1563 MIM.SurroundMarker.prototype = new MIM.Marker ();
1564 MIM.SurroundMarker.prototype.Position = function (ic)
1566 return ic.cursor_pos + this.distance;
1568 MIM.SurroundMarker.prototype.CharAt = function (ic)
1570 if (this.val == '@-0')
1572 var p = this.Position (ic);
1574 return ic.GetSurroundingChar (p);
1575 else if (p >= ic.preedit.length)
1576 return ic.GetSurroundingChar (p - ic.preedit.length);
1577 return ic.preedit.charCodeAt (p);
1580 MIM.Marker.prototype.Parser = function (domain, node)
1582 var name = node.firstChild.nodeValue;
1583 if (name.charAt (0) == '@')
1585 var n = predefined[name];
1588 if (name.charAt (1) == '-' || name.charAt (1) == '+')
1589 return new MIM.SurroundMarker (name);
1590 throw new Xex.ErrTerm (MIM.Error.ParseError,
1591 "Invalid marker: " + name);
1593 return new MIM.FloatingMarker (name);;
1597 MIM.Selector = function (name)
1601 MIM.Selector.prototype = new Xex.Term ('selector');
1605 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1606 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1607 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1608 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1609 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1610 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
1611 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
1613 MIM.Selector.prototype.Parser = function (domain, node)
1615 var name = node.firstChild.nodeValue;
1616 var s = selectors[name];
1618 throw new Xex.ErrTerm (MIM.Error.ParseError,
1619 "Invalid selector: " + name);
1624 MIM.Rule = function (keyseq, actions)
1626 this.keyseq = keyseq;
1627 this.actions = actions;
1629 MIM.Rule.prototype = new Xex.Term ('rule');
1630 MIM.Rule.prototype.Parser = function (domain, node)
1633 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1635 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1636 var keyseq = Xex.Term.Parse (domain, n);
1637 if (keyseq.type != 'keyseq')
1638 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1639 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1640 return new MIM.Rule (keyseq, actions);
1642 MIM.Rule.prototype.toString = function ()
1647 MIM.Map = function (name)
1650 this.rules = new Array ();
1654 var proto = new Xex.Term ('map');
1656 proto.Parser = function (domain, node)
1658 var name = node.attributes['mname'].nodeValue;
1660 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1661 var map = new MIM.Map (name);
1662 for (var n = node.firstChild; n; n = n.nextSibling)
1663 if (n.nodeType == 1)
1664 map.rules.push (Xex.Term.Parse (domain, n));
1668 proto.toString = function ()
1670 var str = '<map mname="' + this.name + '">';
1671 var len = this.rules.length;
1672 for (i = 0; i < len; i++)
1673 str += this.rules[i];
1674 return str + '</map>';
1677 MIM.Map.prototype = proto;
1680 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1682 MIM.Action = function (domain, terms)
1684 var args = new Array ();
1685 args.push (Xex.CatchTag_.mimtag);
1686 for (var i = 0; i < terms.length; i++)
1687 args.push (terms[i]);
1688 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1691 MIM.Action.prototype.Run = function (domain)
1693 var result = this.action.Eval (domain);
1694 if (result.type == 'error')
1696 domain.context.Error = result.toString ();
1699 return (result != Xex.CatchTag._mimtag);
1702 MIM.Keymap = function ()
1705 this.submaps = null;
1711 function add_rule (keymap, rule, branch_actions)
1713 var keyseq = rule.keyseq;
1714 var len = keyseq.val.length;
1717 for (var i = 0; i < len; i++)
1719 var key = keyseq.val[i];
1723 if (! keymap.submaps)
1724 keymap.submaps = {};
1726 sub = keymap.submaps[key.key];
1728 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1732 keymap.map_actions = rule.actions;
1733 keymap.branch_actions = branch_actions;
1736 proto.Add = function (map, branch_actions)
1738 var rules = map.rules;
1739 var len = rules.length;
1741 for (var i = 0; i < len; i++)
1742 add_rule (this, rules[i], branch_actions);
1744 proto.Lookup = function (keys, index)
1748 if (index < keys.val.length && this.submaps
1749 && ! keys.val[index])
1751 Xex.Log ('invalid key at ' + index);
1752 throw 'invalid key';
1755 if (index < keys.val.length && this.submaps
1756 && (sub = this.submaps[keys.val[index].key]))
1759 return sub.Lookup (keys, index);
1761 return { map: this, index: index };
1764 MIM.Keymap.prototype = proto;
1767 MIM.State = function (name)
1770 this.keymap = new MIM.Keymap ();
1774 var proto = new Xex.Term ('state');
1776 proto.Parser = function (domain, node)
1778 var map_list = domain.map_list;
1779 var name = node.attributes['sname'].nodeValue;
1781 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1782 var state = new MIM.State (name);
1783 for (node = node.firstElement (); node; node = node.nextElement ())
1785 if (node.nodeName == 'title')
1786 state.title = node.firstChild.nodeValue;
1789 var n = node.firstElement ();
1790 if (node.nodeName == 'branch')
1791 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1792 Xex.Term.Parse (domain, n, null));
1793 else if (node.nodeName == 'state-hook')
1794 state.enter_actions = Xex.Term.Parse (domain, n, null);
1795 else if (node.nodeName == 'catch-all-branch')
1796 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1802 proto.toString = function ()
1804 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1807 MIM.State.prototype = proto;
1811 function Block (index, term)
1815 this.Data = term.val;
1816 else if (term.IsList)
1818 this.Data = new Array ();
1819 for (var i = 0; i < term.val.length; i++)
1820 this.Data.push (term.val[i].val);
1824 Block.prototype.Count = function () { return this.Data.length; }
1825 Block.prototype.get = function (i)
1827 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1830 MIM.Candidates = function (candidates, column)
1832 this.column = column;
1836 this.blocks = new Array ();
1838 for (var i = 0; i < candidates.length; i++)
1840 var block = new Block (this.total, candidates[i]);
1841 this.blocks.push (block);
1842 this.total += block.Count ();
1848 return (this.column > 0 ? this.index % this.column
1849 : this.index - this.blocks[this.row].Index);
1852 function prev_group ()
1854 var col = get_col.call (this);
1856 if (this.column > 0)
1858 this.index -= this.column;
1859 if (this.index >= 0)
1860 nitems = this.column;
1863 var lastcol = (this.total - 1) % this.column;
1864 this.index = (col < lastcol ? this.total - lastcol + col
1866 this.row = this.blocks.length - 1;
1867 nitems = lastcol + 1;
1869 while (this.blocks[this.row].Index > this.index)
1874 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1875 nitems = this.blocks[this.row].Count ();
1876 this.index = (this.blocks[this.row].Index
1877 + (col < nitems ? col : nitems - 1));
1882 function next_group ()
1884 var col = get_col.call (this);
1886 if (this.column > 0)
1888 this.index += this.column - col;
1889 if (this.index < this.total)
1891 if (this.index + col >= this.total)
1893 nitems = this.total - this.index;
1894 this.index = this.total - 1;
1898 nitems = this.column;
1907 while (this.blocks[this.row].Index > this.index)
1912 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1913 nitems = this.blocks[this.row].Count ();
1914 this.index = (this.blocks[this.row].Index
1915 + (col < nitems ? col : nitems - 1));
1922 if (this.index == 0)
1924 this.index = this.total - 1;
1925 this.row = this.blocks.length - 1;
1930 if (this.blocks[this.row].Index > this.index)
1938 if (this.index == this.total)
1945 var b = this.blocks[this.row];
1946 if (this.index == b.Index + b.Count ())
1953 this.index -= get_col.call (this);
1954 while (this.blocks[this.row].Index > this.index)
1960 var b = this.blocks[this.row];
1961 if (this.column > 0)
1963 if (this.index + 1 < this.total)
1965 this.index += this.column - get_col.call (this) + 1;
1966 while (b.Index + b.Count () <= this.index)
1967 b = this.blocks[++this.row];
1971 this.index = b.Index + b.Count () - 1;
1974 MIM.Candidates.prototype.Current = function ()
1976 var b = this.blocks[this.row];
1977 return b.get (this.index - b.Index);
1980 MIM.Candidates.prototype.Select = function (selector)
1982 if (selector.type == 'selector')
1984 switch (selector.val)
1986 case '@<': first.call (this); break;
1987 case '@>': last.call (this); break;
1988 case '@-': prev.call (this); break;
1989 case '@+': next.call (this); break;
1990 case '@[': prev_group.call (this); break;
1991 case '@]': next_group.cal (this); break;
1994 return this.Current ();
1997 if (this.column > 0)
1999 col = this.index % this.column;
2000 start = this.index - col;
2001 end = start + this.column;
2005 start = this.blocks[this.row].Index;
2006 col = this.index - start;
2007 end = start + this.blocks[this.row].Count;
2009 if (end > this.total)
2011 this.index += selector.val - col;
2012 if (this.index >= end)
2013 this.index = end - 1;
2014 if (this.column > 0)
2016 if (selector.val > col)
2017 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2021 while (this.blocks[this.row].Index > this.index)
2024 return this.Current ();
2027 MIM.Candidates.prototype.CurrentGroup = function (selector)
2030 if (this.column > 0)
2032 col = this.index % this.column;
2033 start = this.index - col;
2034 end = start + this.column;
2035 if (end > this.total)
2040 start = this.blocks[this.row].Index;
2041 col = this.index - start;
2042 end = start + this.blocks[this.row].Count ();
2044 var group = new Array ();
2047 var block = this.blocks[row++];
2050 var c = block.get (start - block.Index);
2053 if (start == block.Index + block.Count ())
2054 block = this.blocks[row++];
2060 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2061 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2062 MIM.im_domain.DefType (MIM.Marker.prototype);
2063 MIM.im_domain.DefType (MIM.Selector.prototype);
2064 MIM.im_domain.DefType (MIM.Rule.prototype);
2065 MIM.im_domain.DefType (MIM.Map.prototype);
2066 MIM.im_domain.DefType (MIM.State.prototype);
2069 var im_domain = MIM.im_domain;
2071 function Finsert (domain, vari, args)
2074 if (args[0].type == 'integer')
2075 text = String.fromCharCode (args[0].val);
2078 domain.context.ins (text, null);
2082 function Finsert_candidates (domain, vari, args)
2084 var ic = domain.context;
2085 var gsize = domain.variables['candidates_group_size'];
2086 var candidates = new MIM.Candidates (args, gsize ? gsize.Intval () : 0);
2087 ic.ins (candidates.Current (), candidates);
2091 function Fdelete (domain, vari, args)
2093 var ic = domain.context;
2094 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2095 return new Xex.IntTerm (ic.del (pos));
2098 function Fselect (domain, vari, args)
2100 var ic = domain.context;
2101 var can = ic.candidates;
2105 var old_text = can.Current ();
2106 var new_text = can.Select (args[0]);
2107 ic.rep (old_text, new_text, can);
2110 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2114 function Fshow (domain, vari, args)
2116 domain.context.candidate_show = true;
2117 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2121 function Fhide (domain, vari, args)
2123 domain.context.candidate_show = false;
2124 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2128 function Fchar_at (domain, vari, args)
2130 return new Xex.IntTerm (args[0].CharAt (domain.context));
2133 function Fmove (domain, vari, args)
2135 var ic = domain.context;
2136 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2138 return new Xex.IntTerm (pos);
2141 function Fmark (domain, vari, args)
2143 args[0].Mark (domain.context);
2147 function Fpushback (domain, vari, args)
2149 var a = (args[0].IsInt ? args[0].Intval ()
2150 : args[0].IsStr ? new KeySeq (args[0])
2152 domain.context.pushback (a);
2156 function Fpop (domain, vari, args)
2158 var ic = domain.context;
2159 if (ic.key_head < ic.keys.val.length)
2160 ic.keys.val.splice (ic.keys_head, 1);
2164 function Fundo (domain, vari, args)
2166 var ic = domain.context;
2167 var n = args.length == 0 ? -2 : args[0].val;
2168 Xex.Log ('undo with arg ' + args[0]);
2170 ic.keys.val.splice (ic.keys.val.length + n, -n);
2172 ic.keys.val.splice (n, ic.keys.val.length);
2177 function Fcommit (domain, vari, args)
2179 domain.context.commit ();
2183 function Funhandle (domain, vari, args)
2185 domain.context.commit ();
2186 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2189 function Fshift (domain, vari, args)
2191 var ic = domain.context;
2192 var state_name = args[0].val;
2193 var state = ic.im.state_list[state_name];
2195 throw ("Unknown state: " + state_name);
2200 function Fshiftback (domain, vari, args)
2202 domain.context.shift (null);
2206 function Fkey_count (domain, vari, args)
2208 return new Xex.IntTerm (domain.context.key_head);
2211 function Fsurrounding_flag (domain, vari, args)
2213 return new Xex.IntTerm (-1);
2216 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2217 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2218 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2219 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2220 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2221 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2222 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2223 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2224 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2225 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2226 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2227 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2228 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2229 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2230 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2231 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2232 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2233 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2238 function get_global_var (vname)
2240 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2241 MIM.im_global.Load ()
2242 return MIM.im_global.domain.variables[vname];
2245 function include (node)
2247 node = node.firstElement ();
2248 if (node.nodeName != 'tags')
2251 var lang = null, name = null, extra = null;
2252 for (node = node.firstElement (); node; node = node.nextElement ())
2254 if (node.nodeName == 'language')
2255 lang = node.firstChild.nodeValue;
2256 else if (node.nodeName == 'name')
2257 name = node.firstChild.nodeValue;
2258 else if (node.nodeName == 'extra-id')
2259 extra = node.firstChild.nodeValue;
2261 if (! lang || ! MIM.imlist[lang])
2265 if (! name || ! (im = MIM.imlist[lang][name]))
2270 if (! (im = MIM.imextra[lang][extra]))
2273 if (im.load_status != MIM.LoadStatus.Loaded
2274 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2281 parsers['description'] = function (node)
2283 this.description = node.firstChild.nodeValue;
2285 parsers['variable-list'] = function (node)
2287 for (node = node.firstElement (); node; node = node.nextElement ())
2289 var vname = node.attributes['vname'].nodeValue;
2290 if (this != MIM.im_global)
2292 var vari = get_global_var (vname);
2294 this.domain.Defvar (vname);
2296 vname = Xex.Term.Parse (this.domain, node)
2299 parsers['command-list'] = function (node)
2302 parsers['macro-list'] = function (node)
2304 for (var n = node.firstElement (); n; n = n.nextElement ())
2305 if (n.nodeName == 'xi:include')
2307 var im = include (n);
2309 alert ('inclusion fail');
2311 for (var macro in im.domain.functions)
2313 var func = im.domain.functions[macro];
2314 if (func instanceof Xex.Macro)
2315 im.domain.CopyFunc (this.domain, macro);
2317 n = n.previousSibling;
2318 node.removeChild (n.nextSibling);
2320 Xex.Term.Parse (this.domain, node.firstElement (), null);
2322 parsers['title'] = function (node)
2324 this.title = node.firstChild.nodeValue;
2326 parsers['map-list'] = function (node)
2328 for (node = node.firstElement (); node; node = node.nextElement ())
2330 if (node.nodeName == 'xi:include')
2332 var im = include (node);
2335 alert ('inclusion fail');
2338 for (var mname in im.map_list)
2339 this.map_list[mname] = im.map_list[mname];
2343 var map = Xex.Term.Parse (this.domain, node);
2344 this.map_list[map.name] = map;
2348 parsers['state-list'] = function (node)
2350 this.domain.map_list = this.map_list;
2351 for (node = node.firstElement (); node; node = node.nextElement ())
2353 if (node.nodeName == 'xi:include')
2355 var im = include (node);
2357 alert ('inclusion fail');
2358 for (var sname in im.state_list)
2360 state = im.state_list[sname];
2361 if (! this.initial_state)
2362 this.initial_state = state;
2363 this.state_list[sname] = state;
2366 else if (node.nodeName == 'state')
2368 var state = Xex.Term.Parse (this.domain, node);
2370 state.title = this.title;
2371 if (! this.initial_state)
2372 this.initial_state = state;
2373 this.state_list[state.name] = state;
2376 delete this.domain.map_list;
2379 MIM.IM = function (lang, name, extra_id, file)
2383 this.extra_id = extra_id;
2385 this.load_status = MIM.LoadStatus.NotLoaded;
2386 this.domain = new Xex.Domain (this.lang + '-'
2387 + (this.name != 'nil'
2388 ? this.name : this.extra_id),
2389 MIM.im_domain, null);
2395 var node = Xex.Load (null, this.file);
2398 this.load_status = MIM.LoadStatus.Error;
2402 this.initial_state = null;
2403 this.state_list = {};
2404 for (node = node.firstElement (); node; node = node.nextElement ())
2406 var name = node.nodeName;
2407 var parser = parsers[name];
2409 parser.call (this, node);
2411 this.load_status = MIM.LoadStatus.Loaded;
2416 MIM.IM.prototype = proto;
2418 MIM.IC = function (im, target)
2420 if (im.load_status == MIM.LoadStatus.NotLoaded)
2422 if (im.load_status != MIM.LoadStatus.Loaded)
2423 alert ('im:' + im.name + ' error:' + im.load_status);
2425 this.target = target;
2426 this.domain = new Xex.Domain ('context', im.domain, this);
2428 this.range = new Array ();
2429 this.range[0] = this.range[1] = 0;
2431 this.initial_state = this.im.initial_state;
2432 this.keys = new MIM.KeySeq ();
2433 this.marker_positions = new Array ();
2434 this.candidate_table = new MIM.CandidateTable ();
2438 MIM.CandidateTable = function ()
2440 this.table = new Array ();
2443 MIM.CandidateTable.prototype.get = function (pos)
2445 for (var i = 0; i < this.table.length; i++)
2447 var elt = this.table[i];
2448 if (elt.from < pos && pos <= elt.to)
2453 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2455 for (var i = 0; i < this.table.length; i++)
2457 var elt = this.table[i];
2458 if (elt.from < to && elt.to > from)
2462 elt.val = candidates;
2466 this.table.push ({ from: from, to: to, val: candidates });
2469 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2471 var diff = inserted - (to - from);
2474 for (var i = 0; i < this.table.length; i++)
2476 var elt = this.table[i];
2485 MIM.CandidateTable.prototype.clear = function ()
2487 this.table.length = 0;
2490 function detach_candidates (ic)
2492 ic.candidate_table.clear ();
2493 ic.candidates = null;
2494 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2495 | MIM.ChangedStatus.CandidateList
2496 | MIM.ChangedStatus.CandidateIndex
2497 | MIM.ChangedStatus.CandidateShow);
2500 function set_cursor (prefix, pos)
2502 this.cursor_pos = pos;
2503 this.candidates = this.candidate_table.get (pos);
2506 function save_state ()
2508 this.state_var_values = this.domain.SaveValues ();
2509 this.state_preedit = this.preedit;
2510 this.state_key_head = this.key_head;
2511 this.state_pos = this.cursor_pos;
2514 function restore_state ()
2516 this.domain.RestoreValues (this.state_var_values);
2517 this.preedit = this.state_preedit;
2518 set_cursor.call (this, "restore", this.state_pos);
2521 function handle_key ()
2523 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
2524 + '" in ' + this.state.name + ':' + this.keymap.name
2525 + " key/state/commit-head/len:"
2526 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
2527 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
2530 if (out.index > this.key_head)
2532 this.key_head = out.index;
2533 Xex.Log (' with submap for ' + this.key_head + 'keys', false, true);
2534 restore_state.call (this);
2536 if (sub.map_actions)
2538 Xex.Log ('taking map actions:');
2539 if (! this.take_actions (sub.map_actions))
2542 else if (sub.submaps)
2544 Xex.Log ('no map actions');
2545 for (var i = this.state_key_head; i < this.key_head; i++)
2547 Xex.Log ('inserting key:' + this.keys.val[i].key);
2548 this.ins (this.keys.val[i].key, null);
2553 Xex.Log ('terminal:');
2554 if (this.keymap.branch_actions != null)
2556 Xex.Log ('branch actions:');
2557 if (! this.take_actions (this.keymap.branch_actions))
2560 if (sub != this.state.keymap)
2561 this.shift (this.state);
2566 Xex.Log (' without submap', false, true);
2568 var current_state = this.state;
2569 var map = this.keymap;
2571 if (map.branch_actions)
2573 Xex.Log ('branch actions');
2574 if (! this.take_actions (map.branch_actions))
2578 if (map == this.keymap)
2580 Xex.Log ('no state change');
2581 if (map == this.initial_state.keymap
2582 && this.key_head < this.keys.val.length)
2584 Xex.Log ('unhandled');
2587 if (this.keymap != current_state.keymap)
2588 this.shift (current_state);
2589 else if (this.keymap.actions == null)
2590 this.shift (this.initial_state);
2599 this.cursor_pos = 0;
2600 this.candidate_show = false;
2601 this.prev_state = null;
2602 this.title = this.initial_state.title;
2603 this.state_preedit = '';
2604 this.state_key_head = 0;
2605 this.state_var_values = {};
2608 this.commit_key_head = 0;
2609 this.key_unhandled = false;
2610 this.unhandled_key = null;
2611 this.changed = MIM.ChangedStatus.None;
2612 this.error_message = '';
2613 this.title = this.initial_state.title;
2616 this.preedit_saved = '';
2617 this.candidate_table.clear ();
2618 this.candidates = null;
2619 this.candidate_show = false;
2620 for (var elt in this.marker_positions)
2621 this.marker_positions[elt] = 0;
2622 this.shift (this.initial_state);
2625 catch_args: new Array (Xex.CatchTag._mimtag, null),
2627 take_actions: function (actions)
2629 var func_progn = this.domain.GetFunc ('progn');
2630 var func_catch = this.domain.GetFunc ('catch');
2631 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2632 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2633 term = term.Eval (this.domain);
2634 return (! term.IsSymbol || term.val != '@mimtag');
2637 GetSurroundingChar: function (pos)
2641 pos += this.range[0];
2647 pos += this.range[1];
2648 if (pos >= this.target.value.length)
2651 return this.target.value.charCodeAt (pos);
2654 DelSurroundText: function (pos)
2659 pos += this.range[0];
2665 text = this.target.value.substring (0, pos);
2666 if (this.range[0] < this.target.value.length)
2667 text += this.target.value.substring (this.range[0]);
2668 this.target.value = text;
2669 this.range[1] -= this.range[0] - pos;
2670 this.range[0] = pos;
2674 pos += this.range[1];
2675 text = this.target.value.substring (0, this.range[1]);
2676 if (pos >= this.target.value.length)
2677 pos = this.target.value.length;
2679 text += this.target.value.substring (pos);
2680 this.target.value = text;
2684 adjust_markers: function (from, to, inserted)
2686 var diff = inserted - (to - from);
2688 for (var name in this.marker_positions)
2690 var pos = this.marker_positions[name];
2694 this.marker_positions[name] += diff;
2695 else if (pos > from)
2696 this.marker_positions[name] = from;
2699 if (this.cursor_pos >= to)
2700 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2701 else if (this.cursor_pos > from)
2702 set_cursor.call (this, 'adjust', from)
2705 preedit_replace: function (from, to, text, candidates)
2707 this.preedit = (this.preedit.substring (0, from)
2708 + text + this.preedit.substring (to));
2709 this.adjust_markers (from, to, text.length);
2710 this.candidate_table.adjust (from, to, text.length);
2712 this.candidate_table.put (from, from + text.length, candidates)
2715 ins: function (text, candidates)
2717 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2718 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2719 this.candidates = candidates;
2722 rep: function (old_text, new_text, candidates)
2724 this.preedit_replace (this.cursor_pos - old_text.length,
2725 this.cursor_pos, new_text, candidates);
2726 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2731 var deleted = pos - this.cursor_pos;
2732 if (pos < this.cursor_pos)
2736 this.DelSurroundText (pos);
2737 deleted = - this.cursor_pos;
2740 if (pos < this.cursor_pos)
2741 this.preedit_replace (pos, this.cursor_pos, '', null);
2745 if (pos > this.preedit.length)
2747 this.DelSurroundText (pos - this.preedit.length);
2748 deleted = this.preedit.length - this.cursor_pos;
2749 pos = this.preedit.length;
2751 if (pos > this.cursor_pos)
2752 this.preedit_replace (this.cursor_pos, pos, '', null);
2755 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2761 this.candidate_show = true;
2762 this.changed |= MIM.ChangedStatus.CandidateShow;
2767 this.candidate_show = false;
2768 this.changed |= MIM.ChangedStatus.CandidateShow;
2771 move: function (pos)
2775 else if (pos > this.preedit.length)
2776 pos = this.preedit.length;
2777 if (pos != this.cursor_pos)
2779 set_cursor.call (this, 'move', pos);
2780 this.changed |= MIM.ChangedStatus.Preedit;
2784 pushback: function (n)
2786 if (n instanceof MIM.KeySeq)
2788 if (this.key_head > 0)
2790 if (this.key_head < this.keys.val.length)
2791 this.keys.val.splice (this.key_head,
2792 this.keys.val.length - this.key_head);
2793 for (var i = 0; i < n.val.length; i++)
2794 this.keys.val.push (n.val[i]);
2800 if (this.key_head < 0)
2807 this.key_head = - n;
2808 if (this.key_head > this.keys.val.length)
2809 this.key_head = this.keys.val.length;
2811 Xex.Log ('0: key head = ' + this.key_head);
2816 if (this.key_head < this.keys.val.length)
2817 this.keys.val.splice (this.key_head, 1);
2822 if (this.preedit.length > 0)
2824 this.candidate_table.clear ();
2825 this.produced += this.preedit;
2826 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2827 this.preedit_saved = '';
2829 this.commit_key_head = this.key_head;
2833 shift: function (state)
2837 if (this.prev_state == null)
2839 state = this.prev_state;
2842 if (state == this.initial_state)
2847 this.keys.val.splice (0, this.key_head);
2848 this.key_head = this.state_key_head = this.commit_key_head = 0;
2849 this.prev_state = null;
2854 if (state != this.state)
2855 this.prev_state = this.state;
2857 if (state != this.state && state.enter_actions)
2858 this.take_actions (state.enter_actions);
2859 if (! this.state || this.state.title != state.title)
2860 this.changed |= MIM.ChangedStatus.StateTitle;
2862 this.keymap = state.keymap;
2863 save_state.call (this);
2866 Filter: function (key)
2870 Xex.Log ("active = false");
2871 this.key_unhandled = true;
2872 this.unhandled_key = key;
2875 if (key.key == '_reload')
2877 this.changed = MIM.ChangedStatus.None;
2879 this.key_unhandled = false;
2880 this.keys.val.push (key);
2882 while (this.key_head < this.keys.val.length)
2884 if (! handle_key.call (this))
2886 if (this.key_head < this.keys.val.length)
2888 this.unhandled_key = this.keys.val[this.key_head];
2889 this.keys.val.splice (this.key_head, this.key_head + 1);
2891 if (this.state_key_head > 0)
2892 this.state_key_head--;
2893 if (this.commit_key_head > 0)
2894 this.commit_key_head--;
2895 this.key_unhandled = true;
2901 this.key_unhandled = true;
2905 if (this.keymap == this.initial_state.keymap)
2908 if (this.commit_key_head > 0)
2910 this.keys.val.splice (0, this.commit_key_head);
2911 this.key_head -= this.commit_key_head;
2912 this.state_key_head -= this.commit_key_head;
2913 this.commit_key_head = 0;
2915 if (this.key_unhandled)
2917 this.keys.val.length = 0;
2918 //this.keys.val.splice (0, this.keys.val.length);
2919 this.key_head = this.state_key_head = this.commit_key_head = 0;
2922 if (false && (this.changed & MIM.ChangedStatus.Candidate))
2924 if (this.candidate_show)
2929 return (! this.key_unhandled
2930 && this.produced.length == 0);
2934 MIM.IC.prototype = proto;
2936 var node = Xex.Load (null, "imlist.xml");
2937 for (node = node.firstChild; node; node = node.nextSibling)
2938 if (node.nodeName == 'input-method')
2940 var lang = null, name = null, extra_id = null, file = null;
2942 for (var n = node.firstChild; n; n = n.nextSibling)
2944 if (n.nodeName == 'language')
2945 lang = n.firstChild.nodeValue;
2946 else if (n.nodeName == 'name')
2947 name = n.firstChild.nodeValue;
2948 else if (n.nodeName == 'extra-id')
2949 extra_id = n.firstChild.nodeValue;
2950 else if (n.nodeName == 'filename')
2951 file = n.firstChild.nodeValue;
2953 if (name && name != 'nil')
2955 if (! MIM.imlist[lang])
2956 MIM.imlist[lang] = {};
2957 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2959 else if (extra_id && extra_id != 'nil')
2961 if (! MIM.imextra[lang])
2962 MIM.imextra[lang] = {};
2963 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2966 if (MIM.imextra.t && MIM.imextra.t.global)
2967 MIM.im_global = MIM.imextra.t.global;
2970 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2971 MIM.im_global.load_status = MIM.LoadStatus.Error;
2977 var keys = new Array ();
2979 keys[0x08] = 'backspace';
2980 keys[0x0D] = 'return';
2981 keys[0x1B] = 'escape';
2982 keys[0x20] = 'space';
2983 keys[0x21] = 'pageup';
2984 keys[0x22] = 'pagedown';
2986 keys[0x24] = 'home';
2987 keys[0x25] = 'left';
2989 keys[0x27] = 'right';
2990 keys[0x28] = 'down';
2991 keys[0x2D] = 'insert';
2992 keys[0x2E] = 'delete';
2993 for (var i = 1; i <= 12; i++)
2994 keys[111 + i] = "f" + i;
2995 keys[0x90] = "numlock";
2996 keys[0xF0] = "capslock";
2999 keyids['U+0008'] = 'backspace';
3000 keyids['U+0009'] = 'tab';
3001 keyids['U+0018'] = 'cancel';
3002 keyids['U+001B'] = 'escape';
3003 keyids['U+0020'] = 'space';
3004 keyids['U+007F'] = 'delete';
3007 modifiers.Shift = 1;
3008 modifiers.Control = 1;
3010 modifiers.AltGraph = 1;
3013 MIM.decode_key_event = function (event)
3015 var key = event.keyIdentifier;
3017 if (key) // keydown event of Chrome
3022 if (event.ctrlKey) mod += 'C-';
3023 if (event.metaKey) mod += 'M-';
3024 if (event.altKey) mod += 'A-';
3025 var keysym = keyids[key];
3028 else if (key.match(/^U\+([0-9A-Z]+)$/))
3030 if (mod.length == 0)
3032 key = String.fromCharCode (parseInt (RegExp.$1, 16));
3035 key = key.toLowerCase ();
3036 if (event.shiftKey) mod += 'S-';
3037 return new MIM.Key (mod + key);
3041 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
3042 : event.charCode ? event.charCode
3046 if (event.type == 'keydown')
3051 if (event.shiftKey) key = "S-" + key ;
3054 key = String.fromCharCode (key);
3056 if (event.altKey) key = "A-" + key ;
3057 if (event.ctrlKey) key = "C-" + key ;
3058 return new MIM.Key (key);
3062 MIM.add_event_listener
3063 = (window.addEventListener
3064 ? function (target, type, listener) {
3065 target.addEventListener (type, listener, false);
3067 : window.attachEvent
3068 ? function (target, type, listener) {
3069 target.attachEvent ('on' + type,
3071 listener.call (target, window.event);
3074 : function (target, type, listener) {
3076 = function (e) { listener.call (target, e || window.event); };
3079 MIM.debug_print = function (event, ic)
3083 if (! MIM.debug_nodes)
3085 MIM.debug_nodes = new Array ();
3086 MIM.debug_nodes['status0'] = document.getElementById ('status0');
3087 MIM.debug_nodes['status1'] = document.getElementById ('status1');
3088 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3089 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3090 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3091 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3092 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3093 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3095 var target = event.target;
3096 var code = event.keyCode;
3097 var ch = event.type == 'keypress' ? event.charCode : 0;
3098 var key = MIM.decode_key_event (event);
3101 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3102 index = (event.type == 'keydown' ? '0' : '1');
3104 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3106 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3107 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3108 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3111 MIM.debug_nodes.keypress.innerHTML = '';
3112 MIM.debug_nodes.status1.innerHTML = '';
3113 MIM.debug_nodes.keymap1.innerHTML = '';
3114 MIM.debug_nodes.preedit1.innerHTML = ''
3118 MIM.get_range = function (target, ic)
3121 if (target.selectionStart != null) // for Mozilla
3123 from = target.selectionStart;
3124 to = target.selectionEnd;
3128 var r = document.selection.createRange ();
3129 var rr = r.duplicate ();
3131 rr.moveToElementText (target);
3132 rr.setEndPoint ('EndToEnd', range);
3133 from = rr.text.length - r.text.length;
3134 to = rr.text.length;
3136 if (ic.range[0] == from && ic.range[1] == to
3137 && (to == from || target.value.substring (from, to) == ic.preedit))
3144 MIM.set_caret = function (target, ic)
3146 if (target.setSelectionRange) // Mozilla
3148 var scrollTop = target.scrollTop;
3149 target.setSelectionRange (ic.range[0], ic.range[1]);
3150 target.scrollTop = scrollTop;
3154 var range = target.createTextRange ();
3155 range.moveStart ('character', ic.range[0]);
3156 range.moveEnd ('character', ic.range[1]);
3161 MIM.update = function (target, ic)
3163 var text = target.value;
3164 target.value = (text.substring (0, ic.range[0])
3167 + text.substring (ic.range[1]));
3168 ic.range[0] += ic.produced.length;
3169 ic.range[1] = ic.range[0] + ic.preedit.length;
3170 MIM.set_caret (target, ic);
3177 padingLeft: 'padding-left',
3178 paddingRight: 'padding-right',
3179 paddingTop: 'padding-top',
3180 paddintBottom: 'padding-bottom',
3181 borderLeftStyle: 'border-left-style',
3182 borderRightStyle: 'border-right-style',
3183 borderTopStyle: 'border-top-style',
3184 borderBottomStyle: 'border-bottom-style',
3185 borderLeftWidth: 'border-left-width',
3186 borderRightWidth: 'border-right-width',
3187 borderTopWidth: 'border-top-width',
3188 borderBottomWidth: 'border-bottom-width',
3189 fontFamily: 'font-family',
3190 fontSize: 'font-size',
3191 lineHeight: 'line-height',
3192 letterSpacing: 'letter-spacing',
3193 wordSpacing: 'word-spacing' };
3195 function copy_style (from, to)
3197 var from_style = getComputedStyle(from,'');
3198 for(var name in style_props)
3199 to.style[name] = from_style.getPropertyValue (style_props[name]);
3200 to.style.left = from.offsetLeft + 'px';
3201 to.style.top = from.offsetTop + 'px';
3202 to.style.width = from.offsetWidth;
3203 to.style.height = from.offsetHeight;
3206 MIM.show = function (ic)
3208 Xex.Log ('show:' + ic.candidates);
3209 if (! ic.candidates)
3211 var target = ic.target;
3216 for (var elm = ic.target.offsetParent; elm; elm = elm.offsetParent)
3218 ic.target_top += elm.offsetTop;
3219 ic.target_left += elm.offsetLeft;
3221 ic.div_node = document.createElement ('div');
3222 copy_style (target, ic.div_node);
3223 ic.div_node.style.visibility="hidden";
3224 ic.div_node.style.position = "absolute";
3225 document.getElementsByTagName ('body')[0].appendChild (ic.div_node);
3226 ic.div_node_first = document.createElement ('span');
3227 ic.div_node_last = document.createElement('span');
3228 ic.div_node_last.innerHTML = '.';
3229 ic.div_node.appendChild (ic.div_node_first);
3230 ic.div_node.appendChild (ic.div_node_last);
3231 ic.can_node = document.createElement ('table');
3232 ic.can_node.style.position = 'absolute';
3233 ic.can_node.style.display = 'none';
3234 ic.can_node.style.backgroundColor = "white";
3235 ic.can_node.style.border = "1px solid black";
3236 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
3239 ic.can_node.innerHTML = '';
3240 var tr = document.createElement ('tr');
3241 ic.can_node.appendChild (tr);
3242 var group = ic.candidates.CurrentGroup ();
3243 for (var i = 1; i < group.length; i++)
3245 var td = document.createElement ('td');
3247 td.innerHTML = i + '.' + group[i];
3248 if (i == group[0] + 1)
3249 td.style.backgroundColor = 'lightblue';
3250 ic.can_node.appendChild (td);
3252 ic.div_node_first.innerHTML = target.value.substr (0, ic.range[0]);
3253 var x = ic.target_left + ic.div_node.lastChild.offsetLeft;
3254 var y = (ic.target_top + ic.div_node.lastChild.offsetTop
3255 + ic.div_node.lastChild.offsetHeight - target.scrollTop + 10);
3256 ic.can_node.style.left = x + 'px';
3257 ic.can_node.style.top = y + 'px';
3258 ic.can_node.style.display = 'block';
3262 MIM.focus_in = function (event)
3264 var target = event.target;
3265 var ic = target.mim_ic;
3266 if (ic.wait_update == true)
3268 Xex.Log ("Focus in " + target.tagName + ' IGNORED');
3269 event.preventDefault ();
3272 Xex.Log ("Focus in " + target.tagName);
3273 ic.Filter (MIM.Key.FocusIn);
3274 MIM.update (target, ic);
3277 MIM.focus_out = function (event)
3279 var target = event.target;
3280 var ic = target.mim_ic;
3281 function reset_update () { ic.wait_update = false; };
3282 if (ic.wait_update == true)
3284 Xex.Log ("Focus out " + target.tagName + ' IGNORED');
3285 event.preventDefault ();
3288 Xex.Log ("Focus out " + target.tagName);
3289 ic.Filter (MIM.Key.FocusOut);
3290 ic.wait_update = true;
3291 MIM.update (target, ic, true);
3292 setTimeout (reset_update, 1000);
3295 MIM.keydown = function (event)
3297 var target = event.target;
3298 if (target.id == 'log')
3300 if (! (target.type == "text" || target.type == "textarea"))
3303 var ic = target.mim_ic;
3304 if (! ic || ic.im != MIM.current)
3306 target.mim_ic = null;
3307 Xex.Log ('creating IC');
3308 ic = new MIM.IC (MIM.current, target);
3309 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3312 MIM.add_event_listener (target, 'focus', MIM.focus_in);
3313 MIM.add_event_listener (target, 'blur', MIM.focus_out);
3314 MIM.get_range (target, ic)
3318 if (! MIM.get_range (target, ic))
3321 //MIM.show (target, ic);
3322 MIM.debug_print (event, ic);
3323 ic.key = MIM.decode_key_event (event);
3326 Xex.Log ("filtering " + ic.key);
3328 var result = ic.Filter (ic.key);
3330 Xex.Log ('Error' + e);
3333 MIM.update (target, ic);
3334 if (! ic.key_unhandled)
3335 event.preventDefault ();
3339 MIM.keypress = function (event)
3341 var target = event.target;
3342 if (target.id == 'log')
3344 if (! (target.type == "text" || target.type == "textarea"))
3347 var ic = target.mim_ic;
3351 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3354 ic.key = MIM.decode_key_event (event);
3361 Xex.Log ("filtering " + ic.key);
3363 var result = ic.Filter (ic.key);
3365 Xex.Log ('Error:' + e);
3368 MIM.update (target, ic);
3369 if (! ic.key_unhandled)
3370 event.preventDefault ();
3372 Xex.Log ("error:" + e);
3373 event.preventDefault ();
3375 MIM.debug_print (event, ic);
3382 var lang_category = {
3384 cs: { name: 'Czech' },
3385 da: { name: 'Danish' },
3386 el: { name: 'Greek' },
3387 en: { name: 'English' },
3388 eo: { name: 'Esperanto' },
3389 fr: { name: 'French' },
3390 grc: { name: 'ClassicGreek' },
3391 hr: { name: 'Croatian' },
3392 hy: { name: 'Armenian' },
3393 ka: { name: 'Georgian' },
3394 kk: { name: 'Kazakh' },
3395 ru: { name: 'Russian' },
3396 sk: { name: 'Slovak' },
3397 sr: { name: 'Serbian' },
3398 sv: { name: 'Swedish' },
3399 yi: { name: 'Yiddish' } },
3401 ar: { name: 'Arabic' },
3402 dv: { name: 'Divehi' },
3403 fa: { name: 'Persian' },
3404 he: { name: 'Hebrew' },
3405 kk: { name: 'Kazakh' },
3406 ps: { name: 'Pushto' },
3407 ug: { name: 'Uighur' },
3408 yi: { name: 'Yiddish' } },
3410 as: { name: 'Assamese' },
3411 bn: { name: 'Bengali' },
3412 bo: { name: 'Tibetan' },
3413 gu: { name: 'Gujarati' },
3414 hi: { name: 'Hindi' },
3415 kn: { name: 'Kannada' },
3416 ks: { name: 'Kashmiri' },
3417 ml: { name: 'Malayalam' },
3418 mr: { name: 'Marathi' },
3419 ne: { name: 'Nepali' },
3420 or: { name: 'Oriya' },
3421 pa: { name: 'Panjabi' },
3422 sa: { name: 'Sanskirit' },
3423 sd: { name: 'Sindhi' },
3424 si: { name: 'Sinhalese' },
3425 ta: { name: 'Tamil' },
3426 te: { name: 'Telugu' },
3427 ur: { name: 'Urdu' } },
3429 cmc: { name: 'Cham' },
3430 km: { name: 'Khmer'},
3431 lo: { name: 'Lao' },
3432 my: { name: 'Burmese' },
3433 tai: { name: 'Tai Viet' },
3434 th: { name: 'Thai' },
3435 vi: { name: 'Vietanamese' } },
3437 ii: { name: 'Yii' },
3438 ja: { name: 'Japanese' },
3439 ko: { name: 'Korean' },
3440 zh: { name: 'Chinese' } },
3442 am: { name: 'Amharic' },
3443 ath: { name: 'Carrier' },
3444 bla: { name: 'Blackfoot' },
3445 cr: { name: 'Cree' },
3446 eo: { name: 'Esperanto' },
3447 iu: { name: 'Inuktitut' },
3448 nsk: { name: 'Naskapi' },
3449 oj: { name: 'Ojibwe' },
3450 t: { name: 'Generic' } }
3453 function categorize_im ()
3455 var cat, lang, list, name;
3456 for (lang in MIM.imlist)
3459 for (cat in lang_category)
3460 if (lang_category[cat][lang])
3462 list = lang_category[cat][lang].list;
3464 list = lang_category[cat][lang].list = {};
3465 for (name in MIM.imlist[lang])
3466 list[name] = MIM.imlist[lang][name];
3469 for (name in MIM.imlist[lang])
3470 Xex.Log ('no category ' + lang + '-' + name);
3479 clearTimeout (destroy_timer);
3480 destroy_timer = null;
3481 var target = document.getElementById ('mim-menu');
3484 for (; last_target && last_target.menu_level;
3485 last_target = last_target.parentLi)
3486 last_target.style.backgroundColor = 'white';
3487 var nodes = target.getElementsByTagName ('ul');
3488 for (var i = 0; i < nodes.length; i++)
3489 nodes[i].style.visibility = 'hidden';
3490 document.getElementsByTagName ('body')[0].removeChild (target);
3494 function destroy_menu () {
3495 if (! destroy_timer)
3496 destroy_timer = setTimeout (destroy, 1000);
3499 function show_submenu (event)
3503 clearTimeout (destroy_timer);
3504 destroy_timer = null;
3506 var target = event.target;
3507 if (! target.menu_level)
3509 if (last_target && target.parentLi != last_target)
3511 last_target.style.backgroundColor = 'white';
3512 if (target.menu_level < last_target.menu_level)
3514 last_target = last_target.parentLi;
3515 last_target.style.backgroundColor = 'white';
3517 var uls = last_target.getElementsByTagName ('ul');
3518 for (var i = 0; i < uls.length; i++)
3519 uls[i].style.visibility = 'hidden';
3521 last_target = target;
3522 target.style.backgroundColor = 'yellow';
3523 if (target.menu_level < 3)
3525 target.lastChild.style.visibility = 'visible';
3526 target.lastChild.style.left = target.clientWidth + 'px';
3528 event.preventDefault ();
3531 function select_im (event)
3533 var target = event.target;
3536 MIM.current = target.im;
3539 event.preventDefault ();
3542 function create_ul (visibility)
3544 var ul = document.createElement ('ul');
3545 ul.style.position = 'absolute';
3546 ul.style.margin = '0px';
3547 ul.style.padding = '0px';
3548 ul.style.border = '1px solid gray';
3549 ul.style.borderBottom = 'none';
3550 ul.style.top = '-1px';
3551 ul.style.backgroundColor = 'white';
3552 ul.style.visibility = visibility;
3556 function create_li (level, text)
3558 var li = document.createElement ('li');
3559 li.style.position = 'relative';
3560 li.style.margin = '0px';
3561 li.style.padding = '1px';
3562 li.style.borderBottom = '1px solid gray';
3563 li.style.top = '0px';
3564 li.style.listStyle = 'none';
3565 li.menu_level = level;
3566 var nobr = document.createElement ('nobr');
3567 nobr.innerHTML = text;
3568 li.appendChild (nobr);
3574 function create_menu (event)
3576 var target = event.target;
3578 if (! ((target.type == "text" || target.type == "textarea")
3579 && event.which == 1 && event.ctrlKey))
3584 menu = create_ul ('visible');
3585 menu.style.fontFamily = 'sans-serif';
3586 menu.style.fontWeight = 'bold';
3587 menu.id = 'mim-menu';
3588 menu.onclick = select_im;
3589 menu.onmouseover = show_submenu;
3590 menu.onmouseout = destroy_menu;
3591 for (var catname in lang_category)
3593 var cat = lang_category[catname];
3594 var li = create_li (1, catname);
3595 var sub = create_ul ('hidden');
3596 for (var langname in cat)
3598 var lang = cat[langname];
3601 var sub_li = create_li (2, lang.name);
3602 sub_li.parentLi = li;
3603 var subsub = create_ul ('hidden');
3604 for (var name in lang.list)
3606 var im = lang.list[name];
3607 var subsub_li = create_li (3, im.name);
3608 subsub_li.parentLi = sub_li;
3610 subsub.appendChild (subsub_li);
3612 sub_li.appendChild (subsub);
3613 sub.appendChild (sub_li);
3615 li.appendChild (sub);
3616 menu.appendChild (li);
3618 lang_category = null;
3620 menu.style.left = (event.clientX - 10) + "px";
3621 menu.style.top = (event.clientY - 10) + "px";
3622 document.getElementsByTagName ('body')[0].appendChild (menu);
3625 MIM.init = function ()
3627 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3628 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3629 MIM.add_event_listener (window, 'mousedown', create_menu);
3630 if (window.location == 'http://localhost/mim/index.html')
3631 MIM.server = 'http://localhost/mim';
3632 MIM.current = MIM.imlist['zh']['tonepy'];
3636 MIM.test = function ()
3638 var im = MIM.imlist['t']['latn-post'];
3639 var ic = new MIM.IC (im, null);
3641 ic.Filter (new MIM.Key ('a'));
3642 ic.Filter (new MIM.Key ("'"));
3645 document.getElementById ('text').value = ic.produced + ic.preedit;
3648 document.getElementById ('text').value
3649 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3651 if (e instanceof Xex.ErrTerm)
3659 MIM.init_debug = function ()
3662 Xex.LogNode = document.getElementById ('log');