1 // -* coding: utf-8; -*
5 Log: function (arg, indent)
10 Xex.LogNode.value = '';
14 if (indent != undefined)
15 for (var i = 0; i <= indent; i++)
17 Xex.LogNode.value = str + arg + "\n" + Xex.LogNode.value;
23 UnknownError: "unknown-error",
24 WrongArgument: "wrong-argument",
26 InvalidInteger: "invalid-integer",
27 TermTypeInvalid: "term-type-invalid",
28 FunctionConflict: "function-conflict",
29 VariableTypeConflict: "variable-type-conflict",
30 VariableRangeConflict: "variable-range-conflict",
31 VariableWrongRange: "variable-wrong-range",
32 VariableWrongValue: "variable-wrong-value",
34 UnknownFunction: "unknown-function",
35 MacroExpansionError: "macro-expansion-error",
36 NoVariableName: "no-variable-name",
37 NoFunctionName: "no-funcion-name",
40 ArithmeticError: "arithmetic-error",
41 WrongType: "wrong-type",
42 IndexOutOfRange: "index-out-of-range",
43 ValueOutOfRange: "value-out-of-range",
44 NoLoopToBreak: "no-loop-to-break",
45 UncaughtThrow: "uncaught-throw"
48 Xex.Variable = function (domain, name, desc, val, range)
57 Xex.Variable.prototype.clone = function ()
59 return new Xex.Variable (this.domain, this.name, this.desc,
60 this.val, this.range);
63 Xex.Variable.prototype.Equals = function (obj)
65 return ((obj instanceof Xex.Variable)
66 && obj.name == this.name);
69 Xex.Variable.prototype.SetValue = function (term)
75 Xex.Function = function (name, with_var, min_args, max_args)
78 this.with_var = with_var;
79 this.min_args = min_args;
80 this.max_args = max_args;
83 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
86 this.with_var = with_var;
87 this.min_args = min_args;
88 this.max_args = max_args;
89 this.builtin = builtin;
92 Xex.Subrountine.prototype.Call = function (domain, vari, args)
94 var newargs = new Array ();
95 for (var i = 0; i < args.length; i++)
97 newargs[i] = args[i].Eval (domain);
101 return this.builtin (domain, vari, newargs)
104 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
107 this.with_var = with_var;
108 this.min_args = min_args;
109 this.max_args = max_args;
110 this.builtin = builtin;
113 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
115 return this.builtin (domain, vari, args)
118 Xex.Lambda = function (name, min_args, max_args, args, body)
121 this.min_args = min_args;
122 this.max_args = max_args;
127 Xex.Lambda.prototype.Call = function (domain, vari, args)
129 var current = domain.bindings;
130 var result = Xex.Zero;
131 var limit = max_args >= 0 ? args.length : args.length - 1;
135 for (i = 0; i < limit; i++)
137 result = args[i].Eval (domain);
138 if (domain.Thrown ())
140 domain.Bind (this.args[i], result);
144 var list = new Array ();
145 for (i = 0; i < args[limit].length; i++)
147 result = args[limit].Eval (domain);
148 if (domain.Thrown ())
152 domain.Bind (this.args[limit], list);
155 domain.Catch (Xex.CatchTag.Return);
156 for (var term in this.body)
158 result = term.Eval (domain);
159 if (domain.Thrown ())
166 domain.UnboundTo (current);
171 Xex.Macro = function (name, min_args, max_args, args, body)
174 this.min_args = min_args;
175 this.max_args = max_args;
180 Xex.Macro.prototype.Call = function (domain, vari, args)
182 var current = domain.bindings;
183 var result = Xex.Zero;
187 for (i = 0; i < args.length; i++)
188 domain.Bind (this.args[i], args[i]);
190 domain.Catch (Xex.CatchTag.Return);
191 for (var i in this.body)
193 result = this.body[i].Eval (domain);
194 if (domain.Thrown ())
201 domain.UnboundTo (current);
206 Xex.Bindings = function (vari)
209 this.old_value = vari.val;
212 Xex.Bindings.prototype.UnboundTo = function (boundary)
214 for (var b = this; b != boundary; b = b.next)
215 b.vari.val = b.old_value;
219 Xex.Bind = function (bindings, vari, val)
221 var b = new Xex.Bindings (vari);
232 Xex.Domain = function (name, parent, context)
235 this.context = context;
238 if (name != 'basic' && ! parent)
239 parent = Xex.BasicDomain
240 this.parent = parent;
247 for (elt in parent.termtypes)
248 this.termtypes[elt] = parent.termtypes[elt];
249 for (elt in parent.functions)
250 this.functions[elt] = parent.functions[elt];
251 for (elt in parent.variables)
252 this.variables[elt] = parent.variables[elt];
255 this.call_stack = new Array ();
256 this.bindings = null;
257 this.catch_stack = new Array ();
258 this.catch_count = 0;
262 Xex.Domain.prototype = {
263 CallStackCount: function () { return this.call_stack.length; },
264 CallStackPush: function (term) { this.call_stack.push (term); },
265 CallStackPop: function () { this.call_stack.pop (); },
266 Bind: function (vari, val)
268 this.bindings = Xex.Bind (this.bindings, vari, val);
270 UnboundTo: function (boundary)
273 this.bindings = this.bindings.UnboundTo (boundary);
275 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
278 this.catch_stack.pop ();
279 if (this.catch_count > this.catch_stack.length)
284 if (this.catch_count < this.catch_stack.length)
286 this.caught = (this.catch_count == this.catch_stack.length - 1);
292 ThrowReturn: function ()
294 for (var i = this.catch_stack.length - 1; i >= 0; i--)
297 if (this.catch_stack[i] == Xex.CatchTag.Return)
301 ThrowBreak: function ()
303 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
304 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
305 "No surrounding loop to break");
308 ThrowSymbol: function (tag)
310 var i = this.catch_count;
311 for (var j = this.catch_stack.length - 1; j >= 0; j--)
314 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
316 this.catch_count = i;
320 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
321 "No corresponding catch: " + tag);
323 DefType: function (obj)
326 if (this.termtypes[type])
327 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
328 "Already defined: " + type);
329 if (this.functions[type])
330 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
331 "Already defined as a funciton or a macro: "
333 this.termtypes[type] = obj.Parser;
335 DefSubr: function (builtin, name, with_var, min_args, max_args)
337 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
340 DefSpecial: function (builtin, name, with_var, min_args, max_args)
342 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
345 Defun: function (name, min_args, max_args, args, body)
347 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
350 DefunByFunc: function (func) { this.functions[func.name] = func; },
351 Defmacro: function (name, min_args, max_args, args, body)
353 this.functions[name] = new Xex.Macro (name, min_args, max_args,
356 DefAlias: function (alias, fname)
358 var func = this.functions[fname];
361 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
362 if (this.termtypes[alias])
363 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
364 "Already defined as a term type: " + alias);
365 if (this.functions[alias])
366 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
367 "Already defined as a function: " + alias);
368 this.functions[alias] = func;
370 Defvar: function (name, desc, val, range)
372 var vari = new Xex.Variable (this, name, desc, val, range);
373 this.variables[name] = vari;
376 GetFunc: function (name)
378 var func = this.functions[name];
380 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
381 "Unknown function: " + name);
384 CopyFunc: function (domain, name)
386 var func = this.functions[name];
387 domain.DefunByFunc (func);
390 CopyFuncAll: function (domain)
392 for (var elt in this.functions)
393 domain.DefunByFunc (this.functions[elt]);
395 GetVarCreate: function (name)
397 var vari = this.variables[name];
399 vari = this.variables[name] = new Xex.Variable (this, name, null,
403 GetVar: function (name) { return this.variables[name]; },
404 SaveValues: function ()
407 for (var elt in this.variables)
408 values[elt] = this.variables[elt].val.Clone ();
411 RestoreValues: function (values)
416 var vari = this.variables[name];
417 vari.val = values[name];
422 Xex.Term = function (type) { this.type = type; }
423 Xex.Term.prototype = {
424 IsTrue: function () { return true; },
425 Eval: function (domain) { return this.Clone (); },
426 Clone: function (domain) { return this; },
427 Equals: function (obj)
429 return (this.type == obj.type
431 && obj.val == this.val);
433 Matches: function (obj) { return this.Equals (obj); },
434 toString: function ()
436 if (this.val != undefined)
437 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
438 return '<' + this.type + '/>';
440 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
441 "Not an integer"); },
442 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
446 Node.prototype.firstElement = function ()
448 for (var n = this.firstChild; n; n = n.nextSibling)
454 Node.prototype.nextElement = function ()
456 for (var n = this.nextSibling; n; n = n.nextSibling)
463 function parse_defvar (domain, node)
465 var name = node.attributes['vname'].nodeValue;
467 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
468 var vari = domain.variables[name];
469 var desc, val, range;
472 desc = vari.description;
476 node = node.firstElement ();
477 if (node && node.nodeName == 'description')
479 desc = node.firstChild.nodeValue;
480 node = node.nextElement ();
484 val = Xex.Term.Parse (domain, node);
485 node = node.nextElement ();
486 if (node && node.nodeName == 'possible-values')
487 for (node = node.firstElement (); node; node = node.nextElement ())
490 if (node.nodeName == 'range')
493 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
494 'Range not allowed for ' + name);
496 for (var n = node.firstElement (); n; n = n.nextElement ())
498 var v = Xex.Term.Parse (domain, n);
500 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
501 'Invalid range value: ' + val);
507 pval = Xex.Term.Parse (domain, node);
508 if (val.type != pval.type)
509 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
510 'Invalid possible value: ' + pval);
513 range = new Array ();
519 domain.Defvar (name, desc, val, range);
523 function parse_defun_head (domain, node)
525 var name = node.attributes['fname'].nodeValue;
527 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
528 var args = new Array ();
529 var nfixed = 0, noptional = 0, nrest = 0;
531 node = node.firstElement ();
532 if (node && node.nodeName == 'args')
535 for (n = n.firstElement (); n; n = n.nextElement ())
537 if (n.nodeName == 'fixed')
539 else if (n.nodeName == 'optional')
541 else if (n.nodeName == 'rest')
544 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
547 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
548 for (n = node.firstElement (); n; n = n.nextElement ())
549 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
551 args.min_args = nfixed;
552 args.max_args = nrest == 0 ? nfixed + noptional : -1;
554 if (node.nodeName == 'defun')
555 domain.Defun (name, args, null);
557 domain.Defmacro (name, args, null);
561 function parse_defun_body (domain, node)
563 var name = node.attributes['fname'].nodeValue;
564 var func = domain.GetFunc (name);
566 for (node = node.firstElement (); node; node = node.nextElement ())
567 if (node.nodeName != 'description' && node.nodeName != 'args')
569 body = Xex.Term.Parse (domain, node, null);
573 Xex.Term.Parse = function (domain, node, stop)
575 if (arguments.length == 2)
577 var name = node.nodeName;
578 var parser = domain.termtypes[name];
581 return parser (domain, node);
582 if (name == 'defun' || name == 'defmacro')
584 name = parse_defun_head (domain, node);
585 parse_defun_body (domain, node);
586 return new Xex.StrTerm (name);
588 if (name == 'defvar')
590 name = parse_defvar (domain, node);
591 return new Xex.StrTerm (name);
593 return new Xex.Funcall.prototype.Parser (domain, node);
595 for (var n = node; n && n != stop; n = n.nextElement ())
596 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
597 parse_defun_head (domain, n);
599 for (var n = node; n && n != stop; n = n.nextElement ())
601 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
602 parse_defun_body (domain, n);
603 else if (n.nodeName == 'defvar')
604 parse_defvar (domain, n);
608 terms = new Array ();
609 terms.push (Xex.Term.Parse (domain, n));
616 Xex.Varref = function (vname)
622 var proto = new Xex.Term ('varref');
624 proto.Clone = function () { return new Xex.Varref (this.val); }
625 proto.Eval = function (domain)
627 if (! this.vari || this.vari.domain != domain)
628 this.vari = domain.GetVarCreate (this.val);
629 Xex.Log (this.ToString () + '=>' + this.vari.val, domain.depth);
630 return this.vari.val;
633 proto.Parser = function (domain, node)
635 return new Xex.Varref (node.attributes['vname'].nodeValue);
638 proto.ToString = function ()
640 return '<varref vname="' + this.val + '"/>';
643 Xex.Varref.prototype = proto;
646 var null_args = new Array ();
648 Xex.Funcall = function (func, vari, args)
652 this.args = args || null_args;
656 var proto = new Xex.Term ('funcall');
658 proto.Parser = function (domain, node)
660 var fname = node.nodeName;
663 if (fname == 'funcall')
664 fname = node.attributes['fname'].nodeValue;
665 var func = domain.GetFunc (fname);
667 attr = node.attributes['vname'];
668 vari = attr != undefined ? domain.GetVarCreate (attr.nodeValue) : null;
669 var args = Xex.Term.Parse (domain, node.firstElement (), null);
670 return new Xex.Funcall (func, vari, args);
673 proto.New = function (domain, fname, vname, args)
675 var func = domain.GetFunc (fname);
676 var vari = vname ? domain.GetVarCreate (vname) : null;
677 var funcall = new Xex.Funcall (func, vari, args);
678 if (func instanceof Xex.Macro)
679 funcall = funcall.Eval (domain);
683 proto.Eval = function (domain)
685 if (! (this.func instanceof Xex.Subrountine))
686 Xex.Log (this, domain.depth);
690 result = this.func.Call (domain, this.vari, this.args);
692 Xex.Log (this + ' => ' + result, --domain.depth);
697 proto.Clone = function ()
699 return new Xex.Funcall (this.func, this.vari, this.args);
702 proto.Equals = function (obj)
704 return (obj.type == 'funcall'
705 && obj.func == this.func
706 && obj.vari.Equals (this.vari)
707 && obj.args.length == this.func.length);
710 proto.toString = function ()
713 var len = this.args.length;
714 var str = '<' + this.func.name;
716 str += ' vname="' + this.vari.name + '"';
719 if (this.func instanceof Xex.Subrountine)
720 for (var i = 0; i < len; i++)
721 arglist += this.args[i].toString ();
723 for (var i = 0; i < len; i++)
725 return str + '>' + arglist + '</' + this.func.name + '>';
728 Xex.Funcall.prototype = proto;
731 Xex.ErrTerm = function (ename, message, stack)
734 this.message = message;
739 var proto = new Xex.Term ('error');
741 proto.IsError = true;
743 proto.Parser = function (domain, node)
745 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
746 node.innerText, false);
749 proto.CallStack = function () { return stack; }
751 proto.SetCallStack = function (value) { statck = value; }
753 proto.Clone = function ()
755 return new Xex.ErrTerm (ename, message, false);
758 proto.Equals = function (obj)
761 && obj.ename == ename && obj.message == message
762 && (obj.stack ? (stack && stack.length == obj.stack.length)
766 proto.Matches = function (obj)
768 return (obj.IsError && obj.ename == ename);
771 proto.toString = function ()
773 return '<error ename="' + this.ename + '">' + this.message + '</error>';
776 Xex.ErrTerm.prototype = proto;
779 Xex.IntTerm = function (num) { this.val = num; };
781 var proto = new Xex.Term ('integer');
783 proto.Intval = function () { return this.val; };
784 proto.IsTrue = function () { return this.val != 0; }
785 proto.Clone = function () { return new Xex.IntTerm (this.val); }
786 proto.Parser = function (domain, node)
788 var str = node.firstChild.nodeValue;
790 if (str.charAt (0) == '?' && str.length == 2)
791 return new Xex.IntTerm (str.charCodeAt (1));
792 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
794 Xex.IntTerm.prototype = proto;
797 Xex.StrTerm = function (str) { this.val = str; };
799 var proto = new Xex.Term ('string');
801 proto.Strval = function () { return this.val; };
802 proto.IsTrue = function () { return this.val.length > 0; }
803 proto.Clone = function () { return new Xex.StrTerm (this.val); }
804 proto.Parser = function (domain, node)
806 return new Xex.StrTerm (node.firstChild ? node.firstChild.nodeValue : '');
808 Xex.StrTerm.prototype = proto;
811 Xex.SymTerm = function (str) { this.val = str; };
813 var proto = new Xex.Term ('symbol');
814 proto.IsSymbol = true;
815 proto.IsTrue = function () { return this.val != 'nil'; }
816 proto.Clone = function () { return new Xex.SymTerm (this.val); }
817 proto.Parser = function (domain, node)
819 return new Xex.SymTerm (node.firstChild.nodeValue);
821 Xex.SymTerm.prototype = proto;
824 Xex.LstTerm = function (list) { this.val = list; };
826 var proto = new Xex.Term ('list');
828 proto.IsTrue = function () { return this.val.length > 0; }
829 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
831 proto.Equals = function (obj)
833 if (obj.type != 'list' || obj.val.length != this.val.length)
835 var i, len = this.val.length;
836 for (i = 0; i < len; i++)
837 if (! this.val[i].Equals (obj.val[i]))
842 proto.Parser = function (domain, node)
844 var list = Xex.Term.Parse (domain, node.firstElement (), null);
845 return new Xex.LstTerm (list);
848 proto.toString = function ()
850 var len = this.val.length;
855 for (var i = 0; i < len; i++)
856 str += this.val[i].toString ();
857 return str + '</list>';
859 Xex.LstTerm.prototype = proto;
863 var basic = new Xex.Domain ('basic', null, null);
865 function Fset (domain, vari, args)
868 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
869 'No variable name to set');
870 vari.SetValue (args[0]);
874 function Fnot (domain, vari, args)
876 return (args[0].IsTrue () ? Xex.Zero : Xex.One);
879 function maybe_set_intvar (vari, n)
881 var term = new Xex.IntTerm (n);
883 vari.SetValue (term);
887 function Fadd (domain, vari, args)
889 var n = vari ? vari.val.Intval () : 0;
890 var len = args.length;
892 for (var i = 0; i < len; i++)
893 n += args[i].Intval ();
894 return maybe_set_intvar (vari, n);
897 function Fmul (domain, vari, args)
899 var n = vari ? vari.val.Intval () : 1;
900 for (var i = 0; i < args.length; i++)
902 return maybe_set_intvar (vari, n);
905 function Fsub (domain, vari, args)
911 n = args[0].Intval ();
916 n = vari.val.Intval ();
919 while (i < args.length)
920 n -= args[i++].Intval ();
921 return maybe_set_intvar (vari, n);
924 function Fdiv (domain, vari, args)
930 n = args[0].Intval ();
935 n = vari.val.Intval ();
938 while (i < args.length)
939 n /= args[i++].Intval ();
940 return maybe_set_intvar (vari, n);
943 function Fmod (domain, vari, args)
945 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
948 function Flogior (domain, vari, args)
950 var n = vari == null ? 0 : vari.val;
951 for (var i = 0; i < args.length; i++)
953 return maybe_set_intvar (vari, n);
956 function Flogand (domain, vari, args)
961 Xex.Log ("logand arg args[0]" + args[0]);
962 n = args[0].Intval ()
967 Xex.Log ("logand arg var " + vari);
968 n = vari.val.Intval ();
971 while (n > 0 && i < args.length)
973 Xex.Log ("logand arg " + args[i]);
976 return maybe_set_intvar (vari, n);
979 function Flsh (domain, vari, args)
981 return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
984 function Frsh (domain, vari, args)
986 return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
989 function Fand (domain, vari, args)
991 var len = args.length;
992 for (var i = 0; i < len; i++)
994 var result = args[i].Eval (domain);
995 if (domain.Thrown ())
997 if (! result.IsTrue ())
1003 function For (domain, vari, args)
1005 var len = args.length;
1006 for (var i = 0; i < len; i++)
1008 var result = args[i].Eval (domain);
1009 if (domain.Thrown ())
1011 if (result.IsTrue ())
1017 function Feq (domain, vari, args)
1019 for (var i = 1; i < args.length; i++)
1020 if (! args[i - 1].Equals (args[i]))
1025 function Fnoteq (domain, vari, args)
1027 return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1030 function Flt (domain, vari, args)
1032 var n = args[0].Intval ();
1034 for (var i = 1; i < args.length; i++)
1036 var n1 = args[i].Intval ();
1044 function Fle (domain, vari, args)
1046 var n = args[0].Intval ();
1047 for (var i = 1; i < args.length; i++)
1049 var n1 = args[i].Intval ();
1057 function Fgt (domain, vari, args)
1059 var n = args[0].Intval ();
1060 for (var i = 1; i < args.length; i++)
1062 var n1 = args[i].Intval ();
1070 function Fge (domain, vari, args)
1072 var n = args[0].Intval ();
1073 for (var i = 1; i < args.Length; i++)
1075 var n1 = args[i].Intval ();
1083 function Fprogn (domain, vari, args)
1085 var result = Xex.One;
1086 var len = args.length;
1088 for (var i = 0; i < len; i++)
1090 result = args[i].Eval (domain);
1091 if (domain.Thrown ())
1097 function Fif (domain, vari, args)
1099 var result = args[0].Eval (domain);
1101 if (domain.Thrown ())
1103 if (result.IsTrue ())
1104 return args[1].Eval (domain);
1105 if (args.length == 2)
1107 return args[2].Eval (domain);
1110 function Fcond (domain, vari, args)
1112 for (var i = 0; i < args.length; i++)
1115 var result = list.val[0].Eval (domain);
1116 if (result.IsTrue ())
1118 for (var j = 1; j < list.val.length; j++)
1121 result = list.val[j].Eval (domain);
1123 if (domain.Thrown ())
1132 function eval_terms (domain, terms, idx)
1134 var result = Xex.Zero;
1135 domain.caught = false;
1136 for (var i = idx; i < terms.length; i++)
1138 result = terms[i].Eval (domain);
1139 if (domain.Thrown ())
1145 function Fcatch (domain, vari, args)
1150 if (args[0].IsError)
1153 result = eval_terms (domain, args, 1);
1155 if (e instanceof Xex.ErrTerm)
1157 if (! args[0].Matches (e))
1165 else if (args[0].IsSymbol)
1168 domain.Catch (args[0].val);
1169 result = eval_terms (domain, args, 1);
1173 vari.SetValue (result);
1181 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1182 "Not a symbol nor an error: " + args[0]);
1185 function Fthrow (domain, vari, args)
1187 if (args[0].IsSymbl)
1189 domain.ThrowSymbol (args[0]);
1190 return (args[args.length - 1]);
1192 if (args[0].IsError)
1196 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1197 "Not a symbol nor an error:" + args[0]);
1200 Xex.BasicDomain = basic;
1202 basic.DefSubr (Fset, "set", true, 1, 1);
1203 basic.DefAlias ("=", "set");
1204 basic.DefSubr (Fnot, "not", false, 1, 1);
1205 basic.DefAlias ("!", "not");
1206 basic.DefSubr (Fadd, "add", true, 1, -1);
1207 basic.DefSubr (Fmul, "mul", true, 1, -1);
1208 basic.DefAlias ("*", "mul");
1209 basic.DefSubr (Fsub, "sub", true, 1, -1);
1210 basic.DefAlias ("-", "sub");
1211 basic.DefSubr (Fdiv, "div", true, 1, -1);
1212 basic.DefAlias ("/", "div");
1213 basic.DefSubr (Fmod, "mod", true, 1, 2);
1214 basic.DefAlias ("%", "mod");
1215 basic.DefSubr (Flogior, "logior", true, 1, -1);
1216 basic.DefAlias ('|', "logior");
1217 basic.DefSubr (Flogand, "logand", true, 1, -1);
1218 basic.DefAlias ("&", "logand");
1219 basic.DefSubr (Flsh, "lsh", true, 1, 2);
1220 basic.DefAlias ("<<", "lsh");
1221 basic.DefSubr (Frsh, "rsh", true, 1, 2);
1222 basic.DefAlias (">>", "rsh");
1223 basic.DefSubr (Feq, "eq", false, 2, -1);
1224 basic.DefAlias ("==", "eq");
1225 basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1226 basic.DefAlias ("!=", "noteq");
1227 basic.DefSubr (Flt, "lt", false, 2, -1);
1228 basic.DefAlias ("<", "lt");
1229 basic.DefSubr (Fle, "le", false, 2, -1);
1230 basic.DefAlias ("<=", "le");
1231 basic.DefSubr (Fgt, "gt", false, 2, -1);
1232 basic.DefAlias (">", "gt");
1233 basic.DefSubr (Fge, "ge", false, 2, -1);
1234 basic.DefAlias (">=", "ge");
1235 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1237 //basic.DefSubr (Fappend, "append", true, 0, -1);
1238 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1239 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1240 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1241 //basic.DefSubr (Fins, "ins", true, 2, 2);
1242 //basic.DefSubr (Fdel, "del", true, 2, 2);
1243 //basic.DefSubr (Feval, "eval", false, 1, 1);
1244 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1245 //basic.DefSubr (Freturn, "return", false, 0, 1);
1246 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1248 basic.DefSpecial (Fand, "and", false, 1, -1);
1249 basic.DefAlias ("&&", "and");
1250 basic.DefSpecial (For, "or", false, 1, -1);
1251 basic.DefAlias ("||", "or");
1252 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1253 basic.DefAlias ("expr", "progn");
1254 basic.DefSpecial (Fif, "if", false, 2, 3);
1255 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1256 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1257 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1258 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1259 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1260 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1261 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1262 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1264 basic.DefType (Xex.Funcall.prototype);
1265 basic.DefType (Xex.Varref.prototype);
1266 basic.DefType (Xex.ErrTerm.prototype);
1267 basic.DefType (Xex.IntTerm.prototype);
1268 basic.DefType (Xex.StrTerm.prototype);
1269 basic.DefType (Xex.SymTerm.prototype);
1270 basic.DefType (Xex.LstTerm.prototype);
1274 Xex.Zero = new Xex.IntTerm (0);
1275 Xex.One = new Xex.IntTerm (1);
1276 Xex.nil = new Xex.SymTerm ('nil');
1278 Xex.Load = function (server, file)
1280 var obj = new XMLHttpRequest ();
1281 var url = server ? server + '/' + file : file;
1282 obj.open ('GET', url, false);
1283 obj.overrideMimeType ('text/xml');
1285 return obj.responseXML.firstChild;
1289 // URL of the input method server.
1290 server: "http://www.m17n.org/common/mim-js",
1291 // Boolean flag to tell if MIM is active or not.
1293 // Boolean flag to tell if MIM is running in debug mode or not.
1295 // List of main input methods.
1297 // List of extra input methods;
1299 // Global input method data
1301 // Currently selected input method.
1305 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1312 CandidateIndex:0x10,
1314 Preedit: 0x06, // PreeditText | CursorPos
1315 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1337 ParseError: "parse-error"
1342 var keysyms = new Array ();
1343 keysyms["bs"] = "backspace";
1344 keysyms["lf"] = "linefeed";
1345 keysyms["cr"] = keysyms["enter"] = "return";
1346 keysyms["esc"] = "escape";
1347 keysyms["spc"] = "space";
1348 keysyms["del"] = "delete";
1350 function decode_keysym (str) {
1351 if (str.length == 1)
1353 var parts = str.split ("-");
1354 var len = parts.length, i;
1355 var has_modifier = len > 1;
1357 for (i = 0; i < len - 1; i++)
1358 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1360 var key = parts[len - 1];
1363 key = keysyms[key.toLowerCase ()];
1369 for (i = 1; i < len - 1; i++)
1370 str += '-' + parts[i];
1379 parts = new Array ();
1386 MIM.Key = function (val)
1389 this.has_modifier = false;
1390 if (val instanceof Xex.Term)
1392 else if (typeof val == 'string' || val instanceof String)
1394 this.key = decode_keysym (val);
1396 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1397 if (this.key instanceof Array)
1399 this.key = this.key[0];
1400 this.has_modifier = true;
1403 else if (typeof val == 'number' || val instanceof Number)
1404 this.key = String.fromCharCode (val);
1406 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1409 MIM.Key.prototype.toString = function () { return this.key; };
1411 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1412 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1413 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1417 MIM.KeySeq = function (seq)
1419 this.val = new Array ();
1420 this.has_modifier = false;
1426 var len = seq.val.length;
1427 for (var i = 0; i < len; i++)
1429 var v = seq.val[i], key;
1430 if (v.type == 'symbol' || v.type == 'string')
1431 key = new MIM.Key (v);
1432 else if (v.type == 'integer')
1433 key = new MIM.Key (v.val);
1435 throw new Xex.ErrTerm (MIM.Error.ParseError,
1436 "Invalid key: " + v);
1437 this.val.push (key);
1438 if (key.has_modifier)
1439 this.has_modifier = true;
1444 var len = seq.val.length;
1445 for (var i = 0; i < len; i++)
1446 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1449 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1453 var proto = new Xex.Term ('keyseq');
1454 proto.Clone = function () { return this; }
1455 proto.Parser = function (domain, node)
1457 var seq = new Array ();
1458 for (node = node.firstChild; node; node = node.nextSibling)
1459 if (node.nodeType == 1)
1461 var term = Xex.Term.Parse (domain, node);
1462 return new MIM.KeySeq (term);
1464 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1466 proto.toString = function ()
1468 var len = this.val.length;
1472 var str = '<keyseq>';
1473 for (var i = 0; i < len; i++)
1477 else if (this.has_modifier)
1479 str += this.val[i].toString ();
1481 return str + '</keyseq>';
1484 MIM.KeySeq.prototype = proto;
1488 MIM.Marker = function () { }
1489 MIM.Marker.prototype = new Xex.Term ('marker');
1490 MIM.Marker.prototype.CharAt = function (ic)
1492 var p = this.Position (ic);
1493 if (p < 0 || p >= ic.preedit.length)
1495 return ic.preedit.charCodeAt (p);
1498 MIM.FloatingMarker = function (name) { this.val = name; };
1499 var proto = new MIM.Marker ();
1500 MIM.FloatingMarker.prototype = proto;
1501 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1502 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1504 MIM.PredefinedMarker = function (name) { this.val = name; }
1505 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1506 MIM.PredefinedMarker.prototype.Position = function (ic)
1508 if (typeof this.pos == 'number')
1510 return this.pos (ic);
1513 var predefined = { }
1515 function def_predefined (name, position)
1517 predefined[name] = new MIM.PredefinedMarker (name);
1518 predefined[name].pos = position;
1521 def_predefined ('@<', 0);
1522 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1523 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1524 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1525 def_predefined ('@[', function (ic) {
1526 if (ic.cursor_pos > 0)
1528 var pos = ic.cursor_pos;
1529 return ic.preedit.FindProp ('candidates', pos - 1).from;
1533 def_predefined ('@]', function (ic) {
1534 if (ic.cursor_pos < ic.preedit.length - 1)
1536 var pos = ic.cursor_pos;
1537 return ic.preedit.FindProp ('candidates', pos).to;
1539 return ic.preedit.length;
1541 for (var i = 0; i < 10; i++)
1542 def_predefined ("@" + i, i);
1543 predefined['@first'] = predefined['@<'];
1544 predefined['@last'] = predefined['@>'];
1545 predefined['@previous'] = predefined['@-'];
1546 predefined['@next'] = predefined['@+'];
1547 predefined['@previous-candidate-change'] = predefined['@['];
1548 predefined['@next-candidate-change'] = predefined['@]'];
1550 MIM.SurroundMarker = function (name)
1553 this.distance = parseInt (name.slice (1));
1554 if (isNaN (this.distance))
1555 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1557 MIM.SurroundMarker.prototype = new MIM.Marker ();
1558 MIM.SurroundMarker.prototype.Position = function (ic)
1560 return ic.cursor_pos + this.distance;
1562 MIM.SurroundMarker.prototype.CharAt = function (ic)
1564 if (this.val == '@-0')
1566 var p = this.Position (ic);
1568 return ic.GetSurroundingChar (p);
1569 else if (p >= ic.preedit.length)
1570 return ic.GetSurroundingChar (p - ic.preedit.length);
1571 return ic.preedit.charCodeAt (p);
1574 MIM.Marker.prototype.Parser = function (domain, node)
1576 var name = node.firstChild.nodeValue;
1577 if (name.charAt (0) == '@')
1579 var n = predefined[name];
1582 if (name.charAt (1) == '-' || name.charAt (1) == '+')
1583 return new MIM.SurroundMarker (name);
1584 throw new Xex.ErrTerm (MIM.Error.ParseError,
1585 "Invalid marker: " + name);
1587 return new MIM.FloatingMarker (name);;
1591 MIM.Selector = function (name)
1595 MIM.Selector.prototype = new Xex.Term ('selector');
1599 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1600 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1601 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1602 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1603 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1604 selectors["@["] = selectors["@previous-candidate-change"]
1605 = new MIM.Selector ('@[');
1606 selectors["@]"] = selectors["@next-candidate-change"]
1607 = new MIM.Selector ('@]');
1609 MIM.Selector.prototype.Parser = function (domain, node)
1611 var name = node.firstChild.nodeValue;
1612 var s = selectors[name];
1614 throw new Xex.ErrTerm (MIM.Error.ParseError,
1615 "Invalid selector: " + name);
1620 MIM.Rule = function (keyseq, actions)
1622 this.keyseq = keyseq;
1623 this.actions = actions;
1625 MIM.Rule.prototype = new Xex.Term ('rule');
1626 MIM.Rule.prototype.Parser = function (domain, node)
1629 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1631 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1632 var keyseq = Xex.Term.Parse (domain, n);
1633 if (keyseq.type != 'keyseq')
1634 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1635 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1636 return new MIM.Rule (keyseq, actions);
1638 MIM.Rule.prototype.toString = function ()
1643 MIM.Map = function (name)
1646 this.rules = new Array ();
1650 var proto = new Xex.Term ('map');
1652 proto.Parser = function (domain, node)
1654 var name = node.attributes['mname'].nodeValue;
1656 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1657 var map = new MIM.Map (name);
1658 for (var n = node.firstChild; n; n = n.nextSibling)
1659 if (n.nodeType == 1)
1660 map.rules.push (Xex.Term.Parse (domain, n));
1664 proto.toString = function ()
1666 var str = '<map mname="' + this.name + '">';
1667 var len = this.rules.length;
1668 for (i = 0; i < len; i++)
1669 str += this.rules[i];
1670 return str + '</map>';
1673 MIM.Map.prototype = proto;
1676 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1678 MIM.Action = function (domain, terms)
1680 var args = new Array ();
1681 args.push (Xex.CatchTag_.mimtag);
1682 for (var i = 0; i < terms.length; i++)
1683 args.push (terms[i]);
1684 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1687 MIM.Action.prototype.Run = function (domain)
1689 var result = this.action.Eval (domain);
1690 if (result.type == 'error')
1692 domain.context.Error = result.toString ();
1695 return (result != Xex.CatchTag._mimtag);
1698 MIM.Keymap = function ()
1701 this.submaps = null;
1707 function add_rule (keymap, rule, branch_actions)
1709 var keyseq = rule.keyseq;
1710 var len = keyseq.val.length;
1713 for (var i = 0; i < len; i++)
1715 var key = keyseq.val[i];
1719 if (! keymap.submaps)
1720 keymap.submaps = {};
1722 sub = keymap.submaps[key.key];
1724 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1728 keymap.map_actions = rule.actions;
1729 keymap.branch_actions = branch_actions;
1732 proto.Add = function (map, branch_actions)
1734 var rules = map.rules;
1735 var len = rules.length;
1737 for (var i = 0; i < len; i++)
1738 add_rule (this, rules[i], branch_actions);
1740 proto.Lookup = function (keys, index)
1744 if (index < keys.val.length && this.submaps
1745 && (sub = this.submaps[keys.val[index].key]))
1748 return sub.Lookup (keys, index);
1750 return { map: this, index: index };
1753 MIM.Keymap.prototype = proto;
1756 MIM.State = function (name)
1759 this.keymap = new MIM.Keymap ();
1763 var proto = new Xex.Term ('state');
1765 proto.Parser = function (domain, node)
1767 var map_list = domain.map_list;
1768 var name = node.attributes['sname'].nodeValue;
1770 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1771 var state = new MIM.State (name);
1772 for (node = node.firstElement (); node; node = node.nextElement ())
1774 if (node.nodeName == 'title')
1775 state.title = node.firstChild.nodeValue;
1778 var n = node.firstElement ();
1779 if (node.nodeName == 'branch')
1780 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1781 Xex.Term.Parse (domain, n, null));
1782 else if (node.nodeName == 'state-hook')
1783 state.enter_actions = Xex.Term.Parse (domain, n, null);
1784 else if (node.nodeName == 'catch-all-branch')
1785 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1791 proto.toString = function ()
1793 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1796 MIM.State.prototype = proto;
1800 function Block (index, term)
1804 this.Data = term.val;
1805 else if (term.IsList)
1807 this.Data = new Array ();
1808 for (var i = 0; i < term.val.length; i++)
1809 this.Data.push (term.val[i].val);
1813 Block.prototype.Count = function () { return this.Data.length; }
1814 Block.prototype.get = function (i)
1816 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1819 MIM.Candidates = function (candidates, column)
1821 this.column = column;
1825 this.blocks = new Array ();
1827 for (var i = 0; i < candidates.length; i++)
1829 var block = new Block (this.total, candidates[i]);
1830 this.blocks.push (block);
1831 this.total += block.Count ();
1837 return (this.column > 0 ? this.index % this.column
1838 : this.index - this.blocks[this.row].Index);
1841 function prev_group ()
1843 var col = get_col.call (this);
1845 if (this.column > 0)
1847 this.index -= this.column;
1848 if (this.index >= 0)
1849 nitems = this.column;
1852 var lastcol = (this.total - 1) % this.column;
1853 this.index = (col < lastcol ? this.total - lastcol + col
1855 this.row = this.blocks.length - 1;
1856 nitems = lastcol + 1;
1858 while (this.blocks[this.row].Index > this.index)
1863 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1864 nitems = this.blocks[this.row].Count ();
1865 this.index = (this.blocks[this.row].Index
1866 + (col < nitems ? col : nitems - 1));
1871 function next_group ()
1873 var col = get_col.call (this);
1875 if (this.column > 0)
1877 this.index += this.column - col;
1878 if (this.index < this.total)
1880 if (this.index + col >= this.total)
1882 nitems = this.total - this.index;
1883 this.index = this.total - 1;
1887 nitems = this.column;
1896 while (this.blocks[this.row].Index > this.index)
1901 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1902 nitems = this.blocks[this.row].Count ();
1903 this.index = (this.blocks[this.row].Index
1904 + (col < nitems ? col : nitems - 1));
1911 if (this.index == 0)
1913 this.index = this.total - 1;
1914 this.row = this.blocks.length - 1;
1919 if (this.blocks[this.row].Index > this.index)
1927 if (this.index == this.total)
1934 var b = this.blocks[this.row];
1935 if (this.index == b.Index + b.Count ())
1942 this.index -= get_col.call (this);
1943 while (this.blocks[this.row].Index > this.index)
1949 var b = this.blocks[this.row];
1950 if (this.column > 0)
1952 if (this.index + 1 < this.total)
1954 this.index += this.column - get_col.call (this) + 1;
1955 while (b.Index + b.Count () <= this.index)
1956 b = this.blocks[++this.row];
1960 this.index = b.Index + b.Count () - 1;
1963 MIM.Candidates.prototype.Current = function ()
1965 var b = this.blocks[this.row];
1966 return b.get (this.index - b.Index);
1969 MIM.Candidates.prototype.Select = function (selector)
1971 if (selector.type == 'selector')
1973 switch (selector.val)
1975 case '@<': first.call (this); break;
1976 case '@>': last.call (this); break;
1977 case '@-': prev.call (this); break;
1978 case '@+': next.call (this); break;
1979 case '@[': prev_group.call (this); break;
1980 case '@]': next_group.cal (this); break;
1983 return this.Current ();
1986 if (this.column > 0)
1988 col = this.index % this.column;
1989 start = this.index - col;
1990 end = start + this.column;
1994 start = this.blocks[this.row].Index;
1995 col = this.index - start;
1996 end = start + this.blocks[this.row].Count;
1998 if (end > this.total)
2000 this.index += selector.val - col;
2001 if (this.index >= end)
2002 this.index = end - 1;
2003 if (this.column > 0)
2005 if (selector.val > col)
2006 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2010 while (this.blocks[this.row].Index > this.index)
2013 return this.Current ();
2017 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2018 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2019 MIM.im_domain.DefType (MIM.Marker.prototype);
2020 MIM.im_domain.DefType (MIM.Selector.prototype);
2021 MIM.im_domain.DefType (MIM.Rule.prototype);
2022 MIM.im_domain.DefType (MIM.Map.prototype);
2023 MIM.im_domain.DefType (MIM.State.prototype);
2026 var im_domain = MIM.im_domain;
2028 function Finsert (domain, vari, args)
2031 if (args[0].type == 'integer')
2032 text = String.fromCharCode (args[0].val);
2035 domain.context.ins (text, null);
2039 function Finsert_candidates (domain, vari, args)
2041 var ic = domain.context;
2042 var gsize = domain.variables['candidates_group_size'];
2043 var candidates = new MIM.Candidates (args, gsize ? gsize.Intval () : 0);
2044 ic.ins (candidates.Current (), candidates);
2048 function Fdelete (domain, vari, args)
2050 var ic = domain.context;
2051 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2052 return new Xex.IntTerm (ic.del (pos));
2055 function Fselect (domain, vari, args)
2057 var ic = domain.context;
2058 var can = ic.candidates;
2062 var old_text = can.Current ();
2063 var new_text = can.Select (args[0]);
2064 ic.rep (old_text, new_text, can);
2067 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2071 function Fshow (domain, vari, args)
2073 domain.context.candidate_show = true;
2074 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2078 function Fhide (domain, vari, args)
2080 domain.context.candidate_show = false;
2081 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2085 function Fchar_at (domain, vari, args)
2087 return new Xex.IntTerm (args[0].CharAt (domain.context));
2090 function Fmove (domain, vari, args)
2092 var ic = domain.context;
2093 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2095 return new Xex.IntTerm (pos);
2098 function Fmark (domain, vari, args)
2100 args[0].Mark (domain.context);
2104 function Fpushback (domain, vari, args)
2106 var a = (args[0].IsInt ? args[0].Intval ()
2107 : args[0].IsStr ? new KeySeq (args[0])
2109 domain.context.pushback (a);
2113 function Fpop (domain, vari, args)
2115 var ic = domain.context;
2116 if (ic.key_head < ic.keys.val.length)
2117 ic.keys.val.splice (ic.keys_head, 1);
2121 function Fundo (domain, vari, args)
2123 var ic = domain.context;
2124 var n = args.length == 0 ? -2 : args[0].val;
2126 ic.keys.val.splice (ic.keys.val.length + n, -n);
2128 ic.keys.val.splice (n, ic.keys.val.length);
2133 function Fcommit (domain, vari, args)
2135 domain.context.commit ();
2139 function Funhandle (domain, vari, args)
2141 domain.context.commit ();
2142 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2145 function Fshift (domain, vari, args)
2147 var ic = domain.context;
2148 var state_name = args[0].val;
2149 var state = ic.im.state_list[state_name];
2151 throw ("Unknown state: " + state_name);
2156 function Fshiftback (domain, vari, args)
2158 domain.context.shift (null);
2162 function Fkey_count (domain, vari, args)
2164 return new Xex.IntTerm (domain.context.key_head);
2167 function Fsurrounding_flag (domain, vari, args)
2169 return new Xex.IntTerm (-1);
2172 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2173 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2174 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2175 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2176 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2177 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2178 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2179 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2180 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2181 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2182 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2183 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2184 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2185 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2186 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2187 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2188 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2189 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2194 function get_global_var (vname)
2196 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2197 MIM.im_global.Load ()
2198 return MIM.im_global.domain.variables[vname];
2201 function include (node)
2203 node = node.firstElement ();
2204 if (node.nodeName != 'tags')
2207 var lang = null, name = null, extra = null;
2208 for (node = node.firstElement (); node; node = node.nextElement ())
2210 if (node.nodeName == 'language')
2211 lang = node.firstChild.nodeValue;
2212 else if (node.nodeName == 'name')
2213 name = node.firstChild.nodeValue;
2214 else if (node.nodeName == 'extra-id')
2215 extra = node.firstChild.nodeValue;
2217 if (! lang || ! MIM.imlist[lang])
2221 if (! name || ! (im = MIM.imlist[lang][name]))
2226 if (! (im = MIM.imextra[lang][extra]))
2229 if (im.load_status != MIM.LoadStatus.Loaded
2230 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2237 parsers['description'] = function (node)
2239 this.description = node.firstChild.nodeValue;
2241 parsers['variable-list'] = function (node)
2243 for (node = node.firstElement (); node; node = node.nextElement ())
2245 var vname = node.attributes['vname'].nodeValue;
2246 if (this != MIM.im_global)
2248 var vari = get_global_var (vname);
2250 this.domain.Defvar (vname);
2252 vname = Xex.Term.Parse (this.domain, node)
2255 parsers['command-list'] = function (node)
2258 parsers['macro-list'] = function (node)
2260 for (var n = node.firstElement (); n; n = n.nextElement ())
2261 if (n.nodeName == 'xi:include')
2263 var im = include (n);
2265 alert ('inclusion fail');
2267 for (var macro in im.domain.functions)
2269 var func = im.domain.functions[macro];
2270 if (func instanceof Xex.Macro)
2271 im.domain.CopyFunc (this.domain, macro);
2273 n = n.previousSibling;
2274 node.removeChild (n.nextSibling);
2276 Xex.Term.Parse (this.domain, node.firstElement (), null);
2278 parsers['title'] = function (node)
2280 this.title = node.firstChild.nodeValue;
2282 parsers['map-list'] = function (node)
2284 for (node = node.firstElement (); node; node = node.nextElement ())
2286 if (node.nodeName == 'xi:include')
2288 var im = include (node);
2291 alert ('inclusion fail');
2294 for (var mname in im.map_list)
2296 this.map_list[mname] = im.map_list[mname];
2297 Xex.Log ('include map ' + mname);
2302 var map = Xex.Term.Parse (this.domain, node);
2303 this.map_list[map.name] = map;
2307 parsers['state-list'] = function (node)
2309 this.domain.map_list = this.map_list;
2310 for (node = node.firstElement (); node; node = node.nextElement ())
2312 if (node.nodeName == 'xi:include')
2314 var im = include (node);
2316 alert ('inclusion fail');
2317 for (var sname in im.state_list)
2319 state = im.state_list[sname];
2320 if (! this.initial_state)
2321 this.initial_state = state;
2322 this.state_list[sname] = state;
2323 Xex.Log ('include state ' + sname);
2326 else if (node.nodeName == 'state')
2328 var state = Xex.Term.Parse (this.domain, node);
2330 state.title = this.title;
2331 if (! this.initial_state)
2332 this.initial_state = state;
2333 this.state_list[state.name] = state;
2336 delete this.domain.map_list;
2339 MIM.IM = function (lang, name, extra_id, file)
2343 this.extra_id = extra_id;
2345 this.load_status = MIM.LoadStatus.NotLoaded;
2346 this.domain = new Xex.Domain (this.lang + '-'
2347 + (this.name != 'nil'
2348 ? this.name : this.extra_id),
2349 MIM.im_domain, null);
2355 var node = Xex.Load (null, this.file);
2358 this.load_status = MIM.LoadStatus.Error;
2362 this.initial_state = null;
2363 this.state_list = {};
2364 for (node = node.firstElement (); node; node = node.nextElement ())
2366 var name = node.nodeName;
2367 var parser = parsers[name];
2369 parser.call (this, node);
2371 this.load_status = MIM.LoadStatus.Loaded;
2376 MIM.IM.prototype = proto;
2378 MIM.IC = function (im, target)
2380 if (im.load_status == MIM.LoadStatus.NotLoaded)
2382 if (im.load_status != MIM.LoadStatus.Loaded)
2383 alert ('im:' + im.name + ' error:' + im.load_status);
2385 this.target = target;
2386 this.domain = new Xex.Domain ('context', im.domain, this);
2388 this.range = new Array ();
2389 this.range[0] = this.range[1] = 0;
2391 this.initial_state = this.im.initial_state;
2392 this.keys = new MIM.KeySeq ();
2393 this.marker_positions = new Array ();
2394 this.candidate_table = new MIM.CandidateTable ();
2398 MIM.CandidateTable = function ()
2400 this.table = new Array ();
2403 MIM.CandidateTable.prototype.get = function (pos)
2405 for (var i = 0; i < this.table.length; i++)
2407 var elt = this.table[i];
2408 if (elt.from < pos && pos <= elt.to)
2413 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2415 for (var i = 0; i < this.table.length; i++)
2417 var elt = this.table[i];
2418 if (elt.from < to && elt.to > from)
2422 elt.val = candidates;
2426 this.table.push ({ from: from, to: to, val: candidates });
2429 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2431 var diff = inserted - (to - from);
2434 for (var i = 0; i < this.table.length; i++)
2436 var elt = this.table[i];
2445 MIM.CandidateTable.prototype.clear = function ()
2447 this.table.length = 0;
2450 function detach_candidates (ic)
2452 ic.candidate_table.clear ();
2453 ic.candidates = null;
2454 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2455 | MIM.ChangedStatus.CandidateList
2456 | MIM.ChangedStatus.CandidateIndex
2457 | MIM.ChangedStatus.CandidateShow);
2460 function set_cursor (prefix, pos)
2462 this.cursor_pos = pos;
2463 this.candidates = this.candidate_table.get (pos);
2466 function save_state ()
2468 this.state_var_values = this.domain.SaveValues ();
2469 this.state_preedit = this.preedit;
2470 this.state_key_head = this.key_head;
2471 this.state_pos = this.cursor_pos;
2474 function restore_state ()
2476 this.domain.RestoreValues (this.state_var_values);
2477 this.preedit = this.state_preedit;
2478 set_cursor.call (this, "restore", this.state_pos);
2481 function handle_key ()
2483 var out = this.keymap.Lookup (this.keys, this.key_head);
2486 Xex.Log ('handling ' + this.keys.val[this.key_head]
2487 + ' in ' + this.state.name + ':' + this.keymap.name);
2488 this.key_head = out.index;
2489 if (sub != this.keymap)
2492 restore_state.call (this);
2494 Xex.Log ('submap found');
2495 if (this.keymap.map_actions)
2497 Xex.Log ('taking map actions:');
2498 if (! this.take_actions (this.keymap.map_actions))
2501 else if (this.keymap.submaps)
2503 Xex.Log ('no map actions');
2504 for (var i = this.state_key_head; i < this.key_head; i++)
2506 Xex.Log ('inserting key:' + this.keys.val[i].key);
2507 this.ins (this.keys.val[i].key, null);
2510 if (! this.keymap.submaps)
2512 Xex.Log ('terminal:');
2513 if (this.keymap.branch_actions != null)
2515 Xex.Log ('branch actions:');
2516 if (! this.take_actions (this.keymap.branch_actions))
2519 if (this.keymap != this.state.keymap)
2520 this.shift (this.state);
2525 Xex.Log ('no submap');
2526 var current_state = this.state;
2527 var map = this.keymap;
2529 if (map.branch_actions)
2531 Xex.Log ('branch actions');
2532 if (! this.take_actions (map.branch_actions))
2536 if (map == this.keymap)
2538 Xex.Log ('no state change');
2539 if (map == this.initial_state.keymap
2540 && this.key_head < this.keys.val.length)
2542 Xex.Log ('unhandled');
2545 if (this.keymap != current_state.keymap)
2546 this.shift (current_state);
2547 else if (this.keymap.actions == null)
2548 this.shift (this.initial_state);
2557 Xex.Log ('reseting ' + this.im.lang + '-' + this.im.name);
2558 this.cursor_pos = 0;
2559 this.candidate_show = false;
2560 this.prev_state = null;
2561 this.title = this.initial_state.title;
2562 this.state_preedit = '';
2563 this.state_key_head = 0;
2564 this.state_var_values = {};
2567 this.key_unhandled = false;
2568 this.unhandled_key = null;
2569 this.changed = MIM.ChangedStatus.None;
2570 this.error_message = '';
2571 this.title = this.initial_state.title;
2574 this.preedit_saved = '';
2575 this.candidate_table.clear ();
2576 this.candidates = null;
2577 this.candidate_show = false;
2578 for (var elt in this.marker_positions)
2579 this.marker_positions[elt] = 0;
2580 this.shift (this.initial_state);
2583 catch_args: new Array (Xex.CatchTag._mimtag, null),
2585 take_actions: function (actions)
2587 var func_progn = this.domain.GetFunc ('progn');
2588 var func_catch = this.domain.GetFunc ('catch');
2589 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2590 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2591 term = term.Eval (this.domain);
2592 return (! term.IsSymbol || term.val != '@mimtag');
2595 GetSurroundingChar: function (pos)
2599 pos += this.range[0];
2605 pos += this.range[1];
2606 if (pos >= this.target.value.length)
2609 return this.target.value.charCodeAt (pos);
2612 DelSurroundText: function (pos)
2617 pos += this.range[0];
2623 text = this.target.value.substring (0, pos);
2624 if (this.range[0] < this.target.value.length)
2625 text += this.target.value.substring (this.range[0]);
2626 this.target.value = text;
2627 this.range[1] -= this.range[0] - pos;
2628 this.range[0] = pos;
2632 pos += this.range[1];
2633 text = this.target.value.substring (0, this.range[1]);
2634 if (pos >= this.target.value.length)
2635 pos = this.target.value.length;
2637 text += this.target.value.substring (pos);
2638 this.target.value = text;
2642 adjust_markers: function (from, to, inserted)
2644 var diff = inserted - (to - from);
2646 for (var name in this.marker_positions)
2648 var pos = this.marker_positions[name];
2652 this.marker_positions[name] += diff;
2653 else if (pos > from)
2654 this.marker_positions[name] = from;
2657 if (this.cursor_pos >= to)
2658 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2659 else if (this.cursor_pos > from)
2660 set_cursor.call (this, 'adjust', from)
2663 preedit_replace: function (from, to, text, candidates)
2665 this.preedit = (this.preedit.substring (0, from)
2666 + text + this.preedit.substring (to));
2667 this.adjust_markers (from, to, text.length);
2668 this.candidate_table.adjust (from, to, text.length);
2670 this.candidate_table.put (from, from + text.length, candidates)
2673 ins: function (text, candidates)
2675 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2676 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2679 rep: function (old_text, new_text, candidates)
2681 this.preedit_replace (this.cursor_pos - old_text.length,
2682 this.cursor_pos, new_text, candidates);
2683 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2688 var deleted = pos - this.cursor_pos;
2689 if (pos < this.cursor_pos)
2693 this.DelSurroundText (pos);
2694 deleted = - this.cursor_pos;
2697 if (pos < this.cursor_pos)
2698 this.preedit_replace (pos, this.cursor_pos, '', null);
2702 if (pos > this.preedit.length)
2704 this.DelSurroundText (pos - this.preedit.length);
2705 deleted = this.preedit.length - this.cursor_pos;
2706 pos = this.preedit.length;
2708 if (pos > this.cursor_pos)
2709 this.preedit_replace (this.cursor_pos, pos, '', null);
2712 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2718 this.candidate_show = true;
2719 this.changed |= MIM.ChangedStatus.CandidateShow;
2724 this.candidate_show = false;
2725 this.changed |= MIM.ChangedStatus.CandidateShow;
2728 move: function (pos)
2732 else if (pos > this.preedit.length)
2733 pos = this.preedit.length;
2734 if (pos != this.cursor_pos)
2736 set_cursor.call (this, 'move', pos);
2737 this.changed |= MIM.ChangedStatus.Preedit;
2741 pushback: function (n)
2743 if (n instanceof MIM.KeySeq)
2745 if (this.key_head > 0)
2747 if (this.key_head < this.keys.val.length)
2748 this.keys.val.splice (this.key_head,
2749 this.keys.val.length - this.key_head);
2750 for (var i = 0; i < n.val.length; i++)
2751 this.keys.val.push (n.val[i]);
2757 if (this.key_head < 0)
2764 this.key_head = - n;
2765 if (this.key_head > this.keys.val.length)
2766 this.key_head = this.keys.val.length;
2772 if (this.key_head < this.keys.val.length)
2773 this.keys.val.splice (this.key_head, 1);
2778 if (this.preedit.length > 0)
2780 this.candidate_table.clear ();
2781 this.produced += this.preedit;
2782 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2786 shift: function (state)
2790 if (this.prev_state == null)
2792 state = this.prev_state;
2795 if (state == this.initial_state)
2800 this.keys.val.splice (0, this.key_head);
2802 this.prev_state = null;
2807 if (state != this.state)
2808 this.prev_state = this.state;
2810 if (state != this.state && state.enter_actions)
2811 this.take_actions (state.enter_actions);
2812 if (! this.state || this.state.title != state.title)
2813 this.changed |= MIM.ChangedStatus.StateTitle;
2815 this.keymap = state.keymap;
2816 this.state_key_head = this.key_head;
2817 save_state.call (this);
2820 Filter: function (key)
2824 this.key_unhandled = true;
2825 this.unhandled_key = key;
2828 if (key.key == '_reload')
2830 this.changed = MIM.ChangedStatus.None;
2832 this.key_unhandled = false;
2833 this.keys.val.push (key);
2835 while (this.key_head < this.keys.val.length)
2837 if (! handle_key.call (this))
2839 if (this.key_head < this.keys.val.length)
2841 this.unhandled_key = this.keys.val[this.key_head];
2842 this.keys.val.splice (this.key_head, this.key_head + 1);
2844 this.key_unhandled = true;
2850 this.key_unhandled = true;
2854 if (this.key_unhandled)
2856 this.keys.val.length = 0;
2857 this.key_head = this.state_key_head = this.commit_key_head = 0;
2859 return (! this.key_unhandled
2860 && this.produced.length == 0
2861 && this.preedit.length == 0);
2865 MIM.IC.prototype = proto;
2867 var node = Xex.Load (null, "imlist.xml");
2868 for (node = node.firstChild; node; node = node.nextSibling)
2869 if (node.nodeName == 'input-method')
2871 var lang = null, name = null, extra_id = null, file = null;
2873 for (var n = node.firstChild; n; n = n.nextSibling)
2875 if (n.nodeName == 'language')
2876 lang = n.firstChild.nodeValue;
2877 else if (n.nodeName == 'name')
2878 name = n.firstChild.nodeValue;
2879 else if (n.nodeName == 'extra-id')
2880 extra_id = n.firstChild.nodeValue;
2881 else if (n.nodeName == 'filename')
2882 file = n.firstChild.nodeValue;
2884 if (name && name != 'nil')
2886 if (! MIM.imlist[lang])
2887 MIM.imlist[lang] = {};
2888 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2890 else if (extra_id && extra_id != 'nil')
2892 if (! MIM.imextra[lang])
2893 MIM.imextra[lang] = {};
2894 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2897 if (MIM.imextra.t && MIM.imextra.t.global)
2898 MIM.im_global = MIM.imextra.t.global;
2901 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2902 MIM.im_global.load_status = MIM.LoadStatus.Error;
2908 var keys = new Array ();
2910 keys[0x08] = 'backspace';
2911 keys[0x0D] = 'return';
2912 keys[0x1B] = 'escape';
2913 keys[0x20] = 'space';
2914 keys[0x21] = 'pageup';
2915 keys[0x22] = 'pagedown';
2917 keys[0x24] = 'home';
2918 keys[0x25] = 'left';
2920 keys[0x27] = 'right';
2921 keys[0x28] = 'down';
2922 keys[0x2D] = 'insert';
2923 keys[0x2E] = 'delete';
2924 for (var i = 1; i <= 12; i++)
2925 keys[111 + i] = "f" + i;
2926 keys[0x90] = "numlock";
2927 keys[0xF0] = "capslock";
2930 keyids['U+0008'] = 'backspace';
2931 keyids['U+0009'] = 'tab';
2932 keyids['U+0018'] = 'cancel';
2933 keyids['U+001B'] = 'escape';
2934 keyids['U+0020'] = 'space';
2935 keyids['U+007F'] = 'delete';
2938 modifiers.Shift = 1;
2939 modifiers.Control = 1;
2941 modifiers.AltGraph = 1;
2944 MIM.decode_key_event = function (event)
2946 var key = event.keyIdentifier;
2948 if (key) // keydown event of Chrome
2953 if (event.ctrlKey) mod += 'C-';
2954 if (event.metaKey) mod += 'M-';
2955 if (event.altKey) mod += 'A-';
2956 var keysym = keyids[key];
2959 else if (key.match(/^U\+([0-9A-Z]+)$/))
2961 if (mod.length == 0)
2963 key = String.fromCharCode (parseInt (RegExp.$1, 16));
2966 key = key.toLowerCase ();
2967 if (event.shiftKey) mod += 'S-';
2968 return new MIM.Key (mod + key);
2972 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
2973 : event.charCode ? event.charCode
2977 if (event.type == 'keydown')
2982 if (event.shiftKey) key = "S-" + key ;
2985 key = String.fromCharCode (key);
2987 if (event.altKey) key = "A-" + key ;
2988 if (event.ctrlKey) key = "C-" + key ;
2989 return new MIM.Key (key);
2993 MIM.add_event_listener
2994 = (window.addEventListener
2995 ? function (target, type, listener) {
2996 target.addEventListener (type, listener, false);
2998 : window.attachEvent
2999 ? function (target, type, listener) {
3000 target.attachEvent ('on' + type,
3002 listener.call (target, window.event);
3005 : function (target, type, listener) {
3007 = function (e) { listener.call (target, e || window.event); };
3010 MIM.debug_print = function (event, ic)
3014 if (! MIM.debug_nodes)
3016 MIM.debug_nodes = new Array ();
3017 MIM.debug_nodes['status0'] = document.getElementById ('status0');
3018 MIM.debug_nodes['status1'] = document.getElementById ('status1');
3019 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3020 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3021 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3022 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3023 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3024 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3026 var target = event.target;
3027 var code = event.keyCode;
3028 var ch = event.type == 'keypress' ? event.charCode : 0;
3029 var key = MIM.decode_key_event (event);
3032 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3033 index = (event.type == 'keydown' ? '0' : '1');
3035 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3037 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3038 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3039 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3042 MIM.debug_nodes.keypress.innerHTML = '';
3043 MIM.debug_nodes.status1.innerHTML = '';
3044 MIM.debug_nodes.keymap1.innerHTML = '';
3045 MIM.debug_nodes.preedit1.innerHTML = ''
3049 MIM.get_range = function (target, ic)
3052 if (target.selectionStart != null) // for Mozilla
3054 from = target.selectionStart;
3055 to = target.selectionEnd;
3059 var r = document.selection.createRange ();
3060 var rr = r.duplicate ();
3062 rr.moveToElementText (target);
3063 rr.setEndPoint ('EndToEnd', range);
3064 from = rr.text.length - r.text.length;
3065 to = rr.text.length;
3067 if (ic.range[0] == from && ic.range[1] == to
3068 && (to == from || target.value.substring (from, to) == ic.preedit))
3075 MIM.set_caret = function (target, ic)
3077 if (target.setSelectionRange) // Mozilla
3079 var scrollTop = target.scrollTop;
3080 target.setSelectionRange (ic.range[0], ic.range[1]);
3081 target.scrollTop = scrollTop;
3085 var range = target.createTextRange ();
3086 range.moveStart ('character', ic.range[0]);
3087 range.moveEnd ('character', ic.range[1]);
3092 MIM.ignore_focus = false;
3094 MIM.update = function (target, ic)
3096 var text = target.value;
3097 target.value = (text.substring (0, ic.range[0])
3100 + text.substring (ic.range[1]));
3101 ic.range[0] += ic.produced.length;
3102 ic.range[1] = ic.range[0] + ic.preedit.length;
3103 MIM.set_caret (target, ic);
3106 MIM.focus_in = function (event)
3108 var target = event.target;
3109 var ic = target.mim_ic;
3112 if (target.mim_ignore_focus_in)
3114 // Ignore this event which happened in setSelectionRange ().
3115 target.mim_ignore_focus_in = false;
3116 event.preventDefault ();
3121 ic.Filter (MIM.Key.FocusIn);
3122 MIM.update (target, ic);
3123 // Ignore further focus-in caused by setSelectionRange ().
3124 target.mim_ignore_focus_in = true;
3126 catch (e) { Xex.Log ('Error:' + e); throw (e); }
3131 MIM.focus_out = function (event)
3133 var target = event.target;
3134 var ic = target.mim_ic;
3137 if (target.mim_ignore_focus_out)
3139 // Ignore this event which caused by setSelectionRange ().
3140 target.mim_ignore_focus_out = false;
3141 event.preventDefault ();
3146 if (! MIM.get_range (target, ic))
3148 ic.Filter (MIM.Key.FocusOut);
3149 MIM.update (target, ic);
3150 // Ignore further focus-out caused by setSelectionRange ().
3151 target.mim_ignore_focus_out = true;
3153 catch (e) { Xex.Log ('Error:' + e); throw (e); }
3158 MIM.keydown = function (event)
3160 var target = event.target;
3161 if (target.id == 'log')
3163 if (! (target.type == "text" || target.type == "textarea"))
3165 document.akey = event;
3167 var ic = target.mim_ic;
3168 if (! ic || ic.im != MIM.current)
3170 target.mim_ic = null;
3171 Xex.Log ('creating IC');
3172 ic = new MIM.IC (MIM.current, target);
3173 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3176 MIM.add_event_listener (target, 'focus', MIM.focus_in);
3177 MIM.add_event_listener (target, 'blur', MIM.focus_out);
3178 MIM.get_range (target, ic)
3182 if (! MIM.get_range (target, ic))
3185 MIM.debug_print (event, ic);
3186 ic.key = MIM.decode_key_event (event);
3189 Xex.Log ("filtering " + ic.key);
3191 var result = ic.Filter (ic.key);
3193 Xex.Log ('Error' + e);
3196 MIM.update (target, ic);
3197 if (! ic.key_unhandled)
3198 event.preventDefault ();
3202 MIM.keypress = function (event)
3204 var target = event.target;
3205 if (target.id == 'log')
3207 if (! (target.type == "text" || target.type == "textarea"))
3210 var ic = target.mim_ic;
3214 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3217 ic.key = MIM.decode_key_event (event);
3224 Xex.Log ("filtering " + ic.key);
3226 var result = ic.Filter (ic.key);
3228 Xex.Log ('Error;' + e);
3231 MIM.update (target, ic);
3232 if (! ic.key_unhandled)
3233 event.preventDefault ();
3235 Xex.Log ("error:" + e);
3236 event.preventDefault ();
3238 MIM.debug_print (event, ic);
3244 var lang_category = {
3246 cs: { name: 'Czech' },
3247 da: { name: 'Danish' },
3248 el: { name: 'Greek' },
3249 en: { name: 'English' },
3250 eo: { name: 'Esperanto' },
3251 fr: { name: 'French' },
3252 grc: { name: 'ClassicGreek' },
3253 hr: { name: 'Croatian' },
3254 hy: { name: 'Armenian' },
3255 ka: { name: 'Georgian' },
3256 kk: { name: 'Kazakh' },
3257 ru: { name: 'Russian' },
3258 sk: { name: 'Slovak' },
3259 sr: { name: 'Serbian' },
3260 sv: { name: 'Swedish' },
3261 vi: { name: 'Vietnamese' },
3262 yi: { name: 'Yiddish' } },
3264 ar: { name: 'Arabic' },
3265 dv: { name: 'Divehi' },
3266 fa: { name: 'Persian' },
3267 he: { name: 'Hebrew' },
3268 kk: { name: 'Kazakh' },
3269 ps: { name: 'Pushto' },
3270 ug: { name: 'Uighur' },
3271 yi: { name: 'Yiddish' } },
3273 as: { name: 'Assamese' },
3274 bn: { name: 'Bengali' },
3275 bo: { name: 'Tibetan' },
3276 gu: { name: 'Gujarati' },
3277 hi: { name: 'Hindi' },
3278 kn: { name: 'Kannada' },
3279 ks: { name: 'Kashmiri' },
3280 ml: { name: 'Malayalam' },
3281 mr: { name: 'Marathi' },
3282 ne: { name: 'Nepali' },
3283 or: { name: 'Oriya' },
3284 pa: { name: 'Panjabi' },
3285 sa: { name: 'Sanskirit' },
3286 sd: { name: 'Sindhi' },
3287 si: { name: 'Sinhalese' },
3288 ta: { name: 'Tamil' },
3289 te: { name: 'Telugu' },
3290 ur: { name: 'Urdu' } },
3292 cmc: { name: 'Cham' },
3293 km: { name: 'Khmer'},
3294 lo: { name: 'Lao' },
3295 my: { name: 'Burmese' },
3296 tai: { name: 'Tai Viet' },
3297 th: { name: 'Thai' },
3298 vi: { name: 'Vietanamese' } },
3300 ii: { name: 'Yii' },
3301 ja: { name: 'Japanese' },
3302 ko: { name: 'Korean' },
3303 zh: { name: 'Chinese' } },
3305 am: { name: 'Amharic' },
3306 ath: { name: 'Carrier' },
3307 bla: { name: 'Blackfoot' },
3308 cr: { name: 'Cree' },
3309 eo: { name: 'Esperanto' },
3310 iu: { name: 'Inuktitut' },
3311 nsk: { name: 'Naskapi' },
3312 oj: { name: 'Ojibwe' },
3313 t: { name: 'Generic' } }
3316 function categorize_im ()
3318 var cat, lang, list, name;
3319 for (lang in MIM.imlist)
3322 for (cat in lang_category)
3323 if (lang_category[cat][lang])
3325 list = lang_category[cat][lang].list;
3327 list = lang_category[cat][lang].list = {};
3331 for (name in MIM.imlist[lang])
3332 list[name] = MIM.imlist[lang][name];
3334 for (name in MIM.imlist[lang])
3335 Xex.Log ('no category ' + lang + '-' + name);
3344 clearTimeout (destroy_timer);
3345 destroy_timer = null;
3346 var target = document.getElementById ('mim-menu');
3349 for (; last_target && last_target.menu_level;
3350 last_target = last_target.parentLi)
3351 last_target.style.backgroundColor = 'white';
3352 var nodes = target.getElementsByTagName ('ul');
3353 for (var i = 0; i < nodes.length; i++)
3354 nodes[i].style.visibility = 'hidden';
3355 document.getElementsByTagName ('body')[0].removeChild (target);
3359 function destroy_menu () {
3360 if (! destroy_timer)
3361 destroy_timer = setTimeout (destroy, 1000);
3364 function show_submenu (event)
3368 clearTimeout (destroy_timer);
3369 destroy_timer = null;
3371 var target = event.target;
3372 if (! target.menu_level)
3374 if (last_target && target.parentLi != last_target)
3376 last_target.style.backgroundColor = 'white';
3377 if (target.menu_level < last_target.menu_level)
3379 last_target = last_target.parentLi;
3380 last_target.style.backgroundColor = 'white';
3382 var uls = last_target.getElementsByTagName ('ul');
3383 for (var i = 0; i < uls.length; i++)
3384 uls[i].style.visibility = 'hidden';
3386 last_target = target;
3387 target.style.backgroundColor = 'yellow';
3388 if (target.menu_level < 3)
3390 target.lastChild.style.visibility = 'visible';
3391 target.lastChild.style.left = target.clientWidth + 'px';
3393 event.preventDefault ();
3396 function select_im (event)
3398 var target = event.target;
3401 MIM.current = target.im;
3404 event.preventDefault ();
3407 function create_ul (visibility)
3409 var ul = document.createElement ('ul');
3410 ul.style.position = 'absolute';
3411 ul.style.margin = '0px';
3412 ul.style.padding = '0px';
3413 ul.style.border = '1px solid gray';
3414 ul.style.borderBottom = 'none';
3415 ul.style.top = '-1px';
3416 ul.style.backgroundColor = 'white';
3417 ul.style.visibility = visibility;
3421 function create_li (level, text)
3423 var li = document.createElement ('li');
3424 li.style.position = 'relative';
3425 li.style.margin = '0px';
3426 li.style.padding = '1px';
3427 li.style.borderBottom = '1px solid gray';
3428 li.style.top = '0px';
3429 li.style.listStyle = 'none';
3430 li.menu_level = level;
3431 li.appendChild (document.createTextNode (text));
3437 function create_menu (event)
3439 var target = event.target;
3441 if (! ((target.type == "text" || target.type == "textarea")
3442 && event.which == 1 && event.ctrlKey))
3447 menu = create_ul ('visible');
3448 menu.style.fontFamily = 'sans-serif';
3449 menu.style.fontWeight = 'bold';
3450 menu.id = 'mim-menu';
3451 menu.onclick = select_im;
3452 menu.onmouseover = show_submenu;
3453 menu.onmouseout = destroy_menu;
3454 for (var catname in lang_category)
3456 var cat = lang_category[catname];
3457 var li = create_li (1, catname);
3458 var sub = create_ul ('hidden');
3459 for (var langname in cat)
3461 var lang = cat[langname];
3464 var sub_li = create_li (2, lang.name);
3465 sub_li.parentLi = li;
3466 var subsub = create_ul ('hidden');
3467 for (var name in lang.list)
3469 var im = lang.list[name];
3470 var subsub_li = create_li (3, im.name);
3471 subsub_li.parentLi = sub_li;
3473 subsub.appendChild (subsub_li);
3475 sub_li.appendChild (subsub);
3476 sub.appendChild (sub_li);
3478 li.appendChild (sub);
3479 menu.appendChild (li);
3481 document.mimmenu = menu;
3482 lang_category = null;
3484 menu.style.left = (event.clientX - 10) + "px";
3485 menu.style.top = (event.clientY - 10) + "px";
3486 document.getElementsByTagName ('body')[0].appendChild (menu);
3489 MIM.init = function ()
3491 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3492 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3493 MIM.add_event_listener (window, 'mousedown', create_menu);
3494 if (window.location == 'http://localhost/mim/index.html')
3495 MIM.server = 'http://localhost/mim';
3496 MIM.current = MIM.imlist['vi']['telex'];
3500 MIM.test = function ()
3502 var im = MIM.imlist['t']['latn-post'];
3503 var ic = new MIM.IC (im, null);
3505 ic.Filter (new MIM.Key ('a'));
3506 ic.Filter (new MIM.Key ("'"));
3509 document.getElementById ('text').value = ic.produced + ic.preedit;
3512 document.getElementById ('text').value
3513 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3515 if (e instanceof Xex.ErrTerm)
3523 MIM.init_debug = function ()
3526 Xex.LogNode = document.getElementById ('log');