1 // -* coding: utf-8; -*
5 Log: function (arg, indent, cont)
9 if (typeof indent == 'number')
12 Xex.LogNode.value = '';
19 if (indent != undefined)
20 for (var i = 0; i <= indent; i++)
23 Xex.LogNode.value += str + arg;
24 Xex.LogNode.scrollTop = Xex.LogNode.scrollHeight;
30 UnknownError: "unknown-error",
31 WrongArgument: "wrong-argument",
33 InvalidInteger: "invalid-integer",
34 TermTypeInvalid: "term-type-invalid",
35 FunctionConflict: "function-conflict",
36 VariableTypeConflict: "variable-type-conflict",
37 VariableRangeConflict: "variable-range-conflict",
38 VariableWrongRange: "variable-wrong-range",
39 VariableWrongValue: "variable-wrong-value",
41 UnknownFunction: "unknown-function",
42 MacroExpansionError: "macro-expansion-error",
43 NoVariableName: "no-variable-name",
44 NoFunctionName: "no-funcion-name",
47 ArithmeticError: "arithmetic-error",
48 WrongType: "wrong-type",
49 IndexOutOfRange: "index-out-of-range",
50 ValueOutOfRange: "value-out-of-range",
51 NoLoopToBreak: "no-loop-to-break",
52 UncaughtThrow: "uncaught-throw"
55 Xex.Variable = function (domain, name, desc, val, range)
64 Xex.Variable.prototype.clone = function ()
66 return new Xex.Variable (this.domain, this.name, this.desc,
67 this.val, this.range);
70 Xex.Variable.prototype.Equals = function (obj)
72 return ((obj instanceof Xex.Variable)
73 && obj.name == this.name);
76 Xex.Variable.prototype.SetValue = function (term)
82 Xex.Function = function (name, with_var, min_args, max_args)
85 this.with_var = with_var;
86 this.min_args = min_args;
87 this.max_args = max_args;
90 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
93 this.with_var = with_var;
94 this.min_args = min_args;
95 this.max_args = max_args;
96 this.builtin = builtin;
99 Xex.Subrountine.prototype.Call = function (domain, vari, args)
101 var newargs = new Array ();
102 for (var i = 0; i < args.length; i++)
104 newargs[i] = args[i].Eval (domain);
105 if (domain.Thrown ())
108 return this.builtin (domain, vari, newargs)
111 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
114 this.with_var = with_var;
115 this.min_args = min_args;
116 this.max_args = max_args;
117 this.builtin = builtin;
120 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
122 return this.builtin (domain, vari, args)
125 Xex.Lambda = function (name, min_args, max_args, args, body)
128 this.min_args = min_args;
129 this.max_args = max_args;
134 Xex.Lambda.prototype.Call = function (domain, vari, args)
136 var current = domain.bindings;
137 var result = Xex.Zero;
138 var limit = max_args >= 0 ? args.length : args.length - 1;
142 for (i = 0; i < limit; i++)
144 result = args[i].Eval (domain);
145 if (domain.Thrown ())
147 domain.Bind (this.args[i], result);
151 var list = new Array ();
152 for (i = 0; i < args[limit].length; i++)
154 result = args[limit].Eval (domain);
155 if (domain.Thrown ())
159 domain.Bind (this.args[limit], list);
162 domain.Catch (Xex.CatchTag.Return);
163 for (var term in this.body)
165 result = term.Eval (domain);
166 if (domain.Thrown ())
173 domain.UnboundTo (current);
178 Xex.Macro = function (name, min_args, max_args, args, body)
181 this.min_args = min_args;
182 this.max_args = max_args;
187 Xex.Macro.prototype.Call = function (domain, vari, args)
189 var current = domain.bindings;
190 var result = Xex.Zero;
194 for (i = 0; i < args.length; i++)
195 domain.Bind (this.args[i], args[i]);
197 domain.Catch (Xex.CatchTag.Return);
198 for (var i in this.body)
200 result = this.body[i].Eval (domain);
201 if (domain.Thrown ())
208 domain.UnboundTo (current);
213 Xex.Bindings = function (vari)
216 this.old_value = vari.val;
219 Xex.Bindings.prototype.UnboundTo = function (boundary)
221 for (var b = this; b != boundary; b = b.next)
222 b.vari.val = b.old_value;
226 Xex.Bind = function (bindings, vari, val)
228 var b = new Xex.Bindings (vari);
239 Xex.Domain = function (name, parent, context)
242 this.context = context;
245 if (name != 'basic' && ! parent)
246 parent = Xex.BasicDomain
247 this.parent = parent;
254 for (elt in parent.termtypes)
255 this.termtypes[elt] = parent.termtypes[elt];
256 for (elt in parent.functions)
257 this.functions[elt] = parent.functions[elt];
258 for (elt in parent.variables)
260 var vari = parent.variables[elt];
261 this.variables[elt] = new Xex.Variable (this, vari.name, vari.desc,
262 vari.val, vari.range);
266 this.call_stack = new Array ();
267 this.bindings = null;
268 this.catch_stack = new Array ();
269 this.catch_count = 0;
273 Xex.Domain.prototype = {
274 CallStackCount: function () { return this.call_stack.length; },
275 CallStackPush: function (term) { this.call_stack.push (term); },
276 CallStackPop: function () { this.call_stack.pop (); },
277 Bind: function (vari, val)
279 this.bindings = Xex.Bind (this.bindings, vari, val);
281 UnboundTo: function (boundary)
284 this.bindings = this.bindings.UnboundTo (boundary);
286 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
289 this.catch_stack.pop ();
290 if (this.catch_count > this.catch_stack.length)
295 if (this.catch_count < this.catch_stack.length)
297 this.caught = (this.catch_count == this.catch_stack.length - 1);
303 ThrowReturn: function ()
305 for (var i = this.catch_stack.length - 1; i >= 0; i--)
308 if (this.catch_stack[i] == Xex.CatchTag.Return)
312 ThrowBreak: function ()
314 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
315 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
316 "No surrounding loop to break");
319 ThrowSymbol: function (tag)
321 var i = this.catch_count;
322 for (var j = this.catch_stack.length - 1; j >= 0; j--)
325 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
327 this.catch_count = i;
331 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
332 "No corresponding catch: " + tag);
334 DefType: function (obj)
337 if (this.termtypes[type])
338 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
339 "Already defined: " + type);
340 if (this.functions[type])
341 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
342 "Already defined as a funciton or a macro: "
344 this.termtypes[type] = obj.Parser;
346 DefSubr: function (builtin, name, with_var, min_args, max_args)
348 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
351 DefSpecial: function (builtin, name, with_var, min_args, max_args)
353 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
356 Defun: function (name, min_args, max_args, args, body)
358 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
361 DefunByFunc: function (func) { this.functions[func.name] = func; },
362 Defmacro: function (name, min_args, max_args, args, body)
364 this.functions[name] = new Xex.Macro (name, min_args, max_args,
367 DefAlias: function (alias, fname)
369 var func = this.functions[fname];
372 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
373 if (this.termtypes[alias])
374 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
375 "Already defined as a term type: " + alias);
376 if (this.functions[alias])
377 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
378 "Already defined as a function: " + alias);
379 this.functions[alias] = func;
381 Defvar: function (name, desc, val, range)
383 var vari = new Xex.Variable (this, name, desc, val, range);
384 this.variables[name] = vari;
387 GetFunc: function (name)
389 var func = this.functions[name];
391 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
392 "Unknown function: " + name);
395 CopyFunc: function (domain, name)
397 var func = this.functions[name];
398 domain.DefunByFunc (func);
401 CopyFuncAll: function (domain)
403 for (var elt in this.functions)
404 domain.DefunByFunc (this.functions[elt]);
406 GetVarCreate: function (name)
408 var vari = this.variables[name];
410 vari = this.variables[name] = new Xex.Variable (this, name, null,
414 GetVar: function (name) { return this.variables[name]; },
415 SaveValues: function ()
418 for (var elt in this.variables)
419 values[elt] = this.variables[elt].val.Clone ();
422 RestoreValues: function (values)
427 var vari = this.variables[name];
428 vari.val = values[name];
433 Xex.Term = function (type) { this.type = type; }
434 Xex.Term.prototype = {
435 IsTrue: function () { return true; },
436 Eval: function (domain) { return this.Clone (); },
437 Clone: function (domain) { return this; },
438 Equals: function (obj)
440 return (this.type == obj.type
442 && obj.val == this.val);
444 Matches: function (obj) { return this.Equals (obj); },
445 toString: function ()
447 if (this.val != undefined)
448 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
449 return '<' + this.type + '/>';
451 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
452 "Not an integer"); },
453 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
457 Node.prototype.firstElement = function ()
459 for (var n = this.firstChild; n; n = n.nextSibling)
465 Node.prototype.nextElement = function ()
467 for (var n = this.nextSibling; n; n = n.nextSibling)
474 function parse_defvar (domain, node)
476 var name = node.attributes['vname'].nodeValue;
478 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
479 var vari = domain.variables[name];
480 var desc, val = null, range;
483 desc = vari.description;
487 node = node.firstElement ();
488 if (node && node.nodeName == 'description')
490 desc = node.firstChild.nodeValue;
491 node = node.nextElement ();
495 val = Xex.Term.Parse (domain, node);
496 node = node.nextElement ();
497 if (node && node.nodeName == 'possible-values')
498 for (node = node.firstElement (); node; node = node.nextElement ())
501 if (node.nodeName == 'range')
504 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
505 'Range not allowed for ' + name);
507 for (var n = node.firstElement (); n; n = n.nextElement ())
509 var v = Xex.Term.Parse (domain, n);
511 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
512 'Invalid range value: ' + val);
518 pval = Xex.Term.Parse (domain, node);
519 if (val.type != pval.type)
520 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
521 'Invalid possible value: ' + pval);
524 range = new Array ();
530 domain.Defvar (name, desc, val, range);
534 function parse_defun_head (domain, node)
536 var name = node.attributes['fname'].nodeValue;
538 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
539 var args = new Array ();
540 var nfixed = 0, noptional = 0, nrest = 0;
542 node = node.firstElement ();
543 if (node && node.nodeName == 'args')
546 for (n = n.firstElement (); n; n = n.nextElement ())
548 if (n.nodeName == 'fixed')
550 else if (n.nodeName == 'optional')
552 else if (n.nodeName == 'rest')
555 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
558 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
559 for (n = node.firstElement (); n; n = n.nextElement ())
560 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
562 args.min_args = nfixed;
563 args.max_args = nrest == 0 ? nfixed + noptional : -1;
565 if (node.nodeName == 'defun')
566 domain.Defun (name, args, null);
568 domain.Defmacro (name, args, null);
572 function parse_defun_body (domain, node)
574 var name = node.attributes['fname'].nodeValue;
575 var func = domain.GetFunc (name);
577 for (node = node.firstElement (); node; node = node.nextElement ())
578 if (node.nodeName != 'description' && node.nodeName != 'args')
580 body = Xex.Term.Parse (domain, node, null);
584 Xex.Term.Parse = function (domain, node, stop)
586 if (arguments.length == 2)
588 var name = node.nodeName;
589 var parser = domain.termtypes[name];
592 return parser (domain, node);
593 if (name == 'defun' || name == 'defmacro')
595 name = parse_defun_head (domain, node);
596 parse_defun_body (domain, node);
597 return new Xex.StrTerm (name);
599 if (name == 'defvar')
601 name = parse_defvar (domain, node);
602 return new Xex.StrTerm (name);
604 return new Xex.Funcall.prototype.Parser (domain, node);
606 for (var n = node; n && n != stop; n = n.nextElement ())
607 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
608 parse_defun_head (domain, n);
610 for (var n = node; n && n != stop; n = n.nextElement ())
612 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
613 parse_defun_body (domain, n);
614 else if (n.nodeName == 'defvar')
615 parse_defvar (domain, n);
619 terms = new Array ();
620 terms.push (Xex.Term.Parse (domain, n));
627 Xex.Varref = function (vname)
633 var proto = new Xex.Term ('varref');
635 proto.Clone = function () { return new Xex.Varref (this.val); }
636 proto.Eval = function (domain)
638 var vari = domain.GetVarCreate (this.val);
639 Xex.Log (this.ToString () + '=>' + vari.val, domain.depth);
643 proto.Parser = function (domain, node)
645 return new Xex.Varref (node.attributes['vname'].nodeValue);
648 proto.ToString = function ()
650 return '<varref vname="' + this.val + '"/>';
653 Xex.Varref.prototype = proto;
656 var null_args = new Array ();
658 Xex.Funcall = function (func, vname, args)
662 this.args = args || null_args;
666 var proto = new Xex.Term ('funcall');
668 proto.Parser = function (domain, node)
670 var fname = node.nodeName;
673 if (fname == 'funcall')
674 fname = node.attributes['fname'].nodeValue;
675 var func = domain.GetFunc (fname);
677 attr = node.attributes['vname'];
678 vname = attr != undefined ? attr.nodeValue : null;
679 var args = Xex.Term.Parse (domain, node.firstElement (), null);
680 return new Xex.Funcall (func, vname, args);
683 proto.New = function (domain, fname, vname, args)
685 var func = domain.GetFunc (fname);
686 var funcall = new Xex.Funcall (func, vname, args);
687 if (func instanceof Xex.Macro)
688 funcall = funcall.Eval (domain);
692 proto.Eval = function (domain)
694 Xex.Log (this, domain.depth);
697 vari = domain.GetVarCreate (this.vname);
701 result = this.func.Call (domain, vari, this.args);
703 Xex.Log (' => ' + result, --domain.depth,
704 this.func instanceof Xex.Subrountine);
709 proto.Clone = function ()
711 return new Xex.Funcall (this.func, this.vari, this.args);
714 proto.Equals = function (obj)
716 return (obj.type == 'funcall'
717 && obj.func == this.func
718 && obj.vari.Equals (this.vari)
719 && obj.args.length == this.func.length);
722 proto.toString = function ()
725 var len = this.args.length;
726 var str = '<' + this.func.name;
728 str += ' vname="' + this.vari.name + '"';
731 if (this.func instanceof Xex.Subrountine)
732 for (var i = 0; i < len; i++)
733 arglist += this.args[i].toString ();
735 for (var i = 0; i < len; i++)
737 return str + '>' + arglist + '</' + this.func.name + '>';
740 Xex.Funcall.prototype = proto;
743 Xex.ErrTerm = function (ename, message, stack)
746 this.message = message;
751 var proto = new Xex.Term ('error');
753 proto.IsError = true;
755 proto.Parser = function (domain, node)
757 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
758 node.innerText, false);
761 proto.CallStack = function () { return stack; }
763 proto.SetCallStack = function (value) { statck = value; }
765 proto.Clone = function ()
767 return new Xex.ErrTerm (ename, message, false);
770 proto.Equals = function (obj)
773 && obj.ename == ename && obj.message == message
774 && (obj.stack ? (stack && stack.length == obj.stack.length)
778 proto.Matches = function (obj)
780 return (obj.IsError && obj.ename == ename);
783 proto.toString = function ()
785 return '<error ename="' + this.ename + '">' + this.message + '</error>';
788 Xex.ErrTerm.prototype = proto;
791 Xex.IntTerm = function (num) { this.val = num; };
793 var proto = new Xex.Term ('integer');
795 proto.Intval = function () { return this.val; };
796 proto.IsTrue = function () { return this.val != 0; }
797 proto.Clone = function () { return new Xex.IntTerm (this.val); }
798 proto.Parser = function (domain, node)
800 var str = node.firstChild.nodeValue;
802 if (str.charAt (0) == '?' && str.length == 2)
803 return new Xex.IntTerm (str.charCodeAt (1));
804 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
806 Xex.IntTerm.prototype = proto;
809 Xex.StrTerm = function (str) { this.val = str; };
811 var proto = new Xex.Term ('string');
813 proto.Strval = function () { return this.val; };
814 proto.IsTrue = function () { return this.val.length > 0; }
815 proto.Clone = function () { return new Xex.StrTerm (this.val); }
816 proto.Parser = function (domain, node)
818 return new Xex.StrTerm (node.firstChild ? node.firstChild.nodeValue : '');
820 Xex.StrTerm.prototype = proto;
823 Xex.SymTerm = function (str) { this.val = str; };
825 var proto = new Xex.Term ('symbol');
826 proto.IsSymbol = true;
827 proto.IsTrue = function () { return this.val != 'nil'; }
828 proto.Clone = function () { return new Xex.SymTerm (this.val); }
829 proto.Parser = function (domain, node)
831 return new Xex.SymTerm (node.firstChild.nodeValue);
833 Xex.SymTerm.prototype = proto;
836 Xex.LstTerm = function (list) { this.val = list; };
838 var proto = new Xex.Term ('list');
840 proto.IsTrue = function () { return this.val.length > 0; }
841 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
843 proto.Equals = function (obj)
845 if (obj.type != 'list' || obj.val.length != this.val.length)
847 var i, len = this.val.length;
848 for (i = 0; i < len; i++)
849 if (! this.val[i].Equals (obj.val[i]))
854 proto.Parser = function (domain, node)
856 var list = Xex.Term.Parse (domain, node.firstElement (), null);
857 return new Xex.LstTerm (list);
860 proto.toString = function ()
862 var len = this.val.length;
867 for (var i = 0; i < len; i++)
868 str += this.val[i].toString ();
869 return str + '</list>';
871 Xex.LstTerm.prototype = proto;
875 var basic = new Xex.Domain ('basic', null, null);
877 function Fset (domain, vari, args)
880 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
881 'No variable name to set');
882 vari.SetValue (args[0]);
886 function Fnot (domain, vari, args)
888 return (args[0].IsTrue () ? Xex.Zero : Xex.One);
891 function maybe_set_intvar (vari, n)
893 var term = new Xex.IntTerm (n);
895 vari.SetValue (term);
899 function Fadd (domain, vari, args)
901 var n = vari ? vari.val.Intval () : 0;
902 var len = args.length;
904 for (var i = 0; i < len; i++)
905 n += args[i].Intval ();
906 return maybe_set_intvar (vari, n);
909 function Fmul (domain, vari, args)
911 var n = vari ? vari.val.Intval () : 1;
912 for (var i = 0; i < args.length; i++)
914 return maybe_set_intvar (vari, n);
917 function Fsub (domain, vari, args)
923 n = args[0].Intval ();
928 n = vari.val.Intval ();
931 while (i < args.length)
932 n -= args[i++].Intval ();
933 return maybe_set_intvar (vari, n);
936 function Fdiv (domain, vari, args)
942 n = args[0].Intval ();
947 n = vari.val.Intval ();
950 while (i < args.length)
951 n /= args[i++].Intval ();
952 return maybe_set_intvar (vari, n);
955 function Fmod (domain, vari, args)
957 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
960 function Flogior (domain, vari, args)
962 var n = vari == null ? 0 : vari.val;
963 for (var i = 0; i < args.length; i++)
965 return maybe_set_intvar (vari, n);
968 function Flogand (domain, vari, args)
973 Xex.Log ("logand arg args[0]" + args[0]);
974 n = args[0].Intval ()
979 Xex.Log ("logand arg var " + vari);
980 n = vari.val.Intval ();
983 while (n > 0 && i < args.length)
985 Xex.Log ("logand arg " + args[i]);
988 return maybe_set_intvar (vari, n);
991 function Flsh (domain, vari, args)
993 return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
996 function Frsh (domain, vari, args)
998 return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
1001 function Fand (domain, vari, args)
1003 var len = args.length;
1004 for (var i = 0; i < len; i++)
1006 var result = args[i].Eval (domain);
1007 if (domain.Thrown ())
1009 if (! result.IsTrue ())
1015 function For (domain, vari, args)
1017 var len = args.length;
1018 for (var i = 0; i < len; i++)
1020 var result = args[i].Eval (domain);
1021 if (domain.Thrown ())
1023 if (result.IsTrue ())
1029 function Feq (domain, vari, args)
1031 for (var i = 1; i < args.length; i++)
1032 if (! args[i - 1].Equals (args[i]))
1037 function Fnoteq (domain, vari, args)
1039 return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1042 function Flt (domain, vari, args)
1044 var n = args[0].Intval ();
1046 for (var i = 1; i < args.length; i++)
1048 var n1 = args[i].Intval ();
1056 function Fle (domain, vari, args)
1058 var n = args[0].Intval ();
1059 for (var i = 1; i < args.length; i++)
1061 var n1 = args[i].Intval ();
1069 function Fgt (domain, vari, args)
1071 var n = args[0].Intval ();
1072 for (var i = 1; i < args.length; i++)
1074 var n1 = args[i].Intval ();
1082 function Fge (domain, vari, args)
1084 var n = args[0].Intval ();
1085 for (var i = 1; i < args.Length; i++)
1087 var n1 = args[i].Intval ();
1095 function Fprogn (domain, vari, args)
1097 var result = Xex.One;
1098 var len = args.length;
1100 for (var i = 0; i < len; i++)
1102 result = args[i].Eval (domain);
1103 if (domain.Thrown ())
1109 function Fif (domain, vari, args)
1111 var result = args[0].Eval (domain);
1113 if (domain.Thrown ())
1115 if (result.IsTrue ())
1116 return args[1].Eval (domain);
1117 if (args.length == 2)
1119 return args[2].Eval (domain);
1122 function Fcond (domain, vari, args)
1124 for (var i = 0; i < args.length; i++)
1127 var result = list.val[0].Eval (domain);
1128 if (result.IsTrue ())
1130 for (var j = 1; j < list.val.length; j++)
1133 result = list.val[j].Eval (domain);
1135 if (domain.Thrown ())
1144 function eval_terms (domain, terms, idx)
1146 var result = Xex.Zero;
1147 domain.caught = false;
1148 for (var i = idx; i < terms.length; i++)
1150 result = terms[i].Eval (domain);
1151 if (domain.Thrown ())
1157 function Fcatch (domain, vari, args)
1162 if (args[0].IsError)
1165 result = eval_terms (domain, args, 1);
1167 if (e instanceof Xex.ErrTerm)
1169 if (! args[0].Matches (e))
1177 else if (args[0].IsSymbol)
1180 domain.Catch (args[0].val);
1181 result = eval_terms (domain, args, 1);
1185 vari.SetValue (result);
1193 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1194 "Not a symbol nor an error: " + args[0]);
1197 function Fthrow (domain, vari, args)
1199 if (args[0].IsSymbl)
1201 domain.ThrowSymbol (args[0]);
1202 return (args[args.length - 1]);
1204 if (args[0].IsError)
1208 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1209 "Not a symbol nor an error:" + args[0]);
1212 Xex.BasicDomain = basic;
1214 basic.DefSubr (Fset, "set", true, 1, 1);
1215 basic.DefAlias ("=", "set");
1216 basic.DefSubr (Fnot, "not", false, 1, 1);
1217 basic.DefAlias ("!", "not");
1218 basic.DefSubr (Fadd, "add", true, 1, -1);
1219 basic.DefSubr (Fmul, "mul", true, 1, -1);
1220 basic.DefAlias ("*", "mul");
1221 basic.DefSubr (Fsub, "sub", true, 1, -1);
1222 basic.DefAlias ("-", "sub");
1223 basic.DefSubr (Fdiv, "div", true, 1, -1);
1224 basic.DefAlias ("/", "div");
1225 basic.DefSubr (Fmod, "mod", true, 1, 2);
1226 basic.DefAlias ("%", "mod");
1227 basic.DefSubr (Flogior, "logior", true, 1, -1);
1228 basic.DefAlias ('|', "logior");
1229 basic.DefSubr (Flogand, "logand", true, 1, -1);
1230 basic.DefAlias ("&", "logand");
1231 basic.DefSubr (Flsh, "lsh", true, 1, 2);
1232 basic.DefAlias ("<<", "lsh");
1233 basic.DefSubr (Frsh, "rsh", true, 1, 2);
1234 basic.DefAlias (">>", "rsh");
1235 basic.DefSubr (Feq, "eq", false, 2, -1);
1236 basic.DefAlias ("==", "eq");
1237 basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1238 basic.DefAlias ("!=", "noteq");
1239 basic.DefSubr (Flt, "lt", false, 2, -1);
1240 basic.DefAlias ("<", "lt");
1241 basic.DefSubr (Fle, "le", false, 2, -1);
1242 basic.DefAlias ("<=", "le");
1243 basic.DefSubr (Fgt, "gt", false, 2, -1);
1244 basic.DefAlias (">", "gt");
1245 basic.DefSubr (Fge, "ge", false, 2, -1);
1246 basic.DefAlias (">=", "ge");
1247 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1249 //basic.DefSubr (Fappend, "append", true, 0, -1);
1250 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1251 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1252 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1253 //basic.DefSubr (Fins, "ins", true, 2, 2);
1254 //basic.DefSubr (Fdel, "del", true, 2, 2);
1255 //basic.DefSubr (Feval, "eval", false, 1, 1);
1256 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1257 //basic.DefSubr (Freturn, "return", false, 0, 1);
1258 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1260 basic.DefSpecial (Fand, "and", false, 1, -1);
1261 basic.DefAlias ("&&", "and");
1262 basic.DefSpecial (For, "or", false, 1, -1);
1263 basic.DefAlias ("||", "or");
1264 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1265 basic.DefAlias ("expr", "progn");
1266 basic.DefSpecial (Fif, "if", false, 2, 3);
1267 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1268 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1269 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1270 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1271 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1272 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1273 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1274 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1276 basic.DefType (Xex.Funcall.prototype);
1277 basic.DefType (Xex.Varref.prototype);
1278 basic.DefType (Xex.ErrTerm.prototype);
1279 basic.DefType (Xex.IntTerm.prototype);
1280 basic.DefType (Xex.StrTerm.prototype);
1281 basic.DefType (Xex.SymTerm.prototype);
1282 basic.DefType (Xex.LstTerm.prototype);
1286 Xex.Zero = new Xex.IntTerm (0);
1287 Xex.One = new Xex.IntTerm (1);
1288 Xex.nil = new Xex.SymTerm ('nil');
1290 Xex.Load = function (server, file)
1292 var obj = new XMLHttpRequest ();
1293 var url = server ? server + '/' + file : file;
1294 obj.open ('GET', url, false);
1295 obj.overrideMimeType ('text/xml');
1297 return obj.responseXML.firstChild;
1301 // URL of the input method server.
1302 server: "http://www.m17n.org/common/mim-js",
1303 // Boolean flag to tell if MIM is active or not.
1305 // Boolean flag to tell if MIM is running in debug mode or not.
1307 // List of main input methods.
1309 // List of extra input methods;
1311 // Global input method data
1313 // Currently selected input method.
1317 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1324 CandidateIndex:0x10,
1326 Preedit: 0x06, // PreeditText | CursorPos
1327 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1349 ParseError: "parse-error"
1354 var keysyms = new Array ();
1355 keysyms["bs"] = "backspace";
1356 keysyms["lf"] = "linefeed";
1357 keysyms["cr"] = keysyms["enter"] = "return";
1358 keysyms["esc"] = "escape";
1359 keysyms["spc"] = "space";
1360 keysyms["del"] = "delete";
1362 function decode_keysym (str) {
1363 if (str.length == 1)
1365 var parts = str.split ("-");
1366 var len = parts.length, i;
1367 var has_modifier = len > 1;
1369 for (i = 0; i < len - 1; i++)
1370 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1372 var key = parts[len - 1];
1375 key = keysyms[key.toLowerCase ()];
1381 for (i = 1; i < len - 1; i++)
1382 str += '-' + parts[i];
1391 parts = new Array ();
1398 MIM.Key = function (val)
1401 if (val instanceof Xex.Term)
1403 else if (typeof val == 'string' || val instanceof String)
1405 this.key = decode_keysym (val);
1407 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1408 if (this.key instanceof Array)
1410 this.key = this.key[0];
1411 this.has_modifier = true;
1414 else if (typeof val == 'number' || val instanceof Number)
1415 this.key = String.fromCharCode (val);
1417 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1420 MIM.Key.prototype.toString = function () { return this.key; };
1422 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1423 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1424 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1428 MIM.KeySeq = function (seq)
1430 this.val = new Array ();
1436 var len = seq.val.length;
1437 for (var i = 0; i < len; i++)
1439 var v = seq.val[i], key;
1440 if (v.type == 'symbol' || v.type == 'string')
1441 key = new MIM.Key (v);
1442 else if (v.type == 'integer')
1443 key = new MIM.Key (v.val);
1445 throw new Xex.ErrTerm (MIM.Error.ParseError,
1446 "Invalid key: " + v);
1447 this.val.push (key);
1448 if (key.has_modifier)
1449 this.has_modifier = true;
1454 var len = seq.val.length;
1455 for (var i = 0; i < len; i++)
1456 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1459 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1463 var proto = new Xex.Term ('keyseq');
1464 proto.Clone = function () { return this; }
1465 proto.Parser = function (domain, node)
1467 var seq = new Array ();
1468 for (node = node.firstChild; node; node = node.nextSibling)
1469 if (node.nodeType == 1)
1471 var term = Xex.Term.Parse (domain, node);
1472 return new MIM.KeySeq (term);
1474 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1476 proto.toString = function ()
1478 var len = this.val.length;
1482 var str = '<keyseq>';
1483 for (var i = 0; i < len; i++)
1487 else if (this.has_modifier)
1489 str += this.val[i].toString ();
1491 return str + '</keyseq>';
1494 MIM.KeySeq.prototype = proto;
1498 MIM.Marker = function () { }
1499 MIM.Marker.prototype = new Xex.Term ('marker');
1500 MIM.Marker.prototype.CharAt = function (ic)
1502 var p = this.Position (ic);
1503 if (p < 0 || p >= ic.preedit.length)
1505 return ic.preedit.charCodeAt (p);
1508 MIM.FloatingMarker = function (name) { this.val = name; };
1509 var proto = new MIM.Marker ();
1510 MIM.FloatingMarker.prototype = proto;
1511 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1512 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1514 MIM.PredefinedMarker = function (name) { this.val = name; }
1515 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1516 MIM.PredefinedMarker.prototype.Position = function (ic)
1518 if (typeof this.pos == 'number')
1520 return this.pos (ic);
1523 var predefined = { }
1525 function def_predefined (name, position)
1527 predefined[name] = new MIM.PredefinedMarker (name);
1528 predefined[name].pos = position;
1531 def_predefined ('@<', 0);
1532 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1533 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1534 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1535 def_predefined ('@[', function (ic) {
1536 if (ic.cursor_pos > 0)
1538 var pos = ic.cursor_pos;
1539 return ic.preedit.FindProp ('candidates', pos - 1).from;
1543 def_predefined ('@]', function (ic) {
1544 if (ic.cursor_pos < ic.preedit.length - 1)
1546 var pos = ic.cursor_pos;
1547 return ic.preedit.FindProp ('candidates', pos).to;
1549 return ic.preedit.length;
1551 for (var i = 0; i < 10; i++)
1552 def_predefined ("@" + i, i);
1553 predefined['@first'] = predefined['@<'];
1554 predefined['@last'] = predefined['@>'];
1555 predefined['@previous'] = predefined['@-'];
1556 predefined['@next'] = predefined['@+'];
1557 predefined['@previous-candidate-change'] = predefined['@['];
1558 predefined['@next-candidate-change'] = predefined['@]'];
1560 MIM.SurroundMarker = function (name)
1563 this.distance = parseInt (name.slice (1));
1564 if (isNaN (this.distance))
1565 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1567 MIM.SurroundMarker.prototype = new MIM.Marker ();
1568 MIM.SurroundMarker.prototype.Position = function (ic)
1570 return ic.cursor_pos + this.distance;
1572 MIM.SurroundMarker.prototype.CharAt = function (ic)
1574 if (this.val == '@-0')
1576 var p = this.Position (ic);
1578 return ic.GetSurroundingChar (p);
1579 else if (p >= ic.preedit.length)
1580 return ic.GetSurroundingChar (p - ic.preedit.length);
1581 return ic.preedit.charCodeAt (p);
1584 MIM.Marker.prototype.Parser = function (domain, node)
1586 var name = node.firstChild.nodeValue;
1587 if (name.charAt (0) == '@')
1589 var n = predefined[name];
1592 if (name.charAt (1) == '-' || name.charAt (1) == '+')
1593 return new MIM.SurroundMarker (name);
1594 throw new Xex.ErrTerm (MIM.Error.ParseError,
1595 "Invalid marker: " + name);
1597 return new MIM.FloatingMarker (name);;
1601 MIM.Selector = function (name)
1605 MIM.Selector.prototype = new Xex.Term ('selector');
1609 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1610 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1611 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1612 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1613 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1614 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
1615 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
1617 MIM.Selector.prototype.Parser = function (domain, node)
1619 var name = node.firstChild.nodeValue;
1620 var s = selectors[name];
1622 throw new Xex.ErrTerm (MIM.Error.ParseError,
1623 "Invalid selector: " + name);
1628 MIM.Rule = function (keyseq, actions)
1630 this.keyseq = keyseq;
1632 this.actions = actions;
1634 MIM.Rule.prototype = new Xex.Term ('rule');
1635 MIM.Rule.prototype.Parser = function (domain, node)
1638 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1640 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1641 var keyseq = Xex.Term.Parse (domain, n);
1642 if (keyseq.type != 'keyseq')
1643 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1645 n = n.nextElement ();
1647 actions = Xex.Term.Parse (domain, n, null);
1648 return new MIM.Rule (keyseq, actions);
1650 MIM.Rule.prototype.toString = function ()
1655 MIM.Map = function (name)
1658 this.rules = new Array ();
1662 var proto = new Xex.Term ('map');
1664 proto.Parser = function (domain, node)
1666 var name = node.attributes['mname'].nodeValue;
1668 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1669 var map = new MIM.Map (name);
1670 for (var n = node.firstChild; n; n = n.nextSibling)
1671 if (n.nodeType == 1)
1672 map.rules.push (Xex.Term.Parse (domain, n));
1676 proto.toString = function ()
1678 var str = '<map mname="' + this.name + '">';
1679 var len = this.rules.length;
1680 for (i = 0; i < len; i++)
1681 str += this.rules[i];
1682 return str + '</map>';
1685 MIM.Map.prototype = proto;
1688 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1690 MIM.Action = function (domain, terms)
1692 var args = new Array ();
1693 args.push (Xex.CatchTag_.mimtag);
1694 for (var i = 0; i < terms.length; i++)
1695 args.push (terms[i]);
1696 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1699 MIM.Action.prototype.Run = function (domain)
1701 var result = this.action.Eval (domain);
1702 if (result.type == 'error')
1704 domain.context.Error = result.toString ();
1707 return (result != Xex.CatchTag._mimtag);
1710 MIM.Keymap = function ()
1713 this.submaps = null;
1719 function add_rule (keymap, rule, branch_actions)
1721 var keyseq = rule.keyseq;
1722 var len = keyseq.val.length;
1725 for (var i = 0; i < len; i++)
1727 var key = keyseq.val[i];
1731 if (! keymap.submaps)
1732 keymap.submaps = {};
1734 sub = keymap.submaps[key.key];
1736 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1740 keymap.map_actions = rule.actions;
1742 keymap.branch_actions = branch_actions;
1745 proto.Add = function (map, branch_actions)
1747 var rules = map.rules;
1748 var len = rules.length;
1750 for (var i = 0; i < len; i++)
1751 add_rule (this, rules[i], branch_actions);
1753 proto.Lookup = function (keys, index)
1757 if (index < keys.val.length && this.submaps
1758 && ! keys.val[index])
1760 Xex.Log ('invalid key at ' + index);
1761 throw 'invalid key';
1764 if (index < keys.val.length && this.submaps
1765 && (sub = this.submaps[keys.val[index].key]))
1768 return sub.Lookup (keys, index);
1770 return { map: this, index: index };
1773 MIM.Keymap.prototype = proto;
1776 MIM.State = function (name)
1779 this.keymap = new MIM.Keymap ();
1783 var proto = new Xex.Term ('state');
1785 proto.Parser = function (domain, node)
1787 var map_list = domain.map_list;
1788 var name = node.attributes['sname'].nodeValue;
1790 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1791 var state = new MIM.State (name);
1792 for (node = node.firstElement (); node; node = node.nextElement ())
1794 if (node.nodeName == 'title')
1795 state.title = node.firstChild.nodeValue;
1798 var n = node.firstElement ();
1799 var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
1800 if (node.nodeName == 'branch')
1801 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1803 else if (node.nodeName == 'state-hook')
1804 state.enter_actions = branch_actions;
1805 else if (node.nodeName == 'catch-all-branch')
1806 state.fallback_actions = branch_actions;
1812 proto.toString = function ()
1814 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1817 MIM.State.prototype = proto;
1821 function Block (index, term)
1825 this.Data = term.val;
1826 else if (term.IsList)
1828 this.Data = new Array ();
1829 for (var i = 0; i < term.val.length; i++)
1830 this.Data.push (term.val[i].val);
1834 Block.prototype.Count = function () { return this.Data.length; }
1835 Block.prototype.get = function (i)
1837 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1840 MIM.Candidates = function (ic, candidates, column)
1843 this.column = column;
1847 this.blocks = new Array ();
1849 for (var i = 0; i < candidates.length; i++)
1851 var block = new Block (this.total, candidates[i]);
1852 this.blocks.push (block);
1853 this.total += block.Count ();
1859 return (this.column > 0 ? this.index % this.column
1860 : this.index - this.blocks[this.row].Index);
1863 function prev_group ()
1865 var col = get_col.call (this);
1867 if (this.column > 0)
1869 this.index -= this.column;
1870 if (this.index >= 0)
1871 nitems = this.column;
1874 var lastcol = (this.total - 1) % this.column;
1875 this.index = (col < lastcol ? this.total - lastcol + col
1877 this.row = this.blocks.length - 1;
1878 nitems = lastcol + 1;
1880 while (this.blocks[this.row].Index > this.index)
1885 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1886 nitems = this.blocks[this.row].Count ();
1887 this.index = (this.blocks[this.row].Index
1888 + (col < nitems ? col : nitems - 1));
1893 function next_group ()
1895 var col = get_col.call (this);
1897 if (this.column > 0)
1899 this.index += this.column - col;
1900 if (this.index < this.total)
1902 if (this.index + col >= this.total)
1904 nitems = this.total - this.index;
1905 this.index = this.total - 1;
1909 nitems = this.column;
1918 while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
1924 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1925 nitems = this.blocks[this.row].Count ();
1926 this.index = (this.blocks[this.row].Index
1927 + (col < nitems ? col : nitems - 1));
1934 if (this.index == 0)
1936 this.index = this.total - 1;
1937 this.row = this.blocks.length - 1;
1942 if (this.blocks[this.row].Index > this.index)
1950 if (this.index == this.total)
1957 var b = this.blocks[this.row];
1958 if (this.index == b.Index + b.Count ())
1965 this.index -= get_col.call (this);
1966 while (this.blocks[this.row].Index > this.index)
1972 var b = this.blocks[this.row];
1973 if (this.column > 0)
1975 if (this.index + 1 < this.total)
1977 this.index += this.column - get_col.call (this) + 1;
1978 while (b.Index + b.Count () <= this.index)
1979 b = this.blocks[++this.row];
1983 this.index = b.Index + b.Count () - 1;
1986 MIM.Candidates.prototype.Current = function ()
1988 var b = this.blocks[this.row];
1989 return b.get (this.index - b.Index);
1992 MIM.Candidates.prototype.Select = function (selector)
1994 var idx = this.index;
1995 var gidx = this.column > 0 ? idx / this.column : this.row;
1996 if (selector.type == 'selector')
1998 switch (selector.val)
2000 case '@<': first.call (this); break;
2001 case '@>': last.call (this); break;
2002 case '@-': prev.call (this); break;
2003 case '@+': next.call (this); break;
2004 case '@[': prev_group.call (this); break;
2005 case '@]': next_group.call (this); break;
2012 if (this.column > 0)
2014 col = this.index % this.column;
2015 start = this.index - col;
2016 end = start + this.column;
2020 start = this.blocks[this.row].Index;
2021 col = this.index - start;
2022 end = start + this.blocks[this.row].Count;
2024 if (end > this.total)
2026 this.index += selector.val - col;
2027 if (this.index >= end)
2028 this.index = end - 1;
2029 if (this.column > 0)
2031 if (selector.val > col)
2032 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2036 while (this.blocks[this.row].Index > this.index)
2040 var newgidx = this.column > 0 ? this.index / this.column : this.row;
2041 if (this.index != idx)
2042 this.ic.changed |= (gidx == newgidx
2043 ? MIM.ChangedStatus.CandidateIndex
2044 : MIM.ChangedStatus.CandidateList);
2045 return this.Current ();
2048 MIM.Candidates.prototype.CurrentCol = function ()
2050 return get_col.call (this);
2053 MIM.Candidates.prototype.CurrentGroup = function ()
2055 var col, start, end, gnum, gidx;
2056 if (this.column > 0)
2058 gnum = Math.floor ((this.total - 1) / this.column) + 1;
2059 col = this.index % this.column;
2060 start = this.index - col;
2061 gidx = start / this.column + 1;
2062 end = start + this.column;
2063 if (end > this.total)
2068 gnum = this.blocks.length;
2069 gidx = this.row + 1;
2070 start = this.blocks[this.row].Index;
2071 col = this.index - start;
2072 end = start + this.blocks[this.row].Count ();
2074 var group = new Array ();
2075 var indices = new Array (gnum, gidx, col);
2076 group.push (indices);
2078 var block = this.blocks[row++];
2081 var c = block.get (start - block.Index);
2084 if (start == block.Index + block.Count ())
2085 block = this.blocks[row++];
2091 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2092 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2093 MIM.im_domain.DefType (MIM.Marker.prototype);
2094 MIM.im_domain.DefType (MIM.Selector.prototype);
2095 MIM.im_domain.DefType (MIM.Rule.prototype);
2096 MIM.im_domain.DefType (MIM.Map.prototype);
2097 MIM.im_domain.DefType (MIM.State.prototype);
2100 var im_domain = MIM.im_domain;
2102 function Finsert (domain, vari, args)
2105 if (args[0].type == 'integer')
2106 text = String.fromCharCode (args[0].val);
2109 domain.context.ins (text, null);
2113 function Finsert_candidates (domain, vari, args)
2115 var ic = domain.context;
2116 var gsize = domain.variables['candidates-group-size'];
2117 var candidates = new MIM.Candidates (ic, args,
2118 gsize ? gsize.val.Intval () : 0);
2119 ic.ins (candidates.Current (), candidates);
2123 function Fdelete (domain, vari, args)
2125 var ic = domain.context;
2126 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2127 return new Xex.IntTerm (ic.del (pos));
2130 function Fselect (domain, vari, args)
2132 var ic = domain.context;
2133 var can = ic.candidates;
2137 var old_text = can.Current ();
2138 var new_text = can.Select (args[0]);
2139 ic.rep (old_text, new_text, can);
2142 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2146 function Fshow (domain, vari, args)
2148 domain.context.candidate_show = true;
2149 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2153 function Fhide (domain, vari, args)
2155 domain.context.candidate_show = false;
2156 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2160 function Fchar_at (domain, vari, args)
2162 return new Xex.IntTerm (args[0].CharAt (domain.context));
2165 function Fmove (domain, vari, args)
2167 var ic = domain.context;
2168 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2170 return new Xex.IntTerm (pos);
2173 function Fmark (domain, vari, args)
2175 args[0].Mark (domain.context);
2179 function Fpushback (domain, vari, args)
2181 var a = (args[0].IsInt ? args[0].Intval ()
2182 : args[0].IsStr ? new KeySeq (args[0])
2184 domain.context.pushback (a);
2188 function Fpop (domain, vari, args)
2190 var ic = domain.context;
2191 if (ic.key_head < ic.keys.val.length)
2192 ic.keys.val.splice (ic.keys_head, 1);
2196 function Fundo (domain, vari, args)
2198 var ic = domain.context;
2199 var n = args.length == 0 ? -2 : args[0].val;
2200 Xex.Log ('undo with arg ' + args[0]);
2202 ic.keys.val.splice (ic.keys.val.length + n, -n);
2204 ic.keys.val.splice (n, ic.keys.val.length);
2209 function Fcommit (domain, vari, args)
2211 domain.context.commit ();
2215 function Funhandle (domain, vari, args)
2217 domain.context.commit ();
2218 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2221 function Fshift (domain, vari, args)
2223 var ic = domain.context;
2224 var state_name = args[0].val;
2225 var state = ic.im.state_list[state_name];
2227 throw ("Unknown state: " + state_name);
2232 function Fshiftback (domain, vari, args)
2234 domain.context.shift (null);
2238 function Fkey_count (domain, vari, args)
2240 return new Xex.IntTerm (domain.context.key_head);
2243 function Fsurrounding_flag (domain, vari, args)
2245 return new Xex.IntTerm (-1);
2248 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2249 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2250 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2251 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2252 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2253 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2254 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2255 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2256 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2257 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2258 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2259 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2260 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2261 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2262 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2263 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2264 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2265 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2270 function get_global_var (vname)
2272 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2273 MIM.im_global.Load ()
2274 return MIM.im_global.domain.variables[vname];
2277 function include (node)
2279 node = node.firstElement ();
2280 if (node.nodeName != 'tags')
2283 var lang = null, name = null, extra = null;
2284 for (node = node.firstElement (); node; node = node.nextElement ())
2286 if (node.nodeName == 'language')
2287 lang = node.firstChild.nodeValue;
2288 else if (node.nodeName == 'name')
2289 name = node.firstChild.nodeValue;
2290 else if (node.nodeName == 'extra-id')
2291 extra = node.firstChild.nodeValue;
2293 if (! lang || ! MIM.imlist[lang])
2297 if (! name || ! (im = MIM.imlist[lang][name]))
2302 if (! (im = MIM.imextra[lang][extra]))
2305 if (im.load_status != MIM.LoadStatus.Loaded
2306 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2313 parsers['description'] = function (node)
2315 this.description = node.firstChild.nodeValue;
2317 parsers['variable-list'] = function (node)
2319 for (node = node.firstElement (); node; node = node.nextElement ())
2321 var vname = node.attributes['vname'].nodeValue;
2322 if (this != MIM.im_global)
2324 var vari = get_global_var (vname);
2326 this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
2328 vname = Xex.Term.Parse (this.domain, node)
2331 parsers['command-list'] = function (node)
2334 parsers['macro-list'] = function (node)
2336 for (var n = node.firstElement (); n; n = n.nextElement ())
2337 if (n.nodeName == 'xi:include')
2339 var im = include (n);
2341 alert ('inclusion fail');
2343 for (var macro in im.domain.functions)
2345 var func = im.domain.functions[macro];
2346 if (func instanceof Xex.Macro)
2347 im.domain.CopyFunc (this.domain, macro);
2349 n = n.previousSibling;
2350 node.removeChild (n.nextSibling);
2352 Xex.Term.Parse (this.domain, node.firstElement (), null);
2354 parsers['title'] = function (node)
2356 this.title = node.firstChild.nodeValue;
2358 parsers['map-list'] = function (node)
2360 for (node = node.firstElement (); node; node = node.nextElement ())
2362 if (node.nodeName == 'xi:include')
2364 var im = include (node);
2367 alert ('inclusion fail');
2370 for (var mname in im.map_list)
2371 this.map_list[mname] = im.map_list[mname];
2375 var map = Xex.Term.Parse (this.domain, node);
2376 this.map_list[map.name] = map;
2380 parsers['state-list'] = function (node)
2382 this.domain.map_list = this.map_list;
2383 for (node = node.firstElement (); node; node = node.nextElement ())
2385 if (node.nodeName == 'xi:include')
2387 var im = include (node);
2389 alert ('inclusion fail');
2390 for (var sname in im.state_list)
2392 state = im.state_list[sname];
2393 if (! this.initial_state)
2394 this.initial_state = state;
2395 this.state_list[sname] = state;
2398 else if (node.nodeName == 'state')
2400 var state = Xex.Term.Parse (this.domain, node);
2402 state.title = this.title;
2403 if (! this.initial_state)
2404 this.initial_state = state;
2405 this.state_list[state.name] = state;
2408 delete this.domain.map_list;
2411 MIM.IM = function (lang, name, extra_id, file)
2415 this.extra_id = extra_id;
2417 this.load_status = MIM.LoadStatus.NotLoaded;
2418 this.domain = new Xex.Domain (this.lang + '-'
2419 + (this.name != 'nil'
2420 ? this.name : this.extra_id),
2421 MIM.im_domain, null);
2427 var node = Xex.Load (null, this.file);
2430 this.load_status = MIM.LoadStatus.Error;
2434 this.initial_state = null;
2435 this.state_list = {};
2436 for (node = node.firstElement (); node; node = node.nextElement ())
2438 var name = node.nodeName;
2439 var parser = parsers[name];
2441 parser.call (this, node);
2443 this.load_status = MIM.LoadStatus.Loaded;
2448 MIM.IM.prototype = proto;
2450 MIM.IC = function (im, target)
2452 if (im.load_status == MIM.LoadStatus.NotLoaded)
2454 if (im.load_status != MIM.LoadStatus.Loaded)
2455 alert ('im:' + im.name + ' error:' + im.load_status);
2457 this.target = target;
2458 this.domain = new Xex.Domain ('context', im.domain, this);
2460 this.range = new Array ();
2461 this.range[0] = this.range[1] = 0;
2463 this.initial_state = this.im.initial_state;
2464 this.keys = new MIM.KeySeq ();
2465 this.marker_positions = new Array ();
2466 this.candidate_table = new MIM.CandidateTable ();
2470 MIM.CandidateTable = function ()
2472 this.table = new Array ();
2475 MIM.CandidateTable.prototype.get = function (pos)
2477 for (var i = 0; i < this.table.length; i++)
2479 var elt = this.table[i];
2480 if (elt.from < pos && pos <= elt.to)
2485 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2487 for (var i = 0; i < this.table.length; i++)
2489 var elt = this.table[i];
2490 if (elt.from < to && elt.to > from)
2494 elt.val = candidates;
2498 this.table.push ({ from: from, to: to, val: candidates });
2501 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2503 var diff = inserted - (to - from);
2506 for (var i = 0; i < this.table.length; i++)
2508 var elt = this.table[i];
2517 MIM.CandidateTable.prototype.clear = function ()
2519 this.table.length = 0;
2522 function set_cursor (prefix, pos)
2524 this.cursor_pos = pos;
2525 var candidates = this.candidate_table.get (pos);
2526 if (this.candidates != candidates)
2528 this.candidates = candidates;
2529 this.changed |= MIM.ChangedStatus.CandidateList;
2533 function save_state ()
2535 this.state_var_values = this.domain.SaveValues ();
2536 this.state_preedit = this.preedit;
2537 this.state_key_head = this.key_head;
2538 this.state_pos = this.cursor_pos;
2541 function restore_state ()
2543 this.domain.RestoreValues (this.state_var_values);
2544 this.preedit = this.state_preedit;
2545 set_cursor.call (this, "restore", this.state_pos);
2548 function handle_key ()
2550 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
2551 + '" in ' + this.state.name + ':' + this.keymap.name
2552 + " key/state/commit-head/len:"
2553 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
2554 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
2557 if (out.index > this.key_head)
2559 this.key_head = out.index;
2560 Xex.Log (' with submap', false, true);
2561 restore_state.call (this);
2563 if (sub.map_actions)
2565 Xex.Log ('taking map actions:');
2566 if (! this.take_actions (sub.map_actions))
2569 else if (sub.submaps)
2571 Xex.Log ('no map actions');
2572 for (var i = this.state_key_head; i < this.key_head; i++)
2574 Xex.Log ('inserting key:' + this.keys.val[i].key);
2575 this.ins (this.keys.val[i].key, null);
2580 Xex.Log ('terminal:');
2581 if (this.keymap.branch_actions)
2583 Xex.Log ('branch actions:');
2584 if (! this.take_actions (this.keymap.branch_actions))
2587 if (sub != this.state.keymap)
2588 this.shift (this.state);
2593 Xex.Log (' without submap', false, true);
2595 var current_state = this.state;
2596 var map = this.keymap;
2598 if (map.branch_actions)
2600 Xex.Log ('branch actions:');
2601 if (! this.take_actions (map.branch_actions))
2605 if (map == this.keymap)
2607 Xex.Log ('no state change');
2608 if (map == this.initial_state.keymap
2609 && this.key_head < this.keys.val.length)
2611 Xex.Log ('unhandled');
2614 if (this.keymap != current_state.keymap)
2615 this.shift (current_state);
2616 else if (this.keymap.actions == null)
2617 this.shift (this.initial_state);
2626 this.cursor_pos = 0;
2627 this.candidate_show = false;
2628 this.prev_state = null;
2629 this.title = this.initial_state.title;
2630 this.state_preedit = '';
2631 this.state_key_head = 0;
2632 this.state_var_values = {};
2635 this.commit_key_head = 0;
2636 this.key_unhandled = false;
2637 this.unhandled_key = null;
2638 this.changed = MIM.ChangedStatus.None;
2639 this.error_message = '';
2640 this.title = this.initial_state.title;
2643 this.preedit_saved = '';
2644 this.candidate_table.clear ();
2645 this.candidates = null;
2646 this.candidate_show = false;
2647 for (var elt in this.marker_positions)
2648 this.marker_positions[elt] = 0;
2649 this.shift (this.initial_state);
2652 catch_args: new Array (Xex.CatchTag._mimtag, null),
2654 take_actions: function (actions)
2656 if (actions.length == 0)
2658 var func_progn = this.domain.GetFunc ('progn');
2659 var func_catch = this.domain.GetFunc ('catch');
2660 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2661 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2662 term = term.Eval (this.domain);
2663 return (! term.IsSymbol || term.val != '@mimtag');
2666 GetSurroundingChar: function (pos)
2670 pos += this.range[0];
2676 pos += this.range[1];
2677 if (pos >= this.target.value.length)
2680 return this.target.value.charCodeAt (pos);
2683 DelSurroundText: function (pos)
2688 pos += this.range[0];
2694 text = this.target.value.substring (0, pos);
2695 if (this.range[0] < this.target.value.length)
2696 text += this.target.value.substring (this.range[0]);
2697 this.target.value = text;
2698 this.range[1] -= this.range[0] - pos;
2699 this.range[0] = pos;
2703 pos += this.range[1];
2704 text = this.target.value.substring (0, this.range[1]);
2705 if (pos >= this.target.value.length)
2706 pos = this.target.value.length;
2708 text += this.target.value.substring (pos);
2709 this.target.value = text;
2713 adjust_markers: function (from, to, inserted)
2715 var diff = inserted - (to - from);
2717 for (var name in this.marker_positions)
2719 var pos = this.marker_positions[name];
2723 this.marker_positions[name] += diff;
2724 else if (pos > from)
2725 this.marker_positions[name] = from;
2730 preedit_replace: function (from, to, text, candidates)
2732 var newlen = text.length;
2733 this.preedit = (this.preedit.substring (0, from)
2734 + text + this.preedit.substring (to));
2735 this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2736 this.adjust_markers (from, to, newlen);
2737 this.candidate_table.adjust (from, to, newlen);
2739 this.candidate_table.put (from, from + newlen, candidates)
2740 if (this.cursor_pos >= to)
2741 set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
2742 else if (this.cursor_pos > from)
2743 set_cursor.call (this, 'adjust', from)
2746 ins: function (text, candidates)
2748 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2751 rep: function (old_text, new_text, candidates)
2753 this.preedit_replace (this.cursor_pos - old_text.length,
2754 this.cursor_pos, new_text, candidates);
2759 var deleted = pos - this.cursor_pos;
2760 if (pos < this.cursor_pos)
2764 this.DelSurroundText (pos);
2765 deleted = - this.cursor_pos;
2768 if (pos < this.cursor_pos)
2769 this.preedit_replace (pos, this.cursor_pos, '', null);
2773 if (pos > this.preedit.length)
2775 this.DelSurroundText (pos - this.preedit.length);
2776 deleted = this.preedit.length - this.cursor_pos;
2777 pos = this.preedit.length;
2779 if (pos > this.cursor_pos)
2780 this.preedit_replace (this.cursor_pos, pos, '', null);
2787 this.candidate_show = true;
2788 this.changed |= MIM.ChangedStatus.CandidateShow;
2793 this.candidate_show = false;
2794 this.changed |= MIM.ChangedStatus.CandidateShow;
2797 move: function (pos)
2801 else if (pos > this.preedit.length)
2802 pos = this.preedit.length;
2803 if (pos != this.cursor_pos)
2805 set_cursor.call (this, 'move', pos);
2806 this.changed |= MIM.ChangedStatus.Preedit;
2810 pushback: function (n)
2812 if (n instanceof MIM.KeySeq)
2814 if (this.key_head > 0)
2816 if (this.key_head < this.keys.val.length)
2817 this.keys.val.splice (this.key_head,
2818 this.keys.val.length - this.key_head);
2819 for (var i = 0; i < n.val.length; i++)
2820 this.keys.val.push (n.val[i]);
2826 if (this.key_head < 0)
2833 this.key_head = - n;
2834 if (this.key_head > this.keys.val.length)
2835 this.key_head = this.keys.val.length;
2841 if (this.key_head < this.keys.val.length)
2842 this.keys.val.splice (this.key_head, 1);
2847 if (this.preedit.length > 0)
2849 this.candidate_table.clear ();
2850 this.produced += this.preedit;
2851 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2852 this.preedit_saved = '';
2854 this.commit_key_head = this.key_head;
2858 shift: function (state)
2862 if (this.prev_state == null)
2864 state = this.prev_state;
2867 if (state == this.initial_state)
2872 this.keys.val.splice (0, this.key_head);
2873 this.key_head = this.state_key_head = this.commit_key_head = 0;
2874 this.prev_state = null;
2879 if (state != this.state)
2880 this.prev_state = this.state;
2882 if (state != this.state && state.enter_actions)
2883 this.take_actions (state.enter_actions);
2884 if (! this.state || this.state.title != state.title)
2885 this.changed |= MIM.ChangedStatus.StateTitle;
2887 this.keymap = state.keymap;
2888 save_state.call (this);
2891 Filter: function (key)
2895 Xex.Log ("active = false");
2896 this.key_unhandled = true;
2897 this.unhandled_key = key;
2900 if (key.key == '_reload')
2902 this.changed = MIM.ChangedStatus.None;
2904 this.key_unhandled = false;
2905 this.keys.val.push (key);
2907 while (this.key_head < this.keys.val.length)
2909 if (! handle_key.call (this))
2911 if (this.key_head < this.keys.val.length)
2913 this.unhandled_key = this.keys.val[this.key_head];
2914 this.keys.val.splice (this.key_head, this.key_head + 1);
2916 if (this.state_key_head > 0)
2917 this.state_key_head--;
2918 if (this.commit_key_head > 0)
2919 this.commit_key_head--;
2920 this.key_unhandled = true;
2926 this.key_unhandled = true;
2930 if (this.keymap == this.initial_state.keymap)
2933 if (this.commit_key_head > 0)
2935 this.keys.val.splice (0, this.commit_key_head);
2936 this.key_head -= this.commit_key_head;
2937 this.state_key_head -= this.commit_key_head;
2938 this.commit_key_head = 0;
2940 if (this.key_unhandled)
2942 this.keys.val.length = 0;
2943 //this.keys.val.splice (0, this.keys.val.length);
2944 this.key_head = this.state_key_head = this.commit_key_head = 0;
2946 if (this.changed & MIM.ChangedStatus.Candidate)
2948 if (this.candidate_show && this.candidates)
2953 return (! this.key_unhandled
2954 && this.produced.length == 0);
2958 MIM.IC.prototype = proto;
2960 var node = Xex.Load (null, "imlist.xml");
2961 for (node = node.firstChild; node; node = node.nextSibling)
2962 if (node.nodeName == 'input-method')
2964 var lang = null, name = null, extra_id = null, file = null;
2966 for (var n = node.firstChild; n; n = n.nextSibling)
2968 if (n.nodeName == 'language')
2969 lang = n.firstChild.nodeValue;
2970 else if (n.nodeName == 'name')
2971 name = n.firstChild.nodeValue;
2972 else if (n.nodeName == 'extra-id')
2973 extra_id = n.firstChild.nodeValue;
2974 else if (n.nodeName == 'filename')
2975 file = n.firstChild.nodeValue;
2977 if (name && name != 'nil')
2979 if (! MIM.imlist[lang])
2980 MIM.imlist[lang] = {};
2981 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2983 else if (extra_id && extra_id != 'nil')
2985 if (! MIM.imextra[lang])
2986 MIM.imextra[lang] = {};
2987 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2990 if (MIM.imextra.t && MIM.imextra.t.global)
2991 MIM.im_global = MIM.imextra.t.global;
2994 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2995 MIM.im_global.load_status = MIM.LoadStatus.Error;
3001 var keys = new Array ();
3003 keys[0x08] = 'backspace';
3004 keys[0x0D] = 'return';
3005 keys[0x1B] = 'escape';
3006 keys[0x20] = 'space';
3007 keys[0x21] = 'pageup';
3008 keys[0x22] = 'pagedown';
3010 keys[0x24] = 'home';
3011 keys[0x25] = 'left';
3013 keys[0x27] = 'right';
3014 keys[0x28] = 'down';
3015 keys[0x2D] = 'insert';
3016 keys[0x2E] = 'delete';
3017 for (var i = 1; i <= 12; i++)
3018 keys[111 + i] = "f" + i;
3019 keys[0x90] = "numlock";
3020 keys[0xF0] = "capslock";
3023 keyids['U+0008'] = 'Backspace';
3024 keyids['U+0009'] = 'Tab';
3025 keyids['U+0018'] = 'Cancel';
3026 keyids['U+001B'] = 'Escape';
3027 keyids['U+0020'] = 'Space';
3028 keyids['U+007F'] = 'Delete';
3031 modifiers.Shift = 1;
3032 modifiers.Control = 1;
3034 modifiers.AltGraph = 1;
3037 MIM.decode_key_event = function (event)
3039 var key = event.keyIdentifier;
3041 if (key) // keydown event of Chrome
3046 if (event.ctrlKey) mod += 'C-';
3047 if (event.metaKey) mod += 'M-';
3048 if (event.altKey) mod += 'A-';
3049 var keysym = keyids[key];
3052 else if (key.match(/^U\+([0-9A-Z]+)$/))
3054 if (mod.length == 0)
3056 key = String.fromCharCode (parseInt (RegExp.$1, 16));
3059 //key = key.toLowerCase ();
3060 if (event.shiftKey) mod += 'S-';
3061 return new MIM.Key (mod + key);
3065 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
3066 : event.charCode ? event.charCode
3070 if (event.type == 'keydown')
3075 if (event.shiftKey) key = "S-" + key ;
3078 key = String.fromCharCode (key);
3080 if (event.altKey) key = "A-" + key ;
3081 if (event.ctrlKey) key = "C-" + key ;
3082 return new MIM.Key (key);
3086 MIM.add_event_listener
3087 = (window.addEventListener
3088 ? function (target, type, listener) {
3089 target.addEventListener (type, listener, false);
3091 : window.attachEvent
3092 ? function (target, type, listener) {
3093 target.attachEvent ('on' + type,
3095 listener.call (target, window.event);
3098 : function (target, type, listener) {
3100 = function (e) { listener.call (target, e || window.event); };
3103 MIM.debug_print = function (event, ic)
3107 if (! MIM.debug_nodes)
3109 MIM.debug_nodes = new Array ();
3110 MIM.debug_nodes['status0'] = document.getElementById ('status0');
3111 MIM.debug_nodes['status1'] = document.getElementById ('status1');
3112 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3113 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3114 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3115 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3116 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3117 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3119 var target = event.target;
3120 var code = event.keyCode;
3121 var ch = event.type == 'keypress' ? event.charCode : 0;
3122 var key = MIM.decode_key_event (event);
3125 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3126 index = (event.type == 'keydown' ? '0' : '1');
3128 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3130 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3131 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3132 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3135 MIM.debug_nodes.keypress.innerHTML = '';
3136 MIM.debug_nodes.status1.innerHTML = '';
3137 MIM.debug_nodes.keymap1.innerHTML = '';
3138 MIM.debug_nodes.preedit1.innerHTML = ''
3142 MIM.get_range = function (target, ic)
3145 if (target.selectionStart != null) // for Mozilla
3147 from = target.selectionStart;
3148 to = target.selectionEnd;
3152 var r = document.selection.createRange ();
3153 var rr = r.duplicate ();
3155 rr.moveToElementText (target);
3156 rr.setEndPoint ('EndToEnd', range);
3157 from = rr.text.length - r.text.length;
3158 to = rr.text.length;
3160 if (ic.range[0] == from && ic.range[1] == to
3161 && (to == from || target.value.substring (from, to) == ic.preedit))
3168 MIM.set_caret = function (target, ic)
3170 if (target.setSelectionRange) // Mozilla
3172 var scrollTop = target.scrollTop;
3173 target.setSelectionRange (ic.range[0], ic.range[1]);
3174 target.scrollTop = scrollTop;
3178 var range = target.createTextRange ();
3179 range.moveStart ('character', ic.range[0]);
3180 range.moveEnd ('character', ic.range[1]);
3185 MIM.update = function (target, ic)
3187 var text = target.value;
3188 target.value = (text.substring (0, ic.range[0])
3191 + text.substring (ic.range[1]));
3192 ic.range[0] += ic.produced.length;
3193 ic.range[1] = ic.range[0] + ic.preedit.length;
3194 MIM.set_caret (target, ic);
3201 padingLeft: 'padding-left',
3202 paddingRight: 'padding-right',
3203 paddingTop: 'padding-top',
3204 paddintBottom: 'padding-bottom',
3205 borderLeftStyle: 'border-left-style',
3206 borderRightStyle: 'border-right-style',
3207 borderTopStyle: 'border-top-style',
3208 borderBottomStyle: 'border-bottom-style',
3209 borderLeftWidth: 'border-left-width',
3210 borderRightWidth: 'border-right-width',
3211 borderTopWidth: 'border-top-width',
3212 borderBottomWidth: 'border-bottom-width',
3213 fontFamily: 'font-family',
3214 fontSize: 'font-size',
3215 lineHeight: 'line-height',
3216 letterSpacing: 'letter-spacing',
3217 wordSpacing: 'word-spacing' };
3219 function copy_style (from, to)
3221 var from_style = getComputedStyle(from,'');
3222 for(var name in style_props)
3223 to.style[name] = from_style.getPropertyValue (style_props[name]);
3224 to.style.left = from.offsetLeft + 'px';
3225 to.style.top = from.offsetTop + 'px';
3226 to.style.width = from.offsetWidth;
3227 to.style.height = from.offsetHeight;
3230 MIM.show = function (ic)
3232 if (! ic.candidates)
3234 var target = ic.target;
3239 for (var elm = ic.target.offsetParent; elm; elm = elm.offsetParent)
3241 ic.target_top += elm.offsetTop;
3242 ic.target_left += elm.offsetLeft;
3244 ic.div_node = document.createElement ('div');
3245 copy_style (target, ic.div_node);
3246 ic.div_node.style.visibility="hidden";
3247 ic.div_node.style.position = "absolute";
3248 document.getElementsByTagName ('body')[0].appendChild (ic.div_node);
3249 ic.div_node_first = document.createElement ('span');
3250 ic.div_node_last = document.createElement('span');
3251 ic.div_node_last.innerHTML = '.';
3252 ic.div_node.appendChild (ic.div_node_first);
3253 ic.div_node.appendChild (ic.div_node_last);
3254 ic.can_node = document.createElement ('table');
3255 ic.can_node.style.position = 'absolute';
3256 ic.can_node.style.display = 'none';
3257 ic.can_node.style.backgroundColor = "white";
3258 ic.can_node.style.border = "1px solid black";
3259 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
3262 if (ic.changed & MIM.ChangedStatus.CandidateList)
3264 while (ic.can_node.childNodes.length > 0)
3265 ic.can_node.removeChild (ic.can_node.firstChild);
3266 var tr = document.createElement ('tr');
3267 var group = ic.candidates.CurrentGroup ();
3268 var td = document.createElement ('td');
3269 td.innerHTML = group[0][1] + '/' + group[0][0];
3270 td.style.color = 'white';
3271 td.style.backgroundColor = 'black';
3272 tr.appendChild (td);
3273 for (var i = 1; i < group.length; i++)
3275 var td = document.createElement ('td');
3277 td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
3278 if (i == group[0][2] + 1)
3279 td.style.backgroundColor = 'lightblue';
3280 tr.appendChild (td);
3282 ic.can_node.appendChild (tr);
3283 ic.div_node_first.innerHTML = target.value.substr (0, ic.range[0]);
3284 var x = ic.target_left + ic.div_node.lastChild.offsetLeft;
3285 var y = (ic.target_top + ic.div_node.lastChild.offsetTop
3286 + ic.div_node.lastChild.offsetHeight - target.scrollTop + 10);
3287 ic.can_node.style.left = x + 'px';
3288 ic.can_node.style.top = y + 'px';
3292 var td = ic.can_node.firstElement ().firstElement ().nextElement ();
3293 var col = ic.candidates.CurrentCol ();
3294 for (var i = 0; td; td = td.nextElement ())
3295 td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
3297 ic.can_node.style.display = 'block';
3300 MIM.hide = function (ic)
3303 ic.can_node.style.display = 'none';
3307 MIM.focus_in = function (event)
3309 var target = event.target;
3310 var ic = target.mim_ic;
3311 if (ic.wait_update == true)
3313 Xex.Log ("Focus in " + target.tagName + ' IGNORED');
3314 event.preventDefault ();
3317 Xex.Log ("Focus in " + target.tagName);
3318 ic.Filter (MIM.Key.FocusIn);
3319 function up () {MIM.update (target, ic);}
3320 setTimeout (up, 100);
3323 MIM.focus_out = function (event)
3325 var target = event.target;
3326 var ic = target.mim_ic;
3327 function reset_update () { ic.wait_update = false; };
3328 if (ic.wait_update == true)
3330 Xex.Log ("Focus out " + target.tagName + ' IGNORED');
3331 event.preventDefault ();
3334 Xex.Log ("Focus out " + target.tagName);
3335 ic.Filter (MIM.Key.FocusOut);
3336 ic.wait_update = true;
3337 MIM.update (target, ic, true);
3338 setTimeout (reset_update, 1000);
3341 MIM.keydown = function (event)
3343 var target = event.target;
3344 if (target.id == 'log')
3346 if (! (target.type == "text" || target.type == "textarea"))
3349 var ic = target.mim_ic;
3350 if (! ic || ic.im != MIM.current)
3352 target.mim_ic = null;
3353 Xex.Log ('creating IC');
3354 ic = new MIM.IC (MIM.current, target);
3355 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3358 MIM.add_event_listener (target, 'focus', MIM.focus_in);
3359 MIM.add_event_listener (target, 'blur', MIM.focus_out);
3360 MIM.get_range (target, ic)
3364 if (! MIM.get_range (target, ic))
3367 MIM.debug_print (event, ic);
3368 ic.key = MIM.decode_key_event (event);
3372 var result = ic.Filter (ic.key);
3374 Xex.Log ('Error' + e);
3377 MIM.update (target, ic);
3378 if (! ic.key_unhandled)
3379 event.preventDefault ();
3383 MIM.keypress = function (event)
3385 var target = event.target;
3386 if (target.id == 'log')
3388 if (! (target.type == "text" || target.type == "textarea"))
3391 var ic = target.mim_ic;
3395 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3398 ic.key = MIM.decode_key_event (event);
3406 var result = ic.Filter (ic.key);
3408 Xex.Log ('Error:' + e);
3411 MIM.update (target, ic);
3412 if (! ic.key_unhandled)
3413 event.preventDefault ();
3415 Xex.Log ("error:" + e);
3416 event.preventDefault ();
3418 MIM.debug_print (event, ic);
3425 var lang_category = {
3427 cs: { name: 'Czech' },
3428 da: { name: 'Danish' },
3429 el: { name: 'Greek' },
3430 en: { name: 'English' },
3431 eo: { name: 'Esperanto' },
3432 fr: { name: 'French' },
3433 grc: { name: 'ClassicGreek' },
3434 hr: { name: 'Croatian' },
3435 hy: { name: 'Armenian' },
3436 ka: { name: 'Georgian' },
3437 kk: { name: 'Kazakh' },
3438 ru: { name: 'Russian' },
3439 sk: { name: 'Slovak' },
3440 sr: { name: 'Serbian' },
3441 sv: { name: 'Swedish' },
3442 yi: { name: 'Yiddish' } },
3444 ar: { name: 'Arabic' },
3445 dv: { name: 'Divehi' },
3446 fa: { name: 'Persian' },
3447 he: { name: 'Hebrew' },
3448 kk: { name: 'Kazakh' },
3449 ps: { name: 'Pushto' },
3450 ug: { name: 'Uighur' },
3451 yi: { name: 'Yiddish' } },
3453 as: { name: 'Assamese' },
3454 bn: { name: 'Bengali' },
3455 bo: { name: 'Tibetan' },
3456 gu: { name: 'Gujarati' },
3457 hi: { name: 'Hindi' },
3458 kn: { name: 'Kannada' },
3459 ks: { name: 'Kashmiri' },
3460 ml: { name: 'Malayalam' },
3461 mr: { name: 'Marathi' },
3462 ne: { name: 'Nepali' },
3463 or: { name: 'Oriya' },
3464 pa: { name: 'Panjabi' },
3465 sa: { name: 'Sanskirit' },
3466 sd: { name: 'Sindhi' },
3467 si: { name: 'Sinhalese' },
3468 ta: { name: 'Tamil' },
3469 te: { name: 'Telugu' },
3470 ur: { name: 'Urdu' } },
3472 cmc: { name: 'Cham' },
3473 km: { name: 'Khmer'},
3474 lo: { name: 'Lao' },
3475 my: { name: 'Burmese' },
3476 tai: { name: 'Tai Viet' },
3477 th: { name: 'Thai' },
3478 vi: { name: 'Vietanamese' } },
3480 ii: { name: 'Yii' },
3481 ja: { name: 'Japanese' },
3482 ko: { name: 'Korean' },
3483 zh: { name: 'Chinese' } },
3485 am: { name: 'Amharic' },
3486 ath: { name: 'Carrier' },
3487 bla: { name: 'Blackfoot' },
3488 cr: { name: 'Cree' },
3489 eo: { name: 'Esperanto' },
3490 iu: { name: 'Inuktitut' },
3491 nsk: { name: 'Naskapi' },
3492 oj: { name: 'Ojibwe' },
3493 t: { name: 'Generic' } }
3496 function categorize_im ()
3498 var cat, lang, list, name;
3499 for (lang in MIM.imlist)
3502 for (cat in lang_category)
3503 if (lang_category[cat][lang])
3505 list = lang_category[cat][lang].list;
3507 list = lang_category[cat][lang].list = {};
3508 for (name in MIM.imlist[lang])
3509 list[name] = MIM.imlist[lang][name];
3512 for (name in MIM.imlist[lang])
3513 Xex.Log ('no category ' + lang + '-' + name);
3522 clearTimeout (destroy_timer);
3523 destroy_timer = null;
3524 var target = document.getElementById ('mim-menu');
3527 for (; last_target && last_target.menu_level;
3528 last_target = last_target.parentLi)
3529 last_target.style.backgroundColor = 'white';
3530 var nodes = target.getElementsByTagName ('ul');
3531 for (var i = 0; i < nodes.length; i++)
3532 nodes[i].style.visibility = 'hidden';
3533 document.getElementsByTagName ('body')[0].removeChild (target);
3537 function destroy_menu () {
3538 if (! destroy_timer)
3539 destroy_timer = setTimeout (destroy, 1000);
3542 function show_submenu (event)
3546 clearTimeout (destroy_timer);
3547 destroy_timer = null;
3549 var target = event.target;
3550 if (! target.menu_level)
3552 if (last_target && target.parentLi != last_target)
3554 last_target.style.backgroundColor = 'white';
3555 if (target.menu_level < last_target.menu_level)
3557 last_target = last_target.parentLi;
3558 last_target.style.backgroundColor = 'white';
3560 var uls = last_target.getElementsByTagName ('ul');
3561 for (var i = 0; i < uls.length; i++)
3562 uls[i].style.visibility = 'hidden';
3564 last_target = target;
3565 target.style.backgroundColor = 'yellow';
3566 if (target.menu_level < 3)
3568 target.lastChild.style.visibility = 'visible';
3569 target.lastChild.style.left = target.clientWidth + 'px';
3571 event.preventDefault ();
3574 function select_im (event)
3576 var target = event.target;
3579 MIM.current = target.im;
3582 event.preventDefault ();
3585 function create_ul (visibility)
3587 var ul = document.createElement ('ul');
3588 ul.style.position = 'absolute';
3589 ul.style.margin = '0px';
3590 ul.style.padding = '0px';
3591 ul.style.border = '1px solid gray';
3592 ul.style.borderBottom = 'none';
3593 ul.style.top = '-1px';
3594 ul.style.backgroundColor = 'white';
3595 ul.style.visibility = visibility;
3599 function create_li (level, text)
3601 var li = document.createElement ('li');
3602 li.style.position = 'relative';
3603 li.style.margin = '0px';
3604 li.style.padding = '1px';
3605 li.style.borderBottom = '1px solid gray';
3606 li.style.top = '0px';
3607 li.style.listStyle = 'none';
3608 li.menu_level = level;
3609 var nobr = document.createElement ('nobr');
3610 nobr.innerHTML = text;
3611 li.appendChild (nobr);
3617 function create_menu (event)
3619 var target = event.target;
3621 if (! ((target.type == "text" || target.type == "textarea")
3622 && event.which == 1 && event.ctrlKey))
3627 menu = create_ul ('visible');
3628 menu.style.fontFamily = 'sans-serif';
3629 menu.style.fontWeight = 'bold';
3630 menu.id = 'mim-menu';
3631 menu.onclick = select_im;
3632 menu.onmouseover = show_submenu;
3633 menu.onmouseout = destroy_menu;
3634 for (var catname in lang_category)
3636 var cat = lang_category[catname];
3637 var li = create_li (1, catname);
3638 var sub = create_ul ('hidden');
3639 for (var langname in cat)
3641 var lang = cat[langname];
3644 var sub_li = create_li (2, lang.name);
3645 sub_li.parentLi = li;
3646 var subsub = create_ul ('hidden');
3647 for (var name in lang.list)
3649 var im = lang.list[name];
3650 var subsub_li = create_li (3, im.name);
3651 subsub_li.parentLi = sub_li;
3653 subsub.appendChild (subsub_li);
3655 sub_li.appendChild (subsub);
3656 sub.appendChild (sub_li);
3658 li.appendChild (sub);
3659 menu.appendChild (li);
3661 lang_category = null;
3663 menu.style.left = (event.clientX - 10) + "px";
3664 menu.style.top = (event.clientY - 10) + "px";
3665 document.getElementsByTagName ('body')[0].appendChild (menu);
3668 MIM.init = function ()
3670 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3671 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3672 MIM.add_event_listener (window, 'mousedown', create_menu);
3673 if (window.location == 'http://localhost/mim/index.html')
3674 MIM.server = 'http://localhost/mim';
3675 MIM.current = MIM.imlist['zh']['py-gb'];
3679 MIM.test = function ()
3681 var im = MIM.imlist['t']['latn-post'];
3682 var ic = new MIM.IC (im, null);
3684 ic.Filter (new MIM.Key ('a'));
3685 ic.Filter (new MIM.Key ("'"));
3688 document.getElementById ('text').value = ic.produced + ic.preedit;
3691 document.getElementById ('text').value
3692 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3694 if (e instanceof Xex.ErrTerm)
3702 MIM.init_debug = function ()
3705 Xex.LogNode = document.getElementById ('log');