1 // -* coding: utf-8; -*
6 Log: function (arg, indent, cont)
12 while (Xex.LogNode.childNodes.length > 0)
13 Xex.LogNode.removeChild (Xex.LogNode.firstChild);
20 if (LogNodeLen >= 200)
21 node = Xex.LogNode.firstElement ();
23 node = document.createElement ('div');
24 if (indent != undefined)
25 node.style.textIndent = (indent + 1) + 'em';
27 node.style.textIndent = '0px';
28 node.innerText = LogNodeLen + ': ' + arg;
29 Xex.LogNode.appendChild (node);
30 Xex.LogNode.scrollTop = Xex.LogNode.scrollHeight;
36 UnknownError: "unknown-error",
37 WrongArgument: "wrong-argument",
39 InvalidInteger: "invalid-integer",
40 TermTypeInvalid: "term-type-invalid",
41 FunctionConflict: "function-conflict",
42 VariableTypeConflict: "variable-type-conflict",
43 VariableRangeConflict: "variable-range-conflict",
44 VariableWrongRange: "variable-wrong-range",
45 VariableWrongValue: "variable-wrong-value",
47 UnknownFunction: "unknown-function",
48 MacroExpansionError: "macro-expansion-error",
49 NoVariableName: "no-variable-name",
50 NoFunctionName: "no-funcion-name",
53 ArithmeticError: "arithmetic-error",
54 WrongType: "wrong-type",
55 IndexOutOfRange: "index-out-of-range",
56 ValueOutOfRange: "value-out-of-range",
57 NoLoopToBreak: "no-loop-to-break",
58 UncaughtThrow: "uncaught-throw"
61 Xex.Variable = function (domain, name, desc, val, range)
70 Xex.Variable.prototype.clone = function ()
72 return new Xex.Variable (this.domain, this.name, this.desc,
73 this.val, this.range);
76 Xex.Variable.prototype.Equals = function (obj)
78 return ((obj instanceof Xex.Variable)
79 && obj.name == this.name);
82 Xex.Variable.prototype.SetValue = function (term)
88 Xex.Function = function (name, with_var, min_args, max_args)
91 this.with_var = with_var;
92 this.min_args = min_args;
93 this.max_args = max_args;
96 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
99 this.with_var = with_var;
100 this.min_args = min_args;
101 this.max_args = max_args;
102 this.builtin = builtin;
105 Xex.Subrountine.prototype.Call = function (domain, vari, args)
107 var newargs = new Array ();
108 for (var i = 0; i < args.length; i++)
110 newargs[i] = args[i].Eval (domain);
111 if (domain.Thrown ())
114 return this.builtin (domain, vari, newargs)
117 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
120 this.with_var = with_var;
121 this.min_args = min_args;
122 this.max_args = max_args;
123 this.builtin = builtin;
126 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
128 return this.builtin (domain, vari, args)
131 Xex.Lambda = function (name, min_args, max_args, args, body)
134 this.min_args = min_args;
135 this.max_args = max_args;
140 Xex.Lambda.prototype.Call = function (domain, vari, args)
142 var current = domain.bindings;
143 var result = Xex.Zero;
144 var limit = max_args >= 0 ? args.length : args.length - 1;
148 for (i = 0; i < limit; i++)
150 result = args[i].Eval (domain);
151 if (domain.Thrown ())
153 domain.Bind (this.args[i], result);
157 var list = new Array ();
158 for (i = 0; i < args[limit].length; i++)
160 result = args[limit].Eval (domain);
161 if (domain.Thrown ())
165 domain.Bind (this.args[limit], list);
168 domain.Catch (Xex.CatchTag.Return);
169 for (var term in this.body)
171 result = term.Eval (domain);
172 if (domain.Thrown ())
179 domain.UnboundTo (current);
184 Xex.Macro = function (name, min_args, max_args, args, body)
187 this.min_args = min_args;
188 this.max_args = max_args;
193 Xex.Macro.prototype.Call = function (domain, vari, args)
195 var current = domain.bindings;
196 var result = Xex.Zero;
200 for (i = 0; i < args.length; i++)
201 domain.Bind (this.args[i], args[i]);
203 domain.Catch (Xex.CatchTag.Return);
204 for (var i in this.body)
206 result = this.body[i].Eval (domain);
207 if (domain.Thrown ())
214 domain.UnboundTo (current);
219 Xex.Bindings = function (vari)
222 this.old_value = vari.val;
225 Xex.Bindings.prototype.UnboundTo = function (boundary)
227 for (var b = this; b != boundary; b = b.next)
228 b.vari.val = b.old_value;
232 Xex.Bind = function (bindings, vari, val)
234 var b = new Xex.Bindings (vari);
245 Xex.Domain = function (name, parent, context)
248 this.context = context;
251 if (name != 'basic' && ! parent)
252 parent = Xex.BasicDomain
253 this.parent = parent;
260 for (elt in parent.termtypes)
261 this.termtypes[elt] = parent.termtypes[elt];
262 for (elt in parent.functions)
263 this.functions[elt] = parent.functions[elt];
264 for (elt in parent.variables)
266 var vari = parent.variables[elt];
267 this.variables[elt] = new Xex.Variable (this, vari.name, vari.desc,
268 vari.val, vari.range);
272 this.call_stack = new Array ();
273 this.bindings = null;
274 this.catch_stack = new Array ();
275 this.catch_count = 0;
279 Xex.Domain.prototype = {
280 CallStackCount: function () { return this.call_stack.length; },
281 CallStackPush: function (term) { this.call_stack.push (term); },
282 CallStackPop: function () { this.call_stack.pop (); },
283 Bind: function (vari, val)
285 this.bindings = Xex.Bind (this.bindings, vari, val);
287 UnboundTo: function (boundary)
290 this.bindings = this.bindings.UnboundTo (boundary);
292 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
295 this.catch_stack.pop ();
296 if (this.catch_count > this.catch_stack.length)
301 if (this.catch_count < this.catch_stack.length)
303 this.caught = (this.catch_count == this.catch_stack.length - 1);
309 ThrowReturn: function ()
311 for (var i = this.catch_stack.length - 1; i >= 0; i--)
314 if (this.catch_stack[i] == Xex.CatchTag.Return)
318 ThrowBreak: function ()
320 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
321 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
322 "No surrounding loop to break");
325 ThrowSymbol: function (tag)
327 var i = this.catch_count;
328 for (var j = this.catch_stack.length - 1; j >= 0; j--)
331 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
333 this.catch_count = i;
337 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
338 "No corresponding catch: " + tag);
340 DefType: function (obj)
343 if (this.termtypes[type])
344 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
345 "Already defined: " + type);
346 if (this.functions[type])
347 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
348 "Already defined as a funciton or a macro: "
350 this.termtypes[type] = obj.Parser;
352 DefSubr: function (builtin, name, with_var, min_args, max_args)
354 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
357 DefSpecial: function (builtin, name, with_var, min_args, max_args)
359 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
362 Defun: function (name, min_args, max_args, args, body)
364 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
367 DefunByFunc: function (func) { this.functions[func.name] = func; },
368 Defmacro: function (name, min_args, max_args, args, body)
370 this.functions[name] = new Xex.Macro (name, min_args, max_args,
373 DefAlias: function (alias, fname)
375 var func = this.functions[fname];
378 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
379 if (this.termtypes[alias])
380 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
381 "Already defined as a term type: " + alias);
382 if (this.functions[alias])
383 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
384 "Already defined as a function: " + alias);
385 this.functions[alias] = func;
387 Defvar: function (name, desc, val, range)
389 var vari = new Xex.Variable (this, name, desc, val, range);
390 this.variables[name] = vari;
393 GetFunc: function (name)
395 var func = this.functions[name];
397 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
398 "Unknown function: " + name);
401 CopyFunc: function (domain, name)
403 var func = this.functions[name];
404 domain.DefunByFunc (func);
407 CopyFuncAll: function (domain)
409 for (var elt in this.functions)
410 domain.DefunByFunc (this.functions[elt]);
412 GetVarCreate: function (name)
414 var vari = this.variables[name];
416 vari = this.variables[name] = new Xex.Variable (this, name, null,
420 GetVar: function (name) { return this.variables[name]; },
421 SaveValues: function ()
424 for (var elt in this.variables)
425 values[elt] = this.variables[elt].val.Clone ();
428 RestoreValues: function (values)
433 var vari = this.variables[name];
434 vari.val = values[name];
439 Xex.Term = function (type) { this.type = type; }
440 Xex.Term.prototype = {
441 IsTrue: function () { return true; },
442 Eval: function (domain) { return this.Clone (); },
443 Clone: function (domain) { return this; },
444 Equals: function (obj)
446 return (this.type == obj.type
448 && obj.val == this.val);
450 Matches: function (obj) { return this.Equals (obj); },
451 toString: function ()
453 if (this.val != undefined)
454 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
455 return '<' + this.type + '/>';
457 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
458 "Not an integer"); },
459 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
463 Node.prototype.firstElement = function ()
465 for (var n = this.firstChild; n; n = n.nextSibling)
471 Node.prototype.nextElement = function ()
473 for (var n = this.nextSibling; n; n = n.nextSibling)
480 function parse_defvar (domain, node)
482 var name = node.attributes['vname'].nodeValue;
484 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
485 var vari = domain.variables[name];
486 var desc, val = null, range;
489 desc = vari.description;
493 node = node.firstElement ();
494 if (node && node.nodeName == 'description')
496 desc = node.firstChild.nodeValue;
497 node = node.nextElement ();
501 val = Xex.Term.Parse (domain, node);
502 node = node.nextElement ();
503 if (node && node.nodeName == 'possible-values')
504 for (node = node.firstElement (); node; node = node.nextElement ())
507 if (node.nodeName == 'range')
510 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
511 'Range not allowed for ' + name);
513 for (var n = node.firstElement (); n; n = n.nextElement ())
515 var v = Xex.Term.Parse (domain, n);
517 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
518 'Invalid range value: ' + val);
524 pval = Xex.Term.Parse (domain, node);
525 if (val.type != pval.type)
526 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
527 'Invalid possible value: ' + pval);
530 range = new Array ();
536 domain.Defvar (name, desc, val, range);
540 function parse_defun_head (domain, node)
542 var name = node.attributes['fname'].nodeValue;
544 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
545 var args = new Array ();
546 var nfixed = 0, noptional = 0, nrest = 0;
548 node = node.firstElement ();
549 if (node && node.nodeName == 'args')
552 for (n = n.firstElement (); n; n = n.nextElement ())
554 if (n.nodeName == 'fixed')
556 else if (n.nodeName == 'optional')
558 else if (n.nodeName == 'rest')
561 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
564 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
565 for (n = node.firstElement (); n; n = n.nextElement ())
566 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
568 args.min_args = nfixed;
569 args.max_args = nrest == 0 ? nfixed + noptional : -1;
571 if (node.nodeName == 'defun')
572 domain.Defun (name, args, null);
574 domain.Defmacro (name, args, null);
578 function parse_defun_body (domain, node)
580 var name = node.attributes['fname'].nodeValue;
581 var func = domain.GetFunc (name);
583 for (node = node.firstElement (); node; node = node.nextElement ())
584 if (node.nodeName != 'description' && node.nodeName != 'args')
586 body = Xex.Term.Parse (domain, node, null);
590 Xex.Term.Parse = function (domain, node, stop)
592 if (arguments.length == 2)
594 var name = node.nodeName;
595 var parser = domain.termtypes[name];
598 return parser (domain, node);
599 if (name == 'defun' || name == 'defmacro')
601 name = parse_defun_head (domain, node);
602 parse_defun_body (domain, node);
603 return new Xex.StrTerm (name);
605 if (name == 'defvar')
607 name = parse_defvar (domain, node);
608 return new Xex.StrTerm (name);
610 return new Xex.Funcall.prototype.Parser (domain, node);
612 for (var n = node; n && n != stop; n = n.nextElement ())
613 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
614 parse_defun_head (domain, n);
616 for (var n = node; n && n != stop; n = n.nextElement ())
618 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
619 parse_defun_body (domain, n);
620 else if (n.nodeName == 'defvar')
621 parse_defvar (domain, n);
625 terms = new Array ();
626 terms.push (Xex.Term.Parse (domain, n));
633 Xex.Varref = function (vname)
639 var proto = new Xex.Term ('varref');
641 proto.Clone = function () { return new Xex.Varref (this.val); }
642 proto.Eval = function (domain)
644 var vari = domain.GetVarCreate (this.val);
645 Xex.Log (this.ToString () + '=>' + vari.val, domain.depth);
649 proto.Parser = function (domain, node)
651 return new Xex.Varref (node.attributes['vname'].nodeValue);
654 proto.ToString = function ()
656 return '<varref vname="' + this.val + '"/>';
659 Xex.Varref.prototype = proto;
662 var null_args = new Array ();
664 Xex.Funcall = function (func, vname, args)
668 this.args = args || null_args;
672 var proto = new Xex.Term ('funcall');
674 proto.Parser = function (domain, node)
676 var fname = node.nodeName;
679 if (fname == 'funcall')
680 fname = node.attributes['fname'].nodeValue;
681 var func = domain.GetFunc (fname);
683 attr = node.attributes['vname'];
684 vname = attr != undefined ? attr.nodeValue : null;
685 var args = Xex.Term.Parse (domain, node.firstElement (), null);
686 return new Xex.Funcall (func, vname, args);
689 proto.New = function (domain, fname, vname, args)
691 var func = domain.GetFunc (fname);
692 var funcall = new Xex.Funcall (func, vname, args);
693 if (func instanceof Xex.Macro)
694 funcall = funcall.Eval (domain);
698 proto.Eval = function (domain)
700 Xex.Log (this, domain.depth);
703 vari = domain.GetVarCreate (this.vname);
707 result = this.func.Call (domain, vari, this.args);
709 Xex.Log (' => ' + result, --domain.depth,
710 this.func instanceof Xex.Subrountine);
715 proto.Clone = function ()
717 return new Xex.Funcall (this.func, this.vari, this.args);
720 proto.Equals = function (obj)
722 return (obj.type == 'funcall'
723 && obj.func == this.func
724 && obj.vari.Equals (this.vari)
725 && obj.args.length == this.func.length);
728 proto.toString = function ()
731 var len = this.args.length;
732 var str = '<' + this.func.name;
734 str += ' vname="' + this.vname + '"';
737 if (this.func instanceof Xex.Subrountine)
738 for (var i = 0; i < len; i++)
739 arglist += this.args[i].toString ();
741 for (var i = 0; i < len; i++)
743 return str + '>' + arglist + '</' + this.func.name + '>';
746 Xex.Funcall.prototype = proto;
749 Xex.ErrTerm = function (ename, message, stack)
752 this.message = message;
757 var proto = new Xex.Term ('error');
759 proto.IsError = true;
761 proto.Parser = function (domain, node)
763 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
764 node.innerText, false);
767 proto.CallStack = function () { return stack; }
769 proto.SetCallStack = function (value) { statck = value; }
771 proto.Clone = function ()
773 return new Xex.ErrTerm (ename, message, false);
776 proto.Equals = function (obj)
779 && obj.ename == ename && obj.message == message
780 && (obj.stack ? (stack && stack.length == obj.stack.length)
784 proto.Matches = function (obj)
786 return (obj.IsError && obj.ename == ename);
789 proto.toString = function ()
791 return '<error ename="' + this.ename + '">' + this.message + '</error>';
794 Xex.ErrTerm.prototype = proto;
797 Xex.IntTerm = function (num) { this.val = num; };
799 var proto = new Xex.Term ('integer');
801 proto.Intval = function () { return this.val; };
802 proto.IsTrue = function () { return this.val != 0; }
803 proto.Clone = function () { return new Xex.IntTerm (this.val); }
804 proto.Parser = function (domain, node)
806 var str = node.firstChild.nodeValue;
808 if (str.charAt (0) == '?' && str.length == 2)
809 return new Xex.IntTerm (str.charCodeAt (1));
810 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
812 Xex.IntTerm.prototype = proto;
815 Xex.StrTerm = function (str) { this.val = str; };
817 var proto = new Xex.Term ('string');
819 proto.Strval = function () { return this.val; };
820 proto.IsTrue = function () { return this.val.length > 0; }
821 proto.Clone = function () { return new Xex.StrTerm (this.val); }
822 proto.Parser = function (domain, node)
824 return new Xex.StrTerm (node.firstChild ? node.firstChild.nodeValue : '');
826 Xex.StrTerm.prototype = proto;
829 Xex.SymTerm = function (str) { this.val = str; };
831 var proto = new Xex.Term ('symbol');
832 proto.IsSymbol = true;
833 proto.IsTrue = function () { return this.val != 'nil'; }
834 proto.Clone = function () { return new Xex.SymTerm (this.val); }
835 proto.Parser = function (domain, node)
837 return new Xex.SymTerm (node.firstChild.nodeValue);
839 Xex.SymTerm.prototype = proto;
842 Xex.LstTerm = function (list) { this.val = list; };
844 var proto = new Xex.Term ('list');
846 proto.IsTrue = function () { return this.val.length > 0; }
847 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
849 proto.Equals = function (obj)
851 if (obj.type != 'list' || obj.val.length != this.val.length)
853 var i, len = this.val.length;
854 for (i = 0; i < len; i++)
855 if (! this.val[i].Equals (obj.val[i]))
860 proto.Parser = function (domain, node)
862 var list = Xex.Term.Parse (domain, node.firstElement (), null);
863 return new Xex.LstTerm (list);
866 proto.toString = function ()
868 var len = this.val.length;
873 for (var i = 0; i < len; i++)
874 str += this.val[i].toString ();
875 return str + '</list>';
877 Xex.LstTerm.prototype = proto;
881 var basic = new Xex.Domain ('basic', null, null);
883 function Fset (domain, vari, args)
886 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
887 'No variable name to set');
888 vari.SetValue (args[0]);
892 function Fnot (domain, vari, args)
894 return (args[0].IsTrue () ? Xex.Zero : Xex.One);
897 function maybe_set_intvar (vari, n)
899 var term = new Xex.IntTerm (n);
901 vari.SetValue (term);
905 function Fadd (domain, vari, args)
907 var n = vari ? vari.val.Intval () : 0;
908 var len = args.length;
910 for (var i = 0; i < len; i++)
911 n += args[i].Intval ();
912 return maybe_set_intvar (vari, n);
915 function Fmul (domain, vari, args)
917 var n = vari ? vari.val.Intval () : 1;
918 for (var i = 0; i < args.length; i++)
920 return maybe_set_intvar (vari, n);
923 function Fsub (domain, vari, args)
929 n = args[0].Intval ();
934 n = vari.val.Intval ();
937 while (i < args.length)
938 n -= args[i++].Intval ();
939 return maybe_set_intvar (vari, n);
942 function Fdiv (domain, vari, args)
948 n = args[0].Intval ();
953 n = vari.val.Intval ();
956 while (i < args.length)
957 n /= args[i++].Intval ();
958 return maybe_set_intvar (vari, n);
961 function Fmod (domain, vari, args)
963 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
966 function Flogior (domain, vari, args)
968 var n = vari == null ? 0 : vari.val;
969 for (var i = 0; i < args.length; i++)
971 return maybe_set_intvar (vari, n);
974 function Flogand (domain, vari, args)
979 Xex.Log ("logand arg args[0]" + args[0]);
980 n = args[0].Intval ()
985 Xex.Log ("logand arg var " + vari);
986 n = vari.val.Intval ();
989 while (n > 0 && i < args.length)
991 Xex.Log ("logand arg " + args[i]);
994 return maybe_set_intvar (vari, n);
997 function Flsh (domain, vari, args)
999 return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
1002 function Frsh (domain, vari, args)
1004 return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
1007 function Fand (domain, vari, args)
1009 var len = args.length;
1010 for (var i = 0; i < len; i++)
1012 var result = args[i].Eval (domain);
1013 if (domain.Thrown ())
1015 if (! result.IsTrue ())
1021 function For (domain, vari, args)
1023 var len = args.length;
1024 for (var i = 0; i < len; i++)
1026 var result = args[i].Eval (domain);
1027 if (domain.Thrown ())
1029 if (result.IsTrue ())
1035 function Feq (domain, vari, args)
1037 for (var i = 1; i < args.length; i++)
1038 if (! args[i - 1].Equals (args[i]))
1043 function Fnoteq (domain, vari, args)
1045 return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1048 function Flt (domain, vari, args)
1050 var n = args[0].Intval ();
1052 for (var i = 1; i < args.length; i++)
1054 var n1 = args[i].Intval ();
1062 function Fle (domain, vari, args)
1064 var n = args[0].Intval ();
1065 for (var i = 1; i < args.length; i++)
1067 var n1 = args[i].Intval ();
1075 function Fgt (domain, vari, args)
1077 var n = args[0].Intval ();
1078 for (var i = 1; i < args.length; i++)
1080 var n1 = args[i].Intval ();
1088 function Fge (domain, vari, args)
1090 var n = args[0].Intval ();
1091 for (var i = 1; i < args.Length; i++)
1093 var n1 = args[i].Intval ();
1101 function Fprogn (domain, vari, args)
1103 var result = Xex.One;
1104 var len = args.length;
1106 for (var i = 0; i < len; i++)
1108 result = args[i].Eval (domain);
1109 if (domain.Thrown ())
1115 function Fif (domain, vari, args)
1117 var result = args[0].Eval (domain);
1119 if (domain.Thrown ())
1121 if (result.IsTrue ())
1122 return args[1].Eval (domain);
1123 if (args.length == 2)
1125 return args[2].Eval (domain);
1128 function Fcond (domain, vari, args)
1130 for (var i = 0; i < args.length; i++)
1133 var result = list.val[0].Eval (domain);
1134 if (result.IsTrue ())
1136 for (var j = 1; j < list.val.length; j++)
1139 result = list.val[j].Eval (domain);
1141 if (domain.Thrown ())
1150 function eval_terms (domain, terms, idx)
1152 var result = Xex.Zero;
1153 domain.caught = false;
1154 for (var i = idx; i < terms.length; i++)
1156 result = terms[i].Eval (domain);
1157 if (domain.Thrown ())
1163 function Fcatch (domain, vari, args)
1168 if (args[0].IsError)
1171 result = eval_terms (domain, args, 1);
1173 if (e instanceof Xex.ErrTerm)
1175 if (! args[0].Matches (e))
1183 else if (args[0].IsSymbol)
1186 domain.Catch (args[0].val);
1187 result = eval_terms (domain, args, 1);
1191 vari.SetValue (result);
1199 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1200 "Not a symbol nor an error: " + args[0]);
1203 function Fthrow (domain, vari, args)
1205 if (args[0].IsSymbl)
1207 domain.ThrowSymbol (args[0]);
1208 return (args[args.length - 1]);
1210 if (args[0].IsError)
1214 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1215 "Not a symbol nor an error:" + args[0]);
1218 Xex.BasicDomain = basic;
1220 basic.DefSubr (Fset, "set", true, 1, 1);
1221 basic.DefAlias ("=", "set");
1222 basic.DefSubr (Fnot, "not", false, 1, 1);
1223 basic.DefAlias ("!", "not");
1224 basic.DefSubr (Fadd, "add", true, 1, -1);
1225 basic.DefSubr (Fmul, "mul", true, 1, -1);
1226 basic.DefAlias ("*", "mul");
1227 basic.DefSubr (Fsub, "sub", true, 1, -1);
1228 basic.DefAlias ("-", "sub");
1229 basic.DefSubr (Fdiv, "div", true, 1, -1);
1230 basic.DefAlias ("/", "div");
1231 basic.DefSubr (Fmod, "mod", true, 1, 2);
1232 basic.DefAlias ("%", "mod");
1233 basic.DefSubr (Flogior, "logior", true, 1, -1);
1234 basic.DefAlias ('|', "logior");
1235 basic.DefSubr (Flogand, "logand", true, 1, -1);
1236 basic.DefAlias ("&", "logand");
1237 basic.DefSubr (Flsh, "lsh", true, 1, 2);
1238 basic.DefAlias ("<<", "lsh");
1239 basic.DefSubr (Frsh, "rsh", true, 1, 2);
1240 basic.DefAlias (">>", "rsh");
1241 basic.DefSubr (Feq, "eq", false, 2, -1);
1242 basic.DefAlias ("==", "eq");
1243 basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1244 basic.DefAlias ("!=", "noteq");
1245 basic.DefSubr (Flt, "lt", false, 2, -1);
1246 basic.DefAlias ("<", "lt");
1247 basic.DefSubr (Fle, "le", false, 2, -1);
1248 basic.DefAlias ("<=", "le");
1249 basic.DefSubr (Fgt, "gt", false, 2, -1);
1250 basic.DefAlias (">", "gt");
1251 basic.DefSubr (Fge, "ge", false, 2, -1);
1252 basic.DefAlias (">=", "ge");
1253 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1255 //basic.DefSubr (Fappend, "append", true, 0, -1);
1256 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1257 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1258 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1259 //basic.DefSubr (Fins, "ins", true, 2, 2);
1260 //basic.DefSubr (Fdel, "del", true, 2, 2);
1261 //basic.DefSubr (Feval, "eval", false, 1, 1);
1262 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1263 //basic.DefSubr (Freturn, "return", false, 0, 1);
1264 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1266 basic.DefSpecial (Fand, "and", false, 1, -1);
1267 basic.DefAlias ("&&", "and");
1268 basic.DefSpecial (For, "or", false, 1, -1);
1269 basic.DefAlias ("||", "or");
1270 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1271 basic.DefAlias ("expr", "progn");
1272 basic.DefSpecial (Fif, "if", false, 2, 3);
1273 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1274 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1275 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1276 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1277 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1278 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1279 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1280 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1282 basic.DefType (Xex.Funcall.prototype);
1283 basic.DefType (Xex.Varref.prototype);
1284 basic.DefType (Xex.ErrTerm.prototype);
1285 basic.DefType (Xex.IntTerm.prototype);
1286 basic.DefType (Xex.StrTerm.prototype);
1287 basic.DefType (Xex.SymTerm.prototype);
1288 basic.DefType (Xex.LstTerm.prototype);
1292 Xex.Zero = new Xex.IntTerm (0);
1293 Xex.One = new Xex.IntTerm (1);
1294 Xex.nil = new Xex.SymTerm ('nil');
1296 Xex.Load = function (server, file)
1298 var obj = new XMLHttpRequest ();
1299 var url = server ? server + '/' + file : file;
1300 obj.open ('GET', url, false);
1301 obj.overrideMimeType ('text/xml');
1303 return obj.responseXML.firstChild;
1307 // URL of the input method server.
1308 server: "http://www.m17n.org/common/mim-js",
1309 // Boolean flag to tell if MIM is active or not.
1311 // Boolean flag to tell if MIM is running in debug mode or not.
1313 // List of main input methods.
1315 // List of extra input methods;
1317 // Global input method data
1319 // Currently selected input method.
1323 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1330 CandidateIndex:0x10,
1332 Preedit: 0x06, // PreeditText | CursorPos
1333 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1355 ParseError: "parse-error"
1360 var keysyms = new Array ();
1361 keysyms["bs"] = "backspace";
1362 keysyms["lf"] = "linefeed";
1363 keysyms["cr"] = keysyms["enter"] = "return";
1364 keysyms["esc"] = "escape";
1365 keysyms["spc"] = "space";
1366 keysyms["del"] = "delete";
1368 function decode_keysym (str) {
1369 if (str.length == 1)
1371 var parts = str.split ("-");
1372 var len = parts.length, i;
1373 var has_modifier = len > 1;
1375 for (i = 0; i < len - 1; i++)
1376 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1378 var key = parts[len - 1];
1381 key = keysyms[key.toLowerCase ()];
1387 for (i = 1; i < len - 1; i++)
1388 str += '-' + parts[i];
1397 parts = new Array ();
1404 MIM.Key = function (val)
1407 if (val instanceof Xex.Term)
1409 else if (typeof val == 'string' || val instanceof String)
1411 this.key = decode_keysym (val);
1413 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1414 if (this.key instanceof Array)
1416 this.key = this.key[0];
1417 this.has_modifier = true;
1420 else if (typeof val == 'number' || val instanceof Number)
1421 this.key = String.fromCharCode (val);
1423 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1426 MIM.Key.prototype.toString = function () { return this.key; };
1428 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1429 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1430 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1434 MIM.KeySeq = function (seq)
1436 this.val = new Array ();
1442 var len = seq.val.length;
1443 for (var i = 0; i < len; i++)
1445 var v = seq.val[i], key;
1446 if (v.type == 'symbol' || v.type == 'string')
1447 key = new MIM.Key (v);
1448 else if (v.type == 'integer')
1449 key = new MIM.Key (v.val);
1451 throw new Xex.ErrTerm (MIM.Error.ParseError,
1452 "Invalid key: " + v);
1453 this.val.push (key);
1454 if (key.has_modifier)
1455 this.has_modifier = true;
1460 var len = seq.val.length;
1461 for (var i = 0; i < len; i++)
1462 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1465 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1469 var proto = new Xex.Term ('keyseq');
1470 proto.Clone = function () { return this; }
1471 proto.Parser = function (domain, node)
1473 var seq = new Array ();
1474 for (node = node.firstChild; node; node = node.nextSibling)
1475 if (node.nodeType == 1)
1477 var term = Xex.Term.Parse (domain, node);
1478 return new MIM.KeySeq (term);
1480 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1482 proto.toString = function ()
1484 var len = this.val.length;
1488 var str = '<keyseq>';
1489 for (var i = 0; i < len; i++)
1493 else if (this.has_modifier)
1495 str += this.val[i].toString ();
1497 return str + '</keyseq>';
1500 MIM.KeySeq.prototype = proto;
1504 MIM.Marker = function () { }
1505 MIM.Marker.prototype = new Xex.Term ('marker');
1506 MIM.Marker.prototype.CharAt = function (ic)
1508 var p = this.Position (ic);
1509 if (p < 0 || p >= ic.preedit.length)
1511 return ic.preedit.charCodeAt (p);
1514 MIM.FloatingMarker = function (name) { this.val = name; };
1515 var proto = new MIM.Marker ();
1516 MIM.FloatingMarker.prototype = proto;
1517 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1518 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1520 MIM.PredefinedMarker = function (name) { this.val = name; }
1521 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1522 MIM.PredefinedMarker.prototype.Position = function (ic)
1524 if (typeof this.pos == 'number')
1526 return this.pos (ic);
1529 var predefined = { }
1531 function def_predefined (name, position)
1533 predefined[name] = new MIM.PredefinedMarker (name);
1534 predefined[name].pos = position;
1537 def_predefined ('@<', 0);
1538 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1539 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1540 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1541 def_predefined ('@[', function (ic) {
1542 if (ic.cursor_pos > 0)
1544 var pos = ic.cursor_pos;
1545 return ic.preedit.FindProp ('candidates', pos - 1).from;
1549 def_predefined ('@]', function (ic) {
1550 if (ic.cursor_pos < ic.preedit.length - 1)
1552 var pos = ic.cursor_pos;
1553 return ic.preedit.FindProp ('candidates', pos).to;
1555 return ic.preedit.length;
1557 for (var i = 0; i < 10; i++)
1558 def_predefined ("@" + i, i);
1559 predefined['@first'] = predefined['@<'];
1560 predefined['@last'] = predefined['@>'];
1561 predefined['@previous'] = predefined['@-'];
1562 predefined['@next'] = predefined['@+'];
1563 predefined['@previous-candidate-change'] = predefined['@['];
1564 predefined['@next-candidate-change'] = predefined['@]'];
1566 MIM.SurroundMarker = function (name)
1569 this.distance = parseInt (name.slice (1));
1570 if (isNaN (this.distance))
1571 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1573 MIM.SurroundMarker.prototype = new MIM.Marker ();
1574 MIM.SurroundMarker.prototype.Position = function (ic)
1576 return ic.cursor_pos + this.distance;
1578 MIM.SurroundMarker.prototype.CharAt = function (ic)
1580 if (this.val == '@-0')
1582 var p = this.Position (ic);
1584 return ic.GetSurroundingChar (p);
1585 else if (p >= ic.preedit.length)
1586 return ic.GetSurroundingChar (p - ic.preedit.length);
1587 return ic.preedit.charCodeAt (p);
1590 MIM.Marker.prototype.Parser = function (domain, node)
1592 var name = node.firstChild.nodeValue;
1593 if (name.charAt (0) == '@')
1595 var n = predefined[name];
1598 if (name.charAt (1) == '-' || name.charAt (1) == '+')
1599 return new MIM.SurroundMarker (name);
1600 throw new Xex.ErrTerm (MIM.Error.ParseError,
1601 "Invalid marker: " + name);
1603 return new MIM.FloatingMarker (name);;
1607 MIM.Selector = function (name)
1611 MIM.Selector.prototype = new Xex.Term ('selector');
1615 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1616 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1617 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1618 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1619 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1620 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
1621 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
1623 MIM.Selector.prototype.Parser = function (domain, node)
1625 var name = node.firstChild.nodeValue;
1626 var s = selectors[name];
1628 throw new Xex.ErrTerm (MIM.Error.ParseError,
1629 "Invalid selector: " + name);
1634 MIM.Rule = function (keyseq, actions)
1636 this.keyseq = keyseq;
1638 this.actions = actions;
1640 MIM.Rule.prototype = new Xex.Term ('rule');
1641 MIM.Rule.prototype.Parser = function (domain, node)
1644 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1646 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1647 var keyseq = Xex.Term.Parse (domain, n);
1648 if (keyseq.type != 'keyseq')
1649 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1651 n = n.nextElement ();
1653 actions = Xex.Term.Parse (domain, n, null);
1654 return new MIM.Rule (keyseq, actions);
1656 MIM.Rule.prototype.toString = function ()
1661 MIM.Map = function (name)
1664 this.rules = new Array ();
1668 var proto = new Xex.Term ('map');
1670 proto.Parser = function (domain, node)
1672 var name = node.attributes['mname'].nodeValue;
1674 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1675 var map = new MIM.Map (name);
1676 for (var n = node.firstChild; n; n = n.nextSibling)
1677 if (n.nodeType == 1)
1678 map.rules.push (Xex.Term.Parse (domain, n));
1682 proto.toString = function ()
1684 var str = '<map mname="' + this.name + '">';
1685 var len = this.rules.length;
1686 for (i = 0; i < len; i++)
1687 str += this.rules[i];
1688 return str + '</map>';
1691 MIM.Map.prototype = proto;
1694 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1696 MIM.Action = function (domain, terms)
1698 var args = new Array ();
1699 args.push (Xex.CatchTag_.mimtag);
1700 for (var i = 0; i < terms.length; i++)
1701 args.push (terms[i]);
1702 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1705 MIM.Action.prototype.Run = function (domain)
1707 var result = this.action.Eval (domain);
1708 if (result.type == 'error')
1710 domain.context.Error = result.toString ();
1713 return (result != Xex.CatchTag._mimtag);
1716 MIM.Keymap = function ()
1719 this.submaps = null;
1725 function add_rule (keymap, rule, branch_actions)
1727 var keyseq = rule.keyseq;
1728 var len = keyseq.val.length;
1731 for (var i = 0; i < len; i++)
1733 var key = keyseq.val[i];
1737 if (! keymap.submaps)
1738 keymap.submaps = {};
1740 sub = keymap.submaps[key.key];
1742 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1746 keymap.map_actions = rule.actions;
1748 keymap.branch_actions = branch_actions;
1751 proto.Add = function (map, branch_actions)
1753 var rules = map.rules;
1754 var len = rules.length;
1756 for (var i = 0; i < len; i++)
1757 add_rule (this, rules[i], branch_actions);
1759 proto.Lookup = function (keys, index)
1763 if (index < keys.val.length && this.submaps
1764 && ! keys.val[index])
1766 Xex.Log ('invalid key at ' + index);
1767 throw 'invalid key';
1770 if (index < keys.val.length && this.submaps
1771 && (sub = this.submaps[keys.val[index].key]))
1774 return sub.Lookup (keys, index);
1776 return { map: this, index: index };
1779 MIM.Keymap.prototype = proto;
1782 MIM.State = function (name)
1785 this.keymap = new MIM.Keymap ();
1789 var proto = new Xex.Term ('state');
1791 proto.Parser = function (domain, node)
1793 var map_list = domain.map_list;
1794 var name = node.attributes['sname'].nodeValue;
1796 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1797 var state = new MIM.State (name);
1798 for (node = node.firstElement (); node; node = node.nextElement ())
1800 if (node.nodeName == 'title')
1801 state.title = node.firstChild.nodeValue;
1804 var n = node.firstElement ();
1805 var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
1806 if (node.nodeName == 'branch')
1807 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1809 else if (node.nodeName == 'state-hook')
1810 state.enter_actions = branch_actions;
1811 else if (node.nodeName == 'catch-all-branch')
1812 state.fallback_actions = branch_actions;
1818 proto.toString = function ()
1820 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1823 MIM.State.prototype = proto;
1827 function Block (index, term)
1831 this.Data = term.val;
1832 else if (term.IsList)
1834 this.Data = new Array ();
1835 for (var i = 0; i < term.val.length; i++)
1836 this.Data.push (term.val[i].val);
1840 Block.prototype.Count = function () { return this.Data.length; }
1841 Block.prototype.get = function (i)
1843 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1846 MIM.Candidates = function (ic, candidates, column)
1849 this.column = column;
1853 this.blocks = new Array ();
1855 for (var i = 0; i < candidates.length; i++)
1857 var block = new Block (this.total, candidates[i]);
1858 this.blocks.push (block);
1859 this.total += block.Count ();
1865 return (this.column > 0 ? this.index % this.column
1866 : this.index - this.blocks[this.row].Index);
1869 function prev_group ()
1871 var col = get_col.call (this);
1873 if (this.column > 0)
1875 this.index -= this.column;
1876 if (this.index >= 0)
1877 nitems = this.column;
1880 var lastcol = (this.total - 1) % this.column;
1881 this.index = (col < lastcol ? this.total - lastcol + col
1883 this.row = this.blocks.length - 1;
1884 nitems = lastcol + 1;
1886 while (this.blocks[this.row].Index > this.index)
1891 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1892 nitems = this.blocks[this.row].Count ();
1893 this.index = (this.blocks[this.row].Index
1894 + (col < nitems ? col : nitems - 1));
1899 function next_group ()
1901 var col = get_col.call (this);
1903 if (this.column > 0)
1905 this.index += this.column - col;
1906 if (this.index < this.total)
1908 if (this.index + col >= this.total)
1910 nitems = this.total - this.index;
1911 this.index = this.total - 1;
1915 nitems = this.column;
1924 while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
1930 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1931 nitems = this.blocks[this.row].Count ();
1932 this.index = (this.blocks[this.row].Index
1933 + (col < nitems ? col : nitems - 1));
1940 if (this.index == 0)
1942 this.index = this.total - 1;
1943 this.row = this.blocks.length - 1;
1948 if (this.blocks[this.row].Index > this.index)
1956 if (this.index == this.total)
1963 var b = this.blocks[this.row];
1964 if (this.index == b.Index + b.Count ())
1971 this.index -= get_col.call (this);
1972 while (this.blocks[this.row].Index > this.index)
1978 var b = this.blocks[this.row];
1979 if (this.column > 0)
1981 if (this.index + 1 < this.total)
1983 this.index += this.column - get_col.call (this) + 1;
1984 while (b.Index + b.Count () <= this.index)
1985 b = this.blocks[++this.row];
1989 this.index = b.Index + b.Count () - 1;
1992 MIM.Candidates.prototype.Current = function ()
1994 var b = this.blocks[this.row];
1995 return b.get (this.index - b.Index);
1998 MIM.Candidates.prototype.Select = function (selector)
2000 var idx = this.index;
2001 var gidx = this.column > 0 ? idx / this.column : this.row;
2002 if (selector.type == 'selector')
2004 switch (selector.val)
2006 case '@<': first.call (this); break;
2007 case '@>': last.call (this); break;
2008 case '@-': prev.call (this); break;
2009 case '@+': next.call (this); break;
2010 case '@[': prev_group.call (this); break;
2011 case '@]': next_group.call (this); break;
2018 if (this.column > 0)
2020 col = this.index % this.column;
2021 start = this.index - col;
2022 end = start + this.column;
2026 start = this.blocks[this.row].Index;
2027 col = this.index - start;
2028 end = start + this.blocks[this.row].Count;
2030 if (end > this.total)
2032 this.index += selector.val - col;
2033 if (this.index >= end)
2034 this.index = end - 1;
2035 if (this.column > 0)
2037 if (selector.val > col)
2038 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2042 while (this.blocks[this.row].Index > this.index)
2046 var newgidx = this.column > 0 ? this.index / this.column : this.row;
2047 if (this.index != idx)
2048 this.ic.changed |= (gidx == newgidx
2049 ? MIM.ChangedStatus.CandidateIndex
2050 : MIM.ChangedStatus.CandidateList);
2051 return this.Current ();
2054 MIM.Candidates.prototype.CurrentCol = function ()
2056 return get_col.call (this);
2059 MIM.Candidates.prototype.CurrentGroup = function ()
2061 var col, start, end, gnum, gidx;
2062 if (this.column > 0)
2064 gnum = Math.floor ((this.total - 1) / this.column) + 1;
2065 col = this.index % this.column;
2066 start = this.index - col;
2067 gidx = start / this.column + 1;
2068 end = start + this.column;
2069 if (end > this.total)
2074 gnum = this.blocks.length;
2075 gidx = this.row + 1;
2076 start = this.blocks[this.row].Index;
2077 col = this.index - start;
2078 end = start + this.blocks[this.row].Count ();
2080 var group = new Array ();
2081 var indices = new Array (gnum, gidx, col);
2082 group.push (indices);
2084 var block = this.blocks[row++];
2087 var c = block.get (start - block.Index);
2090 if (start == block.Index + block.Count ())
2091 block = this.blocks[row++];
2097 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2098 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2099 MIM.im_domain.DefType (MIM.Marker.prototype);
2100 MIM.im_domain.DefType (MIM.Selector.prototype);
2101 MIM.im_domain.DefType (MIM.Rule.prototype);
2102 MIM.im_domain.DefType (MIM.Map.prototype);
2103 MIM.im_domain.DefType (MIM.State.prototype);
2106 var im_domain = MIM.im_domain;
2108 function Finsert (domain, vari, args)
2111 if (args[0].type == 'integer')
2112 text = String.fromCharCode (args[0].val);
2115 domain.context.ins (text, null);
2119 function Finsert_candidates (domain, vari, args)
2121 var ic = domain.context;
2122 var gsize = domain.variables['candidates-group-size'];
2123 var candidates = new MIM.Candidates (ic, args,
2124 gsize ? gsize.val.Intval () : 0);
2125 ic.ins (candidates.Current (), candidates);
2129 function Fdelete (domain, vari, args)
2131 var ic = domain.context;
2132 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2133 return new Xex.IntTerm (ic.del (pos));
2136 function Fselect (domain, vari, args)
2138 var ic = domain.context;
2139 var can = ic.candidates;
2143 var old_text = can.Current ();
2144 var new_text = can.Select (args[0]);
2145 ic.rep (old_text, new_text, can);
2148 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2152 function Fshow (domain, vari, args)
2154 domain.context.candidate_show = true;
2155 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2159 function Fhide (domain, vari, args)
2161 domain.context.candidate_show = false;
2162 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2166 function Fchar_at (domain, vari, args)
2168 return new Xex.IntTerm (args[0].CharAt (domain.context));
2171 function Fmove (domain, vari, args)
2173 var ic = domain.context;
2174 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2176 return new Xex.IntTerm (pos);
2179 function Fmark (domain, vari, args)
2181 args[0].Mark (domain.context);
2185 function Fpushback (domain, vari, args)
2187 var a = (args[0].IsInt ? args[0].Intval ()
2188 : args[0].IsStr ? new KeySeq (args[0])
2190 domain.context.pushback (a);
2194 function Fpop (domain, vari, args)
2196 var ic = domain.context;
2197 if (ic.key_head < ic.keys.val.length)
2198 ic.keys.val.splice (ic.keys_head, 1);
2202 function Fundo (domain, vari, args)
2204 var ic = domain.context;
2205 var n = args.length == 0 ? -2 : args[0].val;
2206 Xex.Log ('undo with arg ' + args[0]);
2208 ic.keys.val.splice (ic.keys.val.length + n, -n);
2210 ic.keys.val.splice (n, ic.keys.val.length);
2215 function Fcommit (domain, vari, args)
2217 domain.context.commit ();
2221 function Funhandle (domain, vari, args)
2223 domain.context.commit ();
2224 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2227 function Fshift (domain, vari, args)
2229 var ic = domain.context;
2230 var state_name = args[0].val;
2231 var state = ic.im.state_list[state_name];
2233 throw ("Unknown state: " + state_name);
2238 function Fshiftback (domain, vari, args)
2240 domain.context.shift (null);
2244 function Fkey_count (domain, vari, args)
2246 return new Xex.IntTerm (domain.context.key_head);
2249 function Fsurrounding_flag (domain, vari, args)
2251 return new Xex.IntTerm (-1);
2254 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2255 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2256 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2257 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2258 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2259 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2260 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2261 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2262 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2263 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2264 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2265 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2266 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2267 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2268 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2269 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2270 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2271 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2276 function get_global_var (vname)
2278 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2279 MIM.im_global.Load ()
2280 return MIM.im_global.domain.variables[vname];
2283 function include (node)
2285 node = node.firstElement ();
2286 if (node.nodeName != 'tags')
2289 var lang = null, name = null, extra = null;
2290 for (node = node.firstElement (); node; node = node.nextElement ())
2292 if (node.nodeName == 'language')
2293 lang = node.firstChild.nodeValue;
2294 else if (node.nodeName == 'name')
2295 name = node.firstChild.nodeValue;
2296 else if (node.nodeName == 'extra-id')
2297 extra = node.firstChild.nodeValue;
2299 if (! lang || ! MIM.imlist[lang])
2303 if (! name || ! (im = MIM.imlist[lang][name]))
2308 if (! (im = MIM.imextra[lang][extra]))
2311 if (im.load_status != MIM.LoadStatus.Loaded
2312 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2319 parsers['description'] = function (node)
2321 this.description = node.firstChild.nodeValue;
2323 parsers['variable-list'] = function (node)
2325 for (node = node.firstElement (); node; node = node.nextElement ())
2327 var vname = node.attributes['vname'].nodeValue;
2328 if (this != MIM.im_global)
2330 var vari = get_global_var (vname);
2332 this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
2334 vname = Xex.Term.Parse (this.domain, node)
2337 parsers['command-list'] = function (node)
2340 parsers['macro-list'] = function (node)
2342 for (var n = node.firstElement (); n; n = n.nextElement ())
2343 if (n.nodeName == 'xi:include')
2345 var im = include (n);
2347 alert ('inclusion fail');
2349 for (var macro in im.domain.functions)
2351 var func = im.domain.functions[macro];
2352 if (func instanceof Xex.Macro)
2353 im.domain.CopyFunc (this.domain, macro);
2355 n = n.previousSibling;
2356 node.removeChild (n.nextSibling);
2358 Xex.Term.Parse (this.domain, node.firstElement (), null);
2360 parsers['title'] = function (node)
2362 this.title = node.firstChild.nodeValue;
2364 parsers['map-list'] = function (node)
2366 for (node = node.firstElement (); node; node = node.nextElement ())
2368 if (node.nodeName == 'xi:include')
2370 var im = include (node);
2373 alert ('inclusion fail');
2376 for (var mname in im.map_list)
2377 this.map_list[mname] = im.map_list[mname];
2381 var map = Xex.Term.Parse (this.domain, node);
2382 this.map_list[map.name] = map;
2386 parsers['state-list'] = function (node)
2388 this.domain.map_list = this.map_list;
2389 for (node = node.firstElement (); node; node = node.nextElement ())
2391 if (node.nodeName == 'xi:include')
2393 var im = include (node);
2395 alert ('inclusion fail');
2396 for (var sname in im.state_list)
2398 state = im.state_list[sname];
2399 if (! this.initial_state)
2400 this.initial_state = state;
2401 this.state_list[sname] = state;
2404 else if (node.nodeName == 'state')
2406 var state = Xex.Term.Parse (this.domain, node);
2408 state.title = this.title;
2409 if (! this.initial_state)
2410 this.initial_state = state;
2411 this.state_list[state.name] = state;
2414 delete this.domain.map_list;
2417 MIM.IM = function (lang, name, extra_id, file)
2421 this.extra_id = extra_id;
2423 this.load_status = MIM.LoadStatus.NotLoaded;
2424 this.domain = new Xex.Domain (this.lang + '-'
2425 + (this.name != 'nil'
2426 ? this.name : this.extra_id),
2427 MIM.im_domain, null);
2433 var node = Xex.Load (null, this.file);
2436 this.load_status = MIM.LoadStatus.Error;
2440 this.initial_state = null;
2441 this.state_list = {};
2442 for (node = node.firstElement (); node; node = node.nextElement ())
2444 var name = node.nodeName;
2445 var parser = parsers[name];
2447 parser.call (this, node);
2449 this.load_status = MIM.LoadStatus.Loaded;
2454 MIM.IM.prototype = proto;
2456 MIM.IC = function (im, target)
2458 if (im.load_status == MIM.LoadStatus.NotLoaded)
2460 if (im.load_status != MIM.LoadStatus.Loaded)
2461 alert ('im:' + im.name + ' error:' + im.load_status);
2463 this.target = target;
2464 this.domain = new Xex.Domain ('context', im.domain, this);
2466 this.range = new Array ();
2467 this.range[0] = this.range[1] = 0;
2469 this.initial_state = this.im.initial_state;
2470 this.keys = new MIM.KeySeq ();
2471 this.marker_positions = new Array ();
2472 this.candidate_table = new MIM.CandidateTable ();
2476 MIM.CandidateTable = function ()
2478 this.table = new Array ();
2481 MIM.CandidateTable.prototype.get = function (pos)
2483 for (var i = 0; i < this.table.length; i++)
2485 var elt = this.table[i];
2486 if (elt.from < pos && pos <= elt.to)
2491 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2493 for (var i = 0; i < this.table.length; i++)
2495 var elt = this.table[i];
2496 if (elt.from < to && elt.to > from)
2500 elt.val = candidates;
2504 this.table.push ({ from: from, to: to, val: candidates });
2507 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2509 var diff = inserted - (to - from);
2512 for (var i = 0; i < this.table.length; i++)
2514 var elt = this.table[i];
2523 MIM.CandidateTable.prototype.clear = function ()
2525 this.table.length = 0;
2528 function set_cursor (prefix, pos)
2530 this.cursor_pos = pos;
2531 var candidates = this.candidate_table.get (pos);
2532 if (this.candidates != candidates)
2534 this.candidates = candidates;
2535 this.changed |= MIM.ChangedStatus.CandidateList;
2539 function save_state ()
2541 this.state_var_values = this.domain.SaveValues ();
2542 this.state_preedit = this.preedit;
2543 this.state_key_head = this.key_head;
2544 this.state_pos = this.cursor_pos;
2547 function restore_state ()
2549 this.domain.RestoreValues (this.state_var_values);
2550 this.preedit = this.state_preedit;
2551 set_cursor.call (this, "restore", this.state_pos);
2554 function handle_key ()
2556 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
2557 + '" in ' + this.state.name + ':' + this.keymap.name
2558 + " key/state/commit-head/len:"
2559 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
2560 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
2563 if (out.index > this.key_head)
2565 this.key_head = out.index;
2566 Xex.Log (' with submap', false, true);
2567 restore_state.call (this);
2569 if (sub.map_actions)
2571 Xex.Log ('taking map actions:');
2572 if (! this.take_actions (sub.map_actions))
2575 else if (sub.submaps)
2577 Xex.Log ('no map actions');
2578 for (var i = this.state_key_head; i < this.key_head; i++)
2580 Xex.Log ('inserting key:' + this.keys.val[i].key);
2581 this.ins (this.keys.val[i].key, null);
2586 Xex.Log ('terminal:');
2587 if (this.keymap.branch_actions)
2589 Xex.Log ('branch actions:');
2590 if (! this.take_actions (this.keymap.branch_actions))
2593 if (sub != this.state.keymap)
2594 this.shift (this.state);
2599 Xex.Log (' without submap', false, true);
2601 var current_state = this.state;
2602 var map = this.keymap;
2604 if (map.branch_actions)
2606 Xex.Log ('branch actions:');
2607 if (! this.take_actions (map.branch_actions))
2611 if (map == this.keymap)
2613 Xex.Log ('no state change');
2614 if (map == this.initial_state.keymap
2615 && this.key_head < this.keys.val.length)
2617 Xex.Log ('unhandled');
2620 if (this.keymap != current_state.keymap)
2621 this.shift (current_state);
2622 else if (this.keymap.actions == null)
2623 this.shift (this.initial_state);
2632 this.cursor_pos = 0;
2633 this.candidate_show = false;
2634 this.prev_state = null;
2635 this.title = this.initial_state.title;
2636 this.state_preedit = '';
2637 this.state_key_head = 0;
2638 this.state_var_values = {};
2641 this.commit_key_head = 0;
2642 this.key_unhandled = false;
2643 this.unhandled_key = null;
2644 this.changed = MIM.ChangedStatus.None;
2645 this.error_message = '';
2646 this.title = this.initial_state.title;
2649 this.preedit_saved = '';
2650 this.candidate_table.clear ();
2651 this.candidates = null;
2652 this.candidate_show = false;
2653 for (var elt in this.marker_positions)
2654 this.marker_positions[elt] = 0;
2655 this.shift (this.initial_state);
2658 catch_args: new Array (Xex.CatchTag._mimtag, null),
2660 take_actions: function (actions)
2662 if (actions.length == 0)
2664 var func_progn = this.domain.GetFunc ('progn');
2665 var func_catch = this.domain.GetFunc ('catch');
2666 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2667 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2668 term = term.Eval (this.domain);
2669 return (! term.IsSymbol || term.val != '@mimtag');
2672 GetSurroundingChar: function (pos)
2676 pos += this.range[0];
2682 pos += this.range[1];
2683 if (pos >= this.target.value.length)
2686 return this.target.value.charCodeAt (pos);
2689 DelSurroundText: function (pos)
2694 pos += this.range[0];
2700 text = this.target.value.substring (0, pos);
2701 if (this.range[0] < this.target.value.length)
2702 text += this.target.value.substring (this.range[0]);
2703 this.target.value = text;
2704 this.range[1] -= this.range[0] - pos;
2705 this.range[0] = pos;
2709 pos += this.range[1];
2710 text = this.target.value.substring (0, this.range[1]);
2711 if (pos >= this.target.value.length)
2712 pos = this.target.value.length;
2714 text += this.target.value.substring (pos);
2715 this.target.value = text;
2719 adjust_markers: function (from, to, inserted)
2721 var diff = inserted - (to - from);
2723 for (var name in this.marker_positions)
2725 var pos = this.marker_positions[name];
2729 this.marker_positions[name] += diff;
2730 else if (pos > from)
2731 this.marker_positions[name] = from;
2736 preedit_replace: function (from, to, text, candidates)
2738 var newlen = text.length;
2739 this.preedit = (this.preedit.substring (0, from)
2740 + text + this.preedit.substring (to));
2741 this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2742 this.adjust_markers (from, to, newlen);
2743 this.candidate_table.adjust (from, to, newlen);
2745 this.candidate_table.put (from, from + newlen, candidates)
2746 if (this.cursor_pos >= to)
2747 set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
2748 else if (this.cursor_pos > from)
2749 set_cursor.call (this, 'adjust', from)
2752 ins: function (text, candidates)
2754 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2757 rep: function (old_text, new_text, candidates)
2759 this.preedit_replace (this.cursor_pos - old_text.length,
2760 this.cursor_pos, new_text, candidates);
2765 var deleted = pos - this.cursor_pos;
2766 if (pos < this.cursor_pos)
2770 this.DelSurroundText (pos);
2771 deleted = - this.cursor_pos;
2774 if (pos < this.cursor_pos)
2775 this.preedit_replace (pos, this.cursor_pos, '', null);
2779 if (pos > this.preedit.length)
2781 this.DelSurroundText (pos - this.preedit.length);
2782 deleted = this.preedit.length - this.cursor_pos;
2783 pos = this.preedit.length;
2785 if (pos > this.cursor_pos)
2786 this.preedit_replace (this.cursor_pos, pos, '', null);
2793 this.candidate_show = true;
2794 this.changed |= MIM.ChangedStatus.CandidateShow;
2799 this.candidate_show = false;
2800 this.changed |= MIM.ChangedStatus.CandidateShow;
2803 move: function (pos)
2807 else if (pos > this.preedit.length)
2808 pos = this.preedit.length;
2809 if (pos != this.cursor_pos)
2811 set_cursor.call (this, 'move', pos);
2812 this.changed |= MIM.ChangedStatus.Preedit;
2816 pushback: function (n)
2818 if (n instanceof MIM.KeySeq)
2820 if (this.key_head > 0)
2822 if (this.key_head < this.keys.val.length)
2823 this.keys.val.splice (this.key_head,
2824 this.keys.val.length - this.key_head);
2825 for (var i = 0; i < n.val.length; i++)
2826 this.keys.val.push (n.val[i]);
2832 if (this.key_head < 0)
2839 this.key_head = - n;
2840 if (this.key_head > this.keys.val.length)
2841 this.key_head = this.keys.val.length;
2847 if (this.key_head < this.keys.val.length)
2848 this.keys.val.splice (this.key_head, 1);
2853 if (this.preedit.length > 0)
2855 this.candidate_table.clear ();
2856 this.produced += this.preedit;
2857 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2858 this.preedit_saved = '';
2860 this.commit_key_head = this.key_head;
2864 shift: function (state)
2868 if (this.prev_state == null)
2870 state = this.prev_state;
2873 if (state == this.initial_state)
2878 this.keys.val.splice (0, this.key_head);
2879 this.key_head = this.state_key_head = this.commit_key_head = 0;
2880 this.prev_state = null;
2885 if (state != this.state)
2886 this.prev_state = this.state;
2888 if (state != this.state && state.enter_actions)
2889 this.take_actions (state.enter_actions);
2890 if (! this.state || this.state.title != state.title)
2891 this.changed |= MIM.ChangedStatus.StateTitle;
2893 this.keymap = state.keymap;
2894 save_state.call (this);
2897 Filter: function (key)
2901 Xex.Log ("active = false");
2902 this.key_unhandled = true;
2903 this.unhandled_key = key;
2906 if (key.key == '_reload')
2908 this.changed = MIM.ChangedStatus.None;
2910 this.key_unhandled = false;
2911 this.keys.val.push (key);
2913 while (this.key_head < this.keys.val.length)
2915 if (! handle_key.call (this))
2917 if (this.key_head < this.keys.val.length)
2919 this.unhandled_key = this.keys.val[this.key_head];
2920 this.keys.val.splice (this.key_head, this.key_head + 1);
2922 if (this.state_key_head > 0)
2923 this.state_key_head--;
2924 if (this.commit_key_head > 0)
2925 this.commit_key_head--;
2926 this.key_unhandled = true;
2932 this.key_unhandled = true;
2936 if (this.keymap == this.initial_state.keymap)
2939 if (this.commit_key_head > 0)
2941 this.keys.val.splice (0, this.commit_key_head);
2942 this.key_head -= this.commit_key_head;
2943 this.state_key_head -= this.commit_key_head;
2944 this.commit_key_head = 0;
2946 if (this.key_unhandled)
2948 this.keys.val.length = 0;
2949 //this.keys.val.splice (0, this.keys.val.length);
2950 this.key_head = this.state_key_head = this.commit_key_head = 0;
2952 if (this.changed & MIM.ChangedStatus.Candidate)
2954 if (this.candidate_show && this.candidates)
2959 return (! this.key_unhandled
2960 && this.produced.length == 0);
2964 MIM.IC.prototype = proto;
2966 var node = Xex.Load (null, "imlist.xml");
2967 for (node = node.firstChild; node; node = node.nextSibling)
2968 if (node.nodeName == 'input-method')
2970 var lang = null, name = null, extra_id = null, file = null;
2972 for (var n = node.firstChild; n; n = n.nextSibling)
2974 if (n.nodeName == 'language')
2975 lang = n.firstChild.nodeValue;
2976 else if (n.nodeName == 'name')
2977 name = n.firstChild.nodeValue;
2978 else if (n.nodeName == 'extra-id')
2979 extra_id = n.firstChild.nodeValue;
2980 else if (n.nodeName == 'filename')
2981 file = n.firstChild.nodeValue;
2983 if (name && name != 'nil')
2985 if (! MIM.imlist[lang])
2986 MIM.imlist[lang] = {};
2987 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2989 else if (extra_id && extra_id != 'nil')
2991 if (! MIM.imextra[lang])
2992 MIM.imextra[lang] = {};
2993 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2996 if (MIM.imextra.t && MIM.imextra.t.global)
2997 MIM.im_global = MIM.imextra.t.global;
3000 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
3001 MIM.im_global.load_status = MIM.LoadStatus.Error;
3007 var keys = new Array ();
3009 keys[0x08] = 'backspace';
3010 keys[0x0D] = 'return';
3011 keys[0x1B] = 'escape';
3012 keys[0x20] = 'space';
3013 keys[0x21] = 'pageup';
3014 keys[0x22] = 'pagedown';
3016 keys[0x24] = 'home';
3017 keys[0x25] = 'left';
3019 keys[0x27] = 'right';
3020 keys[0x28] = 'down';
3021 keys[0x2D] = 'insert';
3022 keys[0x2E] = 'delete';
3023 for (var i = 1; i <= 12; i++)
3024 keys[111 + i] = "f" + i;
3025 keys[0x90] = "numlock";
3026 keys[0xF0] = "capslock";
3029 keyids['U+0008'] = 'Backspace';
3030 keyids['U+0009'] = 'Tab';
3031 keyids['U+0018'] = 'Cancel';
3032 keyids['U+001B'] = 'Escape';
3033 keyids['U+0020'] = 'Space';
3034 keyids['U+007F'] = 'Delete';
3037 modifiers.Shift = 1;
3038 modifiers.Control = 1;
3040 modifiers.AltGraph = 1;
3043 MIM.decode_key_event = function (event)
3045 var key = event.keyIdentifier;
3047 if (key) // keydown event of Chrome
3052 if (event.ctrlKey) mod += 'C-';
3053 if (event.metaKey) mod += 'M-';
3054 if (event.altKey) mod += 'A-';
3055 var keysym = keyids[key];
3058 else if (key.match(/^U\+([0-9A-Z]+)$/))
3060 if (mod.length == 0)
3062 key = String.fromCharCode (parseInt (RegExp.$1, 16));
3065 //key = key.toLowerCase ();
3066 if (event.shiftKey) mod += 'S-';
3067 return new MIM.Key (mod + key);
3071 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
3072 : event.charCode ? event.charCode
3076 if (event.type == 'keydown')
3081 if (event.shiftKey) key = "S-" + key ;
3084 key = String.fromCharCode (key);
3086 if (event.altKey) key = "A-" + key ;
3087 if (event.ctrlKey) key = "C-" + key ;
3088 return new MIM.Key (key);
3092 MIM.add_event_listener
3093 = (window.addEventListener
3094 ? function (target, type, listener) {
3095 target.addEventListener (type, listener, false);
3097 : window.attachEvent
3098 ? function (target, type, listener) {
3099 target.attachEvent ('on' + type,
3101 listener.call (target, window.event);
3104 : function (target, type, listener) {
3106 = function (e) { listener.call (target, e || window.event); };
3109 MIM.debug_print = function (event, ic)
3113 if (! MIM.debug_nodes)
3115 MIM.debug_nodes = new Array ();
3116 MIM.debug_nodes['status0'] = document.getElementById ('status0');
3117 MIM.debug_nodes['status1'] = document.getElementById ('status1');
3118 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3119 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3120 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3121 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3122 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3123 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3125 var target = event.target;
3126 var code = event.keyCode;
3127 var ch = event.type == 'keypress' ? event.charCode : 0;
3128 var key = MIM.decode_key_event (event);
3131 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3132 index = (event.type == 'keydown' ? '0' : '1');
3134 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3136 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3137 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3138 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3141 MIM.debug_nodes.keypress.innerHTML = '';
3142 MIM.debug_nodes.status1.innerHTML = '';
3143 MIM.debug_nodes.keymap1.innerHTML = '';
3144 MIM.debug_nodes.preedit1.innerHTML = ''
3148 MIM.get_range = function (target, ic)
3151 if (target.selectionStart != null) // for Mozilla
3153 from = target.selectionStart;
3154 to = target.selectionEnd;
3158 var r = document.selection.createRange ();
3159 var rr = r.duplicate ();
3161 rr.moveToElementText (target);
3162 rr.setEndPoint ('EndToEnd', range);
3163 from = rr.text.length - r.text.length;
3164 to = rr.text.length;
3167 && from == ic.range[0] + ic.cursor_pos
3168 && (ic.preedit.length == 0
3169 || ic.preedit == target.value.substring (ic.range[0], ic.range[1])))
3171 Xex.Log ('reset ic');
3184 padingLeft: 'padding-left',
3185 paddingRight: 'padding-right',
3186 paddingTop: 'padding-top',
3187 paddintBottom: 'padding-bottom',
3188 borderLeftStyle: 'border-left-style',
3189 borderRightStyle: 'border-right-style',
3190 borderTopStyle: 'border-top-style',
3191 borderBottomStyle: 'border-bottom-style',
3192 borderLeftWidth: 'border-left-width',
3193 borderRightWidth: 'border-right-width',
3194 borderTopWidth: 'border-top-width',
3195 borderBottomWidth: 'border-bottom-width',
3196 fontFamily: 'font-family',
3197 fontSize: 'font-size',
3198 lineHeight: 'line-height',
3199 letterSpacing: 'letter-spacing',
3200 wordSpacing: 'word-spacing' };
3202 function copy_style (from, to)
3204 var from_style = getComputedStyle(from,'');
3205 for(var name in style_props)
3206 to.style[name] = from_style.getPropertyValue (style_props[name]);
3207 to.style.left = from.offsetLeft + 'px';
3208 to.style.top = from.offsetTop + 'px';
3209 to.style.width = from.offsetWidth;
3210 to.style.height = from.offsetHeight;
3213 MIM.get_preedit_pos = function (target, ic)
3217 temp = document.createElement ('div');
3218 temp.style.visibility = 'hidden';
3219 temp.style.position = 'absolute';
3220 temp.appendChild (document.createElement ('span'));
3221 temp.appendChild (document.createElement ('span'));
3222 document.getElementsByTagName ('body')[0].appendChild (temp);
3227 copy_style (target, temp);
3230 for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
3232 ic.abs_top += elm.offsetTop;
3233 ic.abs_left += elm.offsetLeft;
3236 temp.firstChild.innerText = target.value.substr (0, ic.range[0]);
3237 temp.lastChild.innerText = "." + target.value.substr (ic.range[0], ic.range[1]);
3238 ic.abs_y = (ic.abs_top + temp.lastChild.offsetTop
3239 + temp.lastChild.offsetHeight - target.scrollTop);
3240 ic.abs_x0 = ic.abs_left + temp.lastChild.offsetLeft;
3241 ic.abs_x1 = ic.abs_x0 + temp.lastChild.offsetWidth;
3245 MIM.set_caret = function (target, ic)
3247 if (ic.preedit.length > 0)
3249 MIM.get_preedit_pos (target, ic);
3252 ic.bar = document.createElement ('div');
3253 ic.bar.style.position = 'absolute';
3254 ic.bar.style.backgroundColor = "black";
3255 ic.bar.style.minHeight = '1px';
3256 document.getElementsByTagName ('body')[0].appendChild (ic.bar);
3258 ic.bar.style.display = 'block'
3259 ic.bar.style.top = (ic.abs_y + 1) + 'px';
3260 ic.bar.style.left = ic.abs_x0 + 'px';
3261 ic.bar.style.minWidth = (ic.abs_x1 - ic.abs_x0) + 'px';
3264 ic.bar.style.display = 'none'
3267 MIM.update = function (target, ic, for_focus_out)
3269 var text = target.value;
3270 target.value = (text.substring (0, ic.range[0])
3273 + text.substring (ic.range[1]));
3274 ic.range[0] += ic.produced.length;
3275 ic.range[1] = ic.range[0] + ic.preedit.length;
3276 MIM.set_caret (target, ic);
3277 if (! for_focus_out)
3279 var pos = ic.range[0] + ic.cursor_pos;
3280 if (target.setSelectionRange) // Mozilla
3282 var scrollTop = target.scrollTop;
3283 target.setSelectionRange (pos, pos);
3284 target.scrollTop = scrollTop;
3288 var range = target.createTextRange ();
3289 range.moveStart ('character', pos);
3290 range.moveEnd ('character', pos);
3297 MIM.show = function (ic)
3299 if (! ic.candidates)
3301 var target = ic.target;
3302 MIM.get_preedit_pos (target, ic);
3305 ic.can_node = document.createElement ('table');
3306 ic.can_node.style.position = 'absolute';
3307 ic.can_node.style.display = 'none';
3308 ic.can_node.style.backgroundColor = "white";
3309 ic.can_node.style.border = "1px solid black";
3310 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
3313 if (ic.changed & MIM.ChangedStatus.CandidateList)
3315 while (ic.can_node.childNodes.length > 0)
3316 ic.can_node.removeChild (ic.can_node.firstChild);
3317 var tr = document.createElement ('tr');
3318 var group = ic.candidates.CurrentGroup ();
3319 var td = document.createElement ('td');
3320 td.innerHTML = group[0][1] + '/' + group[0][0];
3321 td.style.color = 'white';
3322 td.style.backgroundColor = 'black';
3323 tr.appendChild (td);
3324 for (var i = 1; i < group.length; i++)
3326 var td = document.createElement ('td');
3328 td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
3329 if (i == group[0][2] + 1)
3330 td.style.backgroundColor = 'lightblue';
3331 tr.appendChild (td);
3333 ic.can_node.appendChild (tr);
3334 ic.can_node.style.top = (ic.abs_y + 10) + 'px';
3335 ic.can_node.style.left = ic.abs_x0 + 'px';
3339 var td = ic.can_node.firstElement ().firstElement ().nextElement ();
3340 var col = ic.candidates.CurrentCol ();
3341 for (var i = 0; td; td = td.nextElement ())
3342 td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
3344 ic.can_node.style.display = 'block';
3347 MIM.hide = function (ic)
3350 ic.can_node.style.display = 'none';
3354 MIM.focus_in = function (event)
3356 var target = event.target;
3357 var ic = target.mim_ic;
3358 Xex.Log ("Focus in " + target.tagName);
3359 MIM.get_range (target, ic)
3360 ic.Filter (MIM.Key.FocusIn);
3361 setTimeout (function () { MIM.update (target, ic, false); }, 100);
3365 MIM.focus_out = function (event)
3367 var target = event.target;
3368 var ic = target.mim_ic;
3369 Xex.Log ("Focus out " + target.tagName);
3370 ic.Filter (MIM.Key.FocusOut);
3371 MIM.update (target, ic, true);
3375 MIM.keydown = function (event)
3377 var target = event.target;
3378 if (! (target.type == "text" || target.type == "textarea"))
3381 var ic = target.mim_ic;
3382 if (! ic || ic.im != MIM.current)
3384 target.mim_ic = null;
3385 Xex.Log ('creating IC');
3386 ic = new MIM.IC (MIM.current, target);
3387 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3390 MIM.add_event_listener (target, 'focus', MIM.focus_in);
3391 MIM.add_event_listener (target, 'blur', MIM.focus_out);
3393 MIM.get_range (target, ic)
3394 MIM.debug_print (event, ic);
3395 ic.key = MIM.decode_key_event (event);
3399 var result = ic.Filter (ic.key);
3401 Xex.Log ('Error' + e);
3404 MIM.update (target, ic, false);
3405 if (! ic.key_unhandled)
3406 event.preventDefault ();
3410 MIM.keypress = function (event)
3412 var target = event.target;
3413 if (! (target.type == "text" || target.type == "textarea"))
3416 var ic = target.mim_ic;
3420 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3423 ic.key = MIM.decode_key_event (event);
3431 var result = ic.Filter (ic.key);
3433 Xex.Log ('Error:' + e);
3436 MIM.update (target, ic, false);
3437 if (! ic.key_unhandled)
3438 event.preventDefault ();
3440 Xex.Log ("error:" + e);
3441 event.preventDefault ();
3443 MIM.debug_print (event, ic);
3450 var lang_category = {
3452 cs: { name: 'Czech' },
3453 da: { name: 'Danish' },
3454 el: { name: 'Greek' },
3455 en: { name: 'English' },
3456 eo: { name: 'Esperanto' },
3457 fr: { name: 'French' },
3458 grc: { name: 'ClassicGreek' },
3459 hr: { name: 'Croatian' },
3460 hy: { name: 'Armenian' },
3461 ka: { name: 'Georgian' },
3462 kk: { name: 'Kazakh' },
3463 ru: { name: 'Russian' },
3464 sk: { name: 'Slovak' },
3465 sr: { name: 'Serbian' },
3466 sv: { name: 'Swedish' },
3467 yi: { name: 'Yiddish' } },
3469 ar: { name: 'Arabic' },
3470 dv: { name: 'Divehi' },
3471 fa: { name: 'Persian' },
3472 he: { name: 'Hebrew' },
3473 kk: { name: 'Kazakh' },
3474 ps: { name: 'Pushto' },
3475 ug: { name: 'Uighur' },
3476 yi: { name: 'Yiddish' } },
3478 as: { name: 'Assamese' },
3479 bn: { name: 'Bengali' },
3480 bo: { name: 'Tibetan' },
3481 gu: { name: 'Gujarati' },
3482 hi: { name: 'Hindi' },
3483 kn: { name: 'Kannada' },
3484 ks: { name: 'Kashmiri' },
3485 ml: { name: 'Malayalam' },
3486 mr: { name: 'Marathi' },
3487 ne: { name: 'Nepali' },
3488 or: { name: 'Oriya' },
3489 pa: { name: 'Panjabi' },
3490 sa: { name: 'Sanskirit' },
3491 sd: { name: 'Sindhi' },
3492 si: { name: 'Sinhalese' },
3493 ta: { name: 'Tamil' },
3494 te: { name: 'Telugu' },
3495 ur: { name: 'Urdu' } },
3497 cmc: { name: 'Cham' },
3498 km: { name: 'Khmer'},
3499 lo: { name: 'Lao' },
3500 my: { name: 'Burmese' },
3501 tai: { name: 'Tai Viet' },
3502 th: { name: 'Thai' },
3503 vi: { name: 'Vietanamese' } },
3505 ii: { name: 'Yii' },
3506 ja: { name: 'Japanese' },
3507 ko: { name: 'Korean' },
3508 zh: { name: 'Chinese' } },
3510 am: { name: 'Amharic' },
3511 ath: { name: 'Carrier' },
3512 bla: { name: 'Blackfoot' },
3513 cr: { name: 'Cree' },
3514 eo: { name: 'Esperanto' },
3515 iu: { name: 'Inuktitut' },
3516 nsk: { name: 'Naskapi' },
3517 oj: { name: 'Ojibwe' },
3518 t: { name: 'Generic' } }
3521 function categorize_im ()
3523 var cat, lang, list, name;
3524 for (lang in MIM.imlist)
3527 for (cat in lang_category)
3528 if (lang_category[cat][lang])
3530 list = lang_category[cat][lang].list;
3532 list = lang_category[cat][lang].list = {};
3533 for (name in MIM.imlist[lang])
3534 list[name] = MIM.imlist[lang][name];
3537 for (name in MIM.imlist[lang])
3538 Xex.Log ('no category ' + lang + '-' + name);
3547 clearTimeout (destroy_timer);
3548 destroy_timer = null;
3549 var target = document.getElementById ('mim-menu');
3552 for (; last_target && last_target.menu_level;
3553 last_target = last_target.parentLi)
3554 last_target.style.backgroundColor = 'white';
3555 var nodes = target.getElementsByTagName ('ul');
3556 for (var i = 0; i < nodes.length; i++)
3557 nodes[i].style.visibility = 'hidden';
3558 document.getElementsByTagName ('body')[0].removeChild (target);
3562 function destroy_menu () {
3563 if (! destroy_timer)
3564 destroy_timer = setTimeout (destroy, 1000);
3568 function show_submenu (event)
3570 var target = event.target;
3571 if (! target.menu_level)
3573 if (! target.parentNode || ! target.parentNode.menu_level)
3575 target = target.parentNode;
3579 clearTimeout (destroy_timer);
3580 destroy_timer = null;
3582 if (last_target && target.parentLi != last_target)
3584 last_target.style.backgroundColor = 'white';
3585 if (target.menu_level < last_target.menu_level)
3587 last_target = last_target.parentLi;
3588 last_target.style.backgroundColor = 'white';
3590 var uls = last_target.getElementsByTagName ('ul');
3591 for (var i = 0; i < uls.length; i++)
3592 uls[i].style.visibility = 'hidden';
3594 last_target = target;
3595 target.style.backgroundColor = 'yellow';
3596 if (target.menu_level < 3)
3598 target.lastChild.style.visibility = 'visible';
3599 target.lastChild.style.left = target.clientWidth + 'px';
3601 event.preventDefault ();
3604 function select_im (event)
3606 var target = event.target;
3609 if (! target.parentNode || ! target.parentNode.menu_level)
3611 event.preventDefault ();
3614 target = target.parentNode;
3618 MIM.current = target.im;
3621 event.preventDefault ();
3624 function create_ul (visibility)
3626 var ul = document.createElement ('ul');
3627 ul.style.position = 'absolute';
3628 ul.style.margin = '0px';
3629 ul.style.padding = '0px';
3630 ul.style.border = '1px solid gray';
3631 ul.style.borderBottom = 'none';
3632 ul.style.top = '-1px';
3633 ul.style.backgroundColor = 'white';
3634 ul.style.visibility = visibility;
3638 function create_li (level, text)
3640 var li = document.createElement ('li');
3641 li.style.position = 'relative';
3642 li.style.margin = '0px';
3643 li.style.padding = '1px';
3644 li.style.borderBottom = '1px solid gray';
3645 li.style.top = '0px';
3646 li.style.listStyle = 'none';
3647 li.menu_level = level;
3648 var nobr = document.createElement ('nobr');
3649 nobr.innerHTML = text;
3650 li.appendChild (nobr);
3656 function create_menu (event)
3658 var target = event.target;
3660 if (! ((target.type == "text" || target.type == "textarea")
3661 && event.which == 1 && event.ctrlKey))
3666 menu = create_ul ('visible');
3667 menu.style.fontFamily = 'sans-serif';
3668 menu.style.fontWeight = 'bold';
3669 menu.id = 'mim-menu';
3670 menu.onmousedown = select_im;
3671 menu.onmouseover = show_submenu;
3672 menu.onmouseout = destroy_menu;
3673 for (var catname in lang_category)
3675 var cat = lang_category[catname];
3676 var li = create_li (1, catname);
3677 var sub = create_ul ('hidden');
3678 for (var langname in cat)
3680 var lang = cat[langname];
3683 var sub_li = create_li (2, lang.name);
3684 sub_li.parentLi = li;
3685 var subsub = create_ul ('hidden');
3686 for (var name in lang.list)
3688 var im = lang.list[name];
3689 var subsub_li = create_li (3, im.name);
3690 subsub_li.parentLi = sub_li;
3692 subsub.appendChild (subsub_li);
3694 sub_li.appendChild (subsub);
3695 sub.appendChild (sub_li);
3697 li.appendChild (sub);
3698 menu.appendChild (li);
3700 lang_category = null;
3702 menu.style.left = (event.clientX - 10) + "px";
3703 menu.style.top = (event.clientY - 10) + "px";
3704 document.getElementsByTagName ('body')[0].appendChild (menu);
3707 MIM.init = function ()
3709 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3710 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3711 MIM.add_event_listener (window, 'mousedown', create_menu);
3712 if (window.location == 'http://localhost/mim/index.html')
3713 MIM.server = 'http://localhost/mim';
3714 MIM.current = MIM.imlist['zh']['py-gb'];
3718 MIM.test = function ()
3720 var im = MIM.imlist['t']['latn-post'];
3721 var ic = new MIM.IC (im, null);
3723 ic.Filter (new MIM.Key ('a'));
3724 ic.Filter (new MIM.Key ("'"));
3727 document.getElementById ('text').value = ic.produced + ic.preedit;
3730 document.getElementById ('text').value
3731 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3733 if (e instanceof Xex.ErrTerm)
3741 MIM.init_debug = function ()
3744 Xex.LogNode = document.getElementById ('xexlog');