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 += "\n" + str + arg;
18 Xex.LogNode.scrollTop = Xex.LogNode.scrollHeight;
24 UnknownError: "unknown-error",
25 WrongArgument: "wrong-argument",
27 InvalidInteger: "invalid-integer",
28 TermTypeInvalid: "term-type-invalid",
29 FunctionConflict: "function-conflict",
30 VariableTypeConflict: "variable-type-conflict",
31 VariableRangeConflict: "variable-range-conflict",
32 VariableWrongRange: "variable-wrong-range",
33 VariableWrongValue: "variable-wrong-value",
35 UnknownFunction: "unknown-function",
36 MacroExpansionError: "macro-expansion-error",
37 NoVariableName: "no-variable-name",
38 NoFunctionName: "no-funcion-name",
41 ArithmeticError: "arithmetic-error",
42 WrongType: "wrong-type",
43 IndexOutOfRange: "index-out-of-range",
44 ValueOutOfRange: "value-out-of-range",
45 NoLoopToBreak: "no-loop-to-break",
46 UncaughtThrow: "uncaught-throw"
49 Xex.Variable = function (domain, name, desc, val, range)
58 Xex.Variable.prototype.clone = function ()
60 return new Xex.Variable (this.domain, this.name, this.desc,
61 this.val, this.range);
64 Xex.Variable.prototype.Equals = function (obj)
66 return ((obj instanceof Xex.Variable)
67 && obj.name == this.name);
70 Xex.Variable.prototype.SetValue = function (term)
76 Xex.Function = function (name, with_var, min_args, max_args)
79 this.with_var = with_var;
80 this.min_args = min_args;
81 this.max_args = max_args;
84 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
87 this.with_var = with_var;
88 this.min_args = min_args;
89 this.max_args = max_args;
90 this.builtin = builtin;
93 Xex.Subrountine.prototype.Call = function (domain, vari, args)
95 var newargs = new Array ();
96 for (var i = 0; i < args.length; i++)
98 newargs[i] = args[i].Eval (domain);
102 return this.builtin (domain, vari, newargs)
105 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
108 this.with_var = with_var;
109 this.min_args = min_args;
110 this.max_args = max_args;
111 this.builtin = builtin;
114 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
116 return this.builtin (domain, vari, args)
119 Xex.Lambda = function (name, min_args, max_args, args, body)
122 this.min_args = min_args;
123 this.max_args = max_args;
128 Xex.Lambda.prototype.Call = function (domain, vari, args)
130 var current = domain.bindings;
131 var result = Xex.Zero;
132 var limit = max_args >= 0 ? args.length : args.length - 1;
136 for (i = 0; i < limit; i++)
138 result = args[i].Eval (domain);
139 if (domain.Thrown ())
141 domain.Bind (this.args[i], result);
145 var list = new Array ();
146 for (i = 0; i < args[limit].length; i++)
148 result = args[limit].Eval (domain);
149 if (domain.Thrown ())
153 domain.Bind (this.args[limit], list);
156 domain.Catch (Xex.CatchTag.Return);
157 for (var term in this.body)
159 result = term.Eval (domain);
160 if (domain.Thrown ())
167 domain.UnboundTo (current);
172 Xex.Macro = function (name, min_args, max_args, args, body)
175 this.min_args = min_args;
176 this.max_args = max_args;
181 Xex.Macro.prototype.Call = function (domain, vari, args)
183 var current = domain.bindings;
184 var result = Xex.Zero;
188 for (i = 0; i < args.length; i++)
189 domain.Bind (this.args[i], args[i]);
191 domain.Catch (Xex.CatchTag.Return);
192 for (var i in this.body)
194 result = this.body[i].Eval (domain);
195 if (domain.Thrown ())
202 domain.UnboundTo (current);
207 Xex.Bindings = function (vari)
210 this.old_value = vari.val;
213 Xex.Bindings.prototype.UnboundTo = function (boundary)
215 for (var b = this; b != boundary; b = b.next)
216 b.vari.val = b.old_value;
220 Xex.Bind = function (bindings, vari, val)
222 var b = new Xex.Bindings (vari);
233 Xex.Domain = function (name, parent, context)
236 this.context = context;
239 if (name != 'basic' && ! parent)
240 parent = Xex.BasicDomain
241 this.parent = parent;
248 for (elt in parent.termtypes)
249 this.termtypes[elt] = parent.termtypes[elt];
250 for (elt in parent.functions)
251 this.functions[elt] = parent.functions[elt];
252 for (elt in parent.variables)
254 var vari = parent.variables[elt];
255 this.variables[elt] = new Xex.Variable (this, vari.name, vari.desc,
256 vari.val, vari.range);
260 this.call_stack = new Array ();
261 this.bindings = null;
262 this.catch_stack = new Array ();
263 this.catch_count = 0;
267 Xex.Domain.prototype = {
268 CallStackCount: function () { return this.call_stack.length; },
269 CallStackPush: function (term) { this.call_stack.push (term); },
270 CallStackPop: function () { this.call_stack.pop (); },
271 Bind: function (vari, val)
273 this.bindings = Xex.Bind (this.bindings, vari, val);
275 UnboundTo: function (boundary)
278 this.bindings = this.bindings.UnboundTo (boundary);
280 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
283 this.catch_stack.pop ();
284 if (this.catch_count > this.catch_stack.length)
289 if (this.catch_count < this.catch_stack.length)
291 this.caught = (this.catch_count == this.catch_stack.length - 1);
297 ThrowReturn: function ()
299 for (var i = this.catch_stack.length - 1; i >= 0; i--)
302 if (this.catch_stack[i] == Xex.CatchTag.Return)
306 ThrowBreak: function ()
308 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
309 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
310 "No surrounding loop to break");
313 ThrowSymbol: function (tag)
315 var i = this.catch_count;
316 for (var j = this.catch_stack.length - 1; j >= 0; j--)
319 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
321 this.catch_count = i;
325 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
326 "No corresponding catch: " + tag);
328 DefType: function (obj)
331 if (this.termtypes[type])
332 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
333 "Already defined: " + type);
334 if (this.functions[type])
335 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
336 "Already defined as a funciton or a macro: "
338 this.termtypes[type] = obj.Parser;
340 DefSubr: function (builtin, name, with_var, min_args, max_args)
342 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
345 DefSpecial: function (builtin, name, with_var, min_args, max_args)
347 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
350 Defun: function (name, min_args, max_args, args, body)
352 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
355 DefunByFunc: function (func) { this.functions[func.name] = func; },
356 Defmacro: function (name, min_args, max_args, args, body)
358 this.functions[name] = new Xex.Macro (name, min_args, max_args,
361 DefAlias: function (alias, fname)
363 var func = this.functions[fname];
366 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
367 if (this.termtypes[alias])
368 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
369 "Already defined as a term type: " + alias);
370 if (this.functions[alias])
371 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
372 "Already defined as a function: " + alias);
373 this.functions[alias] = func;
375 Defvar: function (name, desc, val, range)
377 var vari = new Xex.Variable (this, name, desc, val, range);
378 this.variables[name] = vari;
381 GetFunc: function (name)
383 var func = this.functions[name];
385 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
386 "Unknown function: " + name);
389 CopyFunc: function (domain, name)
391 var func = this.functions[name];
392 domain.DefunByFunc (func);
395 CopyFuncAll: function (domain)
397 for (var elt in this.functions)
398 domain.DefunByFunc (this.functions[elt]);
400 GetVarCreate: function (name)
402 var vari = this.variables[name];
404 vari = this.variables[name] = new Xex.Variable (this, name, null,
408 GetVar: function (name) { return this.variables[name]; },
409 SaveValues: function ()
412 for (var elt in this.variables)
413 values[elt] = this.variables[elt].val.Clone ();
416 RestoreValues: function (values)
421 var vari = this.variables[name];
422 vari.val = values[name];
427 Xex.Term = function (type) { this.type = type; }
428 Xex.Term.prototype = {
429 IsTrue: function () { return true; },
430 Eval: function (domain) { return this.Clone (); },
431 Clone: function (domain) { return this; },
432 Equals: function (obj)
434 return (this.type == obj.type
436 && obj.val == this.val);
438 Matches: function (obj) { return this.Equals (obj); },
439 toString: function ()
441 if (this.val != undefined)
442 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
443 return '<' + this.type + '/>';
445 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
446 "Not an integer"); },
447 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
451 Node.prototype.firstElement = function ()
453 for (var n = this.firstChild; n; n = n.nextSibling)
459 Node.prototype.nextElement = function ()
461 for (var n = this.nextSibling; n; n = n.nextSibling)
468 function parse_defvar (domain, node)
470 var name = node.attributes['vname'].nodeValue;
472 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
473 var vari = domain.variables[name];
474 var desc, val, range;
477 desc = vari.description;
481 node = node.firstElement ();
482 if (node && node.nodeName == 'description')
484 desc = node.firstChild.nodeValue;
485 node = node.nextElement ();
489 val = Xex.Term.Parse (domain, node);
490 node = node.nextElement ();
491 if (node && node.nodeName == 'possible-values')
492 for (node = node.firstElement (); node; node = node.nextElement ())
495 if (node.nodeName == 'range')
498 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
499 'Range not allowed for ' + name);
501 for (var n = node.firstElement (); n; n = n.nextElement ())
503 var v = Xex.Term.Parse (domain, n);
505 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
506 'Invalid range value: ' + val);
512 pval = Xex.Term.Parse (domain, node);
513 if (val.type != pval.type)
514 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
515 'Invalid possible value: ' + pval);
518 range = new Array ();
524 domain.Defvar (name, desc, val, range);
528 function parse_defun_head (domain, node)
530 var name = node.attributes['fname'].nodeValue;
532 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
533 var args = new Array ();
534 var nfixed = 0, noptional = 0, nrest = 0;
536 node = node.firstElement ();
537 if (node && node.nodeName == 'args')
540 for (n = n.firstElement (); n; n = n.nextElement ())
542 if (n.nodeName == 'fixed')
544 else if (n.nodeName == 'optional')
546 else if (n.nodeName == 'rest')
549 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
552 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
553 for (n = node.firstElement (); n; n = n.nextElement ())
554 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
556 args.min_args = nfixed;
557 args.max_args = nrest == 0 ? nfixed + noptional : -1;
559 if (node.nodeName == 'defun')
560 domain.Defun (name, args, null);
562 domain.Defmacro (name, args, null);
566 function parse_defun_body (domain, node)
568 var name = node.attributes['fname'].nodeValue;
569 var func = domain.GetFunc (name);
571 for (node = node.firstElement (); node; node = node.nextElement ())
572 if (node.nodeName != 'description' && node.nodeName != 'args')
574 body = Xex.Term.Parse (domain, node, null);
578 Xex.Term.Parse = function (domain, node, stop)
580 if (arguments.length == 2)
582 var name = node.nodeName;
583 var parser = domain.termtypes[name];
586 return parser (domain, node);
587 if (name == 'defun' || name == 'defmacro')
589 name = parse_defun_head (domain, node);
590 parse_defun_body (domain, node);
591 return new Xex.StrTerm (name);
593 if (name == 'defvar')
595 name = parse_defvar (domain, node);
596 return new Xex.StrTerm (name);
598 return new Xex.Funcall.prototype.Parser (domain, node);
600 for (var n = node; n && n != stop; n = n.nextElement ())
601 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
602 parse_defun_head (domain, n);
604 for (var n = node; n && n != stop; n = n.nextElement ())
606 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
607 parse_defun_body (domain, n);
608 else if (n.nodeName == 'defvar')
609 parse_defvar (domain, n);
613 terms = new Array ();
614 terms.push (Xex.Term.Parse (domain, n));
621 Xex.Varref = function (vname)
627 var proto = new Xex.Term ('varref');
629 proto.Clone = function () { return new Xex.Varref (this.val); }
630 proto.Eval = function (domain)
632 var vari = domain.GetVarCreate (this.val);
633 Xex.Log (this.ToString () + '=>' + vari.val, domain.depth);
637 proto.Parser = function (domain, node)
639 return new Xex.Varref (node.attributes['vname'].nodeValue);
642 proto.ToString = function ()
644 return '<varref vname="' + this.val + '"/>';
647 Xex.Varref.prototype = proto;
650 var null_args = new Array ();
652 Xex.Funcall = function (func, vname, args)
656 this.args = args || null_args;
660 var proto = new Xex.Term ('funcall');
662 proto.Parser = function (domain, node)
664 var fname = node.nodeName;
667 if (fname == 'funcall')
668 fname = node.attributes['fname'].nodeValue;
669 var func = domain.GetFunc (fname);
671 attr = node.attributes['vname'];
672 vname = attr != undefined ? attr.nodeValue : null;
673 var args = Xex.Term.Parse (domain, node.firstElement (), null);
674 return new Xex.Funcall (func, vname, args);
677 proto.New = function (domain, fname, vname, args)
679 var func = domain.GetFunc (fname);
680 var funcall = new Xex.Funcall (func, vname, args);
681 if (func instanceof Xex.Macro)
682 funcall = funcall.Eval (domain);
686 proto.Eval = function (domain)
688 if (! (this.func instanceof Xex.Subrountine))
689 Xex.Log (this, domain.depth);
692 vari = domain.GetVarCreate (this.vname);
696 result = this.func.Call (domain, vari, this.args);
698 Xex.Log (this + ' => ' + result, --domain.depth);
703 proto.Clone = function ()
705 return new Xex.Funcall (this.func, this.vari, this.args);
708 proto.Equals = function (obj)
710 return (obj.type == 'funcall'
711 && obj.func == this.func
712 && obj.vari.Equals (this.vari)
713 && obj.args.length == this.func.length);
716 proto.toString = function ()
719 var len = this.args.length;
720 var str = '<' + this.func.name;
722 str += ' vname="' + this.vari.name + '"';
725 if (this.func instanceof Xex.Subrountine)
726 for (var i = 0; i < len; i++)
727 arglist += this.args[i].toString ();
729 for (var i = 0; i < len; i++)
731 return str + '>' + arglist + '</' + this.func.name + '>';
734 Xex.Funcall.prototype = proto;
737 Xex.ErrTerm = function (ename, message, stack)
740 this.message = message;
745 var proto = new Xex.Term ('error');
747 proto.IsError = true;
749 proto.Parser = function (domain, node)
751 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
752 node.innerText, false);
755 proto.CallStack = function () { return stack; }
757 proto.SetCallStack = function (value) { statck = value; }
759 proto.Clone = function ()
761 return new Xex.ErrTerm (ename, message, false);
764 proto.Equals = function (obj)
767 && obj.ename == ename && obj.message == message
768 && (obj.stack ? (stack && stack.length == obj.stack.length)
772 proto.Matches = function (obj)
774 return (obj.IsError && obj.ename == ename);
777 proto.toString = function ()
779 return '<error ename="' + this.ename + '">' + this.message + '</error>';
782 Xex.ErrTerm.prototype = proto;
785 Xex.IntTerm = function (num) { this.val = num; };
787 var proto = new Xex.Term ('integer');
789 proto.Intval = function () { return this.val; };
790 proto.IsTrue = function () { return this.val != 0; }
791 proto.Clone = function () { return new Xex.IntTerm (this.val); }
792 proto.Parser = function (domain, node)
794 var str = node.firstChild.nodeValue;
796 if (str.charAt (0) == '?' && str.length == 2)
797 return new Xex.IntTerm (str.charCodeAt (1));
798 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
800 Xex.IntTerm.prototype = proto;
803 Xex.StrTerm = function (str) { this.val = str; };
805 var proto = new Xex.Term ('string');
807 proto.Strval = function () { return this.val; };
808 proto.IsTrue = function () { return this.val.length > 0; }
809 proto.Clone = function () { return new Xex.StrTerm (this.val); }
810 proto.Parser = function (domain, node)
812 return new Xex.StrTerm (node.firstChild ? node.firstChild.nodeValue : '');
814 Xex.StrTerm.prototype = proto;
817 Xex.SymTerm = function (str) { this.val = str; };
819 var proto = new Xex.Term ('symbol');
820 proto.IsSymbol = true;
821 proto.IsTrue = function () { return this.val != 'nil'; }
822 proto.Clone = function () { return new Xex.SymTerm (this.val); }
823 proto.Parser = function (domain, node)
825 return new Xex.SymTerm (node.firstChild.nodeValue);
827 Xex.SymTerm.prototype = proto;
830 Xex.LstTerm = function (list) { this.val = list; };
832 var proto = new Xex.Term ('list');
834 proto.IsTrue = function () { return this.val.length > 0; }
835 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
837 proto.Equals = function (obj)
839 if (obj.type != 'list' || obj.val.length != this.val.length)
841 var i, len = this.val.length;
842 for (i = 0; i < len; i++)
843 if (! this.val[i].Equals (obj.val[i]))
848 proto.Parser = function (domain, node)
850 var list = Xex.Term.Parse (domain, node.firstElement (), null);
851 return new Xex.LstTerm (list);
854 proto.toString = function ()
856 var len = this.val.length;
861 for (var i = 0; i < len; i++)
862 str += this.val[i].toString ();
863 return str + '</list>';
865 Xex.LstTerm.prototype = proto;
869 var basic = new Xex.Domain ('basic', null, null);
871 function Fset (domain, vari, args)
874 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
875 'No variable name to set');
876 vari.SetValue (args[0]);
880 function Fnot (domain, vari, args)
882 return (args[0].IsTrue () ? Xex.Zero : Xex.One);
885 function maybe_set_intvar (vari, n)
887 var term = new Xex.IntTerm (n);
889 vari.SetValue (term);
893 function Fadd (domain, vari, args)
895 var n = vari ? vari.val.Intval () : 0;
896 var len = args.length;
898 for (var i = 0; i < len; i++)
899 n += args[i].Intval ();
900 return maybe_set_intvar (vari, n);
903 function Fmul (domain, vari, args)
905 var n = vari ? vari.val.Intval () : 1;
906 for (var i = 0; i < args.length; i++)
908 return maybe_set_intvar (vari, n);
911 function Fsub (domain, vari, args)
917 n = args[0].Intval ();
922 n = vari.val.Intval ();
925 while (i < args.length)
926 n -= args[i++].Intval ();
927 return maybe_set_intvar (vari, n);
930 function Fdiv (domain, vari, args)
936 n = args[0].Intval ();
941 n = vari.val.Intval ();
944 while (i < args.length)
945 n /= args[i++].Intval ();
946 return maybe_set_intvar (vari, n);
949 function Fmod (domain, vari, args)
951 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
954 function Flogior (domain, vari, args)
956 var n = vari == null ? 0 : vari.val;
957 for (var i = 0; i < args.length; i++)
959 return maybe_set_intvar (vari, n);
962 function Flogand (domain, vari, args)
967 Xex.Log ("logand arg args[0]" + args[0]);
968 n = args[0].Intval ()
973 Xex.Log ("logand arg var " + vari);
974 n = vari.val.Intval ();
977 while (n > 0 && i < args.length)
979 Xex.Log ("logand arg " + args[i]);
982 return maybe_set_intvar (vari, n);
985 function Flsh (domain, vari, args)
987 return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
990 function Frsh (domain, vari, args)
992 return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
995 function Fand (domain, vari, args)
997 var len = args.length;
998 for (var i = 0; i < len; i++)
1000 var result = args[i].Eval (domain);
1001 if (domain.Thrown ())
1003 if (! result.IsTrue ())
1009 function For (domain, vari, args)
1011 var len = args.length;
1012 for (var i = 0; i < len; i++)
1014 var result = args[i].Eval (domain);
1015 if (domain.Thrown ())
1017 if (result.IsTrue ())
1023 function Feq (domain, vari, args)
1025 for (var i = 1; i < args.length; i++)
1026 if (! args[i - 1].Equals (args[i]))
1031 function Fnoteq (domain, vari, args)
1033 return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1036 function Flt (domain, vari, args)
1038 var n = args[0].Intval ();
1040 for (var i = 1; i < args.length; i++)
1042 var n1 = args[i].Intval ();
1050 function Fle (domain, vari, args)
1052 var n = args[0].Intval ();
1053 for (var i = 1; i < args.length; i++)
1055 var n1 = args[i].Intval ();
1063 function Fgt (domain, vari, args)
1065 var n = args[0].Intval ();
1066 for (var i = 1; i < args.length; i++)
1068 var n1 = args[i].Intval ();
1076 function Fge (domain, vari, args)
1078 var n = args[0].Intval ();
1079 for (var i = 1; i < args.Length; i++)
1081 var n1 = args[i].Intval ();
1089 function Fprogn (domain, vari, args)
1091 var result = Xex.One;
1092 var len = args.length;
1094 for (var i = 0; i < len; i++)
1096 result = args[i].Eval (domain);
1097 if (domain.Thrown ())
1103 function Fif (domain, vari, args)
1105 var result = args[0].Eval (domain);
1107 if (domain.Thrown ())
1109 if (result.IsTrue ())
1110 return args[1].Eval (domain);
1111 if (args.length == 2)
1113 return args[2].Eval (domain);
1116 function Fcond (domain, vari, args)
1118 for (var i = 0; i < args.length; i++)
1121 var result = list.val[0].Eval (domain);
1122 if (result.IsTrue ())
1124 for (var j = 1; j < list.val.length; j++)
1127 result = list.val[j].Eval (domain);
1129 if (domain.Thrown ())
1138 function eval_terms (domain, terms, idx)
1140 var result = Xex.Zero;
1141 domain.caught = false;
1142 for (var i = idx; i < terms.length; i++)
1144 result = terms[i].Eval (domain);
1145 if (domain.Thrown ())
1151 function Fcatch (domain, vari, args)
1156 if (args[0].IsError)
1159 result = eval_terms (domain, args, 1);
1161 if (e instanceof Xex.ErrTerm)
1163 if (! args[0].Matches (e))
1171 else if (args[0].IsSymbol)
1174 domain.Catch (args[0].val);
1175 result = eval_terms (domain, args, 1);
1179 vari.SetValue (result);
1187 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1188 "Not a symbol nor an error: " + args[0]);
1191 function Fthrow (domain, vari, args)
1193 if (args[0].IsSymbl)
1195 domain.ThrowSymbol (args[0]);
1196 return (args[args.length - 1]);
1198 if (args[0].IsError)
1202 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1203 "Not a symbol nor an error:" + args[0]);
1206 Xex.BasicDomain = basic;
1208 basic.DefSubr (Fset, "set", true, 1, 1);
1209 basic.DefAlias ("=", "set");
1210 basic.DefSubr (Fnot, "not", false, 1, 1);
1211 basic.DefAlias ("!", "not");
1212 basic.DefSubr (Fadd, "add", true, 1, -1);
1213 basic.DefSubr (Fmul, "mul", true, 1, -1);
1214 basic.DefAlias ("*", "mul");
1215 basic.DefSubr (Fsub, "sub", true, 1, -1);
1216 basic.DefAlias ("-", "sub");
1217 basic.DefSubr (Fdiv, "div", true, 1, -1);
1218 basic.DefAlias ("/", "div");
1219 basic.DefSubr (Fmod, "mod", true, 1, 2);
1220 basic.DefAlias ("%", "mod");
1221 basic.DefSubr (Flogior, "logior", true, 1, -1);
1222 basic.DefAlias ('|', "logior");
1223 basic.DefSubr (Flogand, "logand", true, 1, -1);
1224 basic.DefAlias ("&", "logand");
1225 basic.DefSubr (Flsh, "lsh", true, 1, 2);
1226 basic.DefAlias ("<<", "lsh");
1227 basic.DefSubr (Frsh, "rsh", true, 1, 2);
1228 basic.DefAlias (">>", "rsh");
1229 basic.DefSubr (Feq, "eq", false, 2, -1);
1230 basic.DefAlias ("==", "eq");
1231 basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1232 basic.DefAlias ("!=", "noteq");
1233 basic.DefSubr (Flt, "lt", false, 2, -1);
1234 basic.DefAlias ("<", "lt");
1235 basic.DefSubr (Fle, "le", false, 2, -1);
1236 basic.DefAlias ("<=", "le");
1237 basic.DefSubr (Fgt, "gt", false, 2, -1);
1238 basic.DefAlias (">", "gt");
1239 basic.DefSubr (Fge, "ge", false, 2, -1);
1240 basic.DefAlias (">=", "ge");
1241 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1243 //basic.DefSubr (Fappend, "append", true, 0, -1);
1244 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1245 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1246 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1247 //basic.DefSubr (Fins, "ins", true, 2, 2);
1248 //basic.DefSubr (Fdel, "del", true, 2, 2);
1249 //basic.DefSubr (Feval, "eval", false, 1, 1);
1250 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1251 //basic.DefSubr (Freturn, "return", false, 0, 1);
1252 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1254 basic.DefSpecial (Fand, "and", false, 1, -1);
1255 basic.DefAlias ("&&", "and");
1256 basic.DefSpecial (For, "or", false, 1, -1);
1257 basic.DefAlias ("||", "or");
1258 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1259 basic.DefAlias ("expr", "progn");
1260 basic.DefSpecial (Fif, "if", false, 2, 3);
1261 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1262 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1263 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1264 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1265 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1266 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1267 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1268 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1270 basic.DefType (Xex.Funcall.prototype);
1271 basic.DefType (Xex.Varref.prototype);
1272 basic.DefType (Xex.ErrTerm.prototype);
1273 basic.DefType (Xex.IntTerm.prototype);
1274 basic.DefType (Xex.StrTerm.prototype);
1275 basic.DefType (Xex.SymTerm.prototype);
1276 basic.DefType (Xex.LstTerm.prototype);
1280 Xex.Zero = new Xex.IntTerm (0);
1281 Xex.One = new Xex.IntTerm (1);
1282 Xex.nil = new Xex.SymTerm ('nil');
1284 Xex.Load = function (server, file)
1286 var obj = new XMLHttpRequest ();
1287 var url = server ? server + '/' + file : file;
1288 obj.open ('GET', url, false);
1289 obj.overrideMimeType ('text/xml');
1291 return obj.responseXML.firstChild;
1295 // URL of the input method server.
1296 server: "http://www.m17n.org/common/mim-js",
1297 // Boolean flag to tell if MIM is active or not.
1299 // Boolean flag to tell if MIM is running in debug mode or not.
1301 // List of main input methods.
1303 // List of extra input methods;
1305 // Global input method data
1307 // Currently selected input method.
1311 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1318 CandidateIndex:0x10,
1320 Preedit: 0x06, // PreeditText | CursorPos
1321 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1343 ParseError: "parse-error"
1348 var keysyms = new Array ();
1349 keysyms["bs"] = "backspace";
1350 keysyms["lf"] = "linefeed";
1351 keysyms["cr"] = keysyms["enter"] = "return";
1352 keysyms["esc"] = "escape";
1353 keysyms["spc"] = "space";
1354 keysyms["del"] = "delete";
1356 function decode_keysym (str) {
1357 if (str.length == 1)
1359 var parts = str.split ("-");
1360 var len = parts.length, i;
1361 var has_modifier = len > 1;
1363 for (i = 0; i < len - 1; i++)
1364 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1366 var key = parts[len - 1];
1369 key = keysyms[key.toLowerCase ()];
1375 for (i = 1; i < len - 1; i++)
1376 str += '-' + parts[i];
1385 parts = new Array ();
1392 MIM.Key = function (val)
1395 if (val instanceof Xex.Term)
1397 else if (typeof val == 'string' || val instanceof String)
1399 this.key = decode_keysym (val);
1401 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1402 if (this.key instanceof Array)
1404 this.key = this.key[0];
1405 this.has_modifier = true;
1408 else if (typeof val == 'number' || val instanceof Number)
1409 this.key = String.fromCharCode (val);
1411 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1414 MIM.Key.prototype.toString = function () { return this.key; };
1416 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1417 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1418 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1422 MIM.KeySeq = function (seq)
1424 this.val = new Array ();
1430 var len = seq.val.length;
1431 for (var i = 0; i < len; i++)
1433 var v = seq.val[i], key;
1434 if (v.type == 'symbol' || v.type == 'string')
1435 key = new MIM.Key (v);
1436 else if (v.type == 'integer')
1437 key = new MIM.Key (v.val);
1439 throw new Xex.ErrTerm (MIM.Error.ParseError,
1440 "Invalid key: " + v);
1441 this.val.push (key);
1442 if (key.has_modifier)
1443 this.has_modifier = true;
1448 var len = seq.val.length;
1449 for (var i = 0; i < len; i++)
1450 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1453 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1457 var proto = new Xex.Term ('keyseq');
1458 proto.Clone = function () { return this; }
1459 proto.Parser = function (domain, node)
1461 var seq = new Array ();
1462 for (node = node.firstChild; node; node = node.nextSibling)
1463 if (node.nodeType == 1)
1465 var term = Xex.Term.Parse (domain, node);
1466 return new MIM.KeySeq (term);
1468 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1470 proto.toString = function ()
1472 var len = this.val.length;
1476 var str = '<keyseq>';
1477 for (var i = 0; i < len; i++)
1481 else if (this.has_modifier)
1483 str += this.val[i].toString ();
1485 return str + '</keyseq>';
1488 MIM.KeySeq.prototype = proto;
1492 MIM.Marker = function () { }
1493 MIM.Marker.prototype = new Xex.Term ('marker');
1494 MIM.Marker.prototype.CharAt = function (ic)
1496 var p = this.Position (ic);
1497 if (p < 0 || p >= ic.preedit.length)
1499 return ic.preedit.charCodeAt (p);
1502 MIM.FloatingMarker = function (name) { this.val = name; };
1503 var proto = new MIM.Marker ();
1504 MIM.FloatingMarker.prototype = proto;
1505 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1506 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1508 MIM.PredefinedMarker = function (name) { this.val = name; }
1509 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1510 MIM.PredefinedMarker.prototype.Position = function (ic)
1512 if (typeof this.pos == 'number')
1514 return this.pos (ic);
1517 var predefined = { }
1519 function def_predefined (name, position)
1521 predefined[name] = new MIM.PredefinedMarker (name);
1522 predefined[name].pos = position;
1525 def_predefined ('@<', 0);
1526 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1527 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1528 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1529 def_predefined ('@[', function (ic) {
1530 if (ic.cursor_pos > 0)
1532 var pos = ic.cursor_pos;
1533 return ic.preedit.FindProp ('candidates', pos - 1).from;
1537 def_predefined ('@]', function (ic) {
1538 if (ic.cursor_pos < ic.preedit.length - 1)
1540 var pos = ic.cursor_pos;
1541 return ic.preedit.FindProp ('candidates', pos).to;
1543 return ic.preedit.length;
1545 for (var i = 0; i < 10; i++)
1546 def_predefined ("@" + i, i);
1547 predefined['@first'] = predefined['@<'];
1548 predefined['@last'] = predefined['@>'];
1549 predefined['@previous'] = predefined['@-'];
1550 predefined['@next'] = predefined['@+'];
1551 predefined['@previous-candidate-change'] = predefined['@['];
1552 predefined['@next-candidate-change'] = predefined['@]'];
1554 MIM.SurroundMarker = function (name)
1557 this.distance = parseInt (name.slice (1));
1558 if (isNaN (this.distance))
1559 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1561 MIM.SurroundMarker.prototype = new MIM.Marker ();
1562 MIM.SurroundMarker.prototype.Position = function (ic)
1564 return ic.cursor_pos + this.distance;
1566 MIM.SurroundMarker.prototype.CharAt = function (ic)
1568 if (this.val == '@-0')
1570 var p = this.Position (ic);
1572 return ic.GetSurroundingChar (p);
1573 else if (p >= ic.preedit.length)
1574 return ic.GetSurroundingChar (p - ic.preedit.length);
1575 return ic.preedit.charCodeAt (p);
1578 MIM.Marker.prototype.Parser = function (domain, node)
1580 var name = node.firstChild.nodeValue;
1581 if (name.charAt (0) == '@')
1583 var n = predefined[name];
1586 if (name.charAt (1) == '-' || name.charAt (1) == '+')
1587 return new MIM.SurroundMarker (name);
1588 throw new Xex.ErrTerm (MIM.Error.ParseError,
1589 "Invalid marker: " + name);
1591 return new MIM.FloatingMarker (name);;
1595 MIM.Selector = function (name)
1599 MIM.Selector.prototype = new Xex.Term ('selector');
1603 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1604 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1605 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1606 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1607 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1608 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
1609 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
1611 MIM.Selector.prototype.Parser = function (domain, node)
1613 var name = node.firstChild.nodeValue;
1614 var s = selectors[name];
1616 throw new Xex.ErrTerm (MIM.Error.ParseError,
1617 "Invalid selector: " + name);
1622 MIM.Rule = function (keyseq, actions)
1624 this.keyseq = keyseq;
1625 this.actions = actions;
1627 MIM.Rule.prototype = new Xex.Term ('rule');
1628 MIM.Rule.prototype.Parser = function (domain, node)
1631 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1633 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1634 var keyseq = Xex.Term.Parse (domain, n);
1635 if (keyseq.type != 'keyseq')
1636 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1637 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1638 return new MIM.Rule (keyseq, actions);
1640 MIM.Rule.prototype.toString = function ()
1645 MIM.Map = function (name)
1648 this.rules = new Array ();
1652 var proto = new Xex.Term ('map');
1654 proto.Parser = function (domain, node)
1656 var name = node.attributes['mname'].nodeValue;
1658 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1659 var map = new MIM.Map (name);
1660 for (var n = node.firstChild; n; n = n.nextSibling)
1661 if (n.nodeType == 1)
1662 map.rules.push (Xex.Term.Parse (domain, n));
1666 proto.toString = function ()
1668 var str = '<map mname="' + this.name + '">';
1669 var len = this.rules.length;
1670 for (i = 0; i < len; i++)
1671 str += this.rules[i];
1672 return str + '</map>';
1675 MIM.Map.prototype = proto;
1678 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1680 MIM.Action = function (domain, terms)
1682 var args = new Array ();
1683 args.push (Xex.CatchTag_.mimtag);
1684 for (var i = 0; i < terms.length; i++)
1685 args.push (terms[i]);
1686 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1689 MIM.Action.prototype.Run = function (domain)
1691 var result = this.action.Eval (domain);
1692 if (result.type == 'error')
1694 domain.context.Error = result.toString ();
1697 return (result != Xex.CatchTag._mimtag);
1700 MIM.Keymap = function ()
1703 this.submaps = null;
1709 function add_rule (keymap, rule, branch_actions)
1711 var keyseq = rule.keyseq;
1712 var len = keyseq.val.length;
1715 for (var i = 0; i < len; i++)
1717 var key = keyseq.val[i];
1721 if (! keymap.submaps)
1722 keymap.submaps = {};
1724 sub = keymap.submaps[key.key];
1726 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1730 keymap.map_actions = rule.actions;
1731 keymap.branch_actions = branch_actions;
1734 proto.Add = function (map, branch_actions)
1736 var rules = map.rules;
1737 var len = rules.length;
1739 for (var i = 0; i < len; i++)
1740 add_rule (this, rules[i], branch_actions);
1742 proto.Lookup = function (keys, index)
1746 if (index < keys.val.length && this.submaps
1747 && ! keys.val[index])
1749 Xex.Log ('invalid key at ' + index);
1750 throw 'invalid key';
1753 if (index < keys.val.length && this.submaps
1754 && (sub = this.submaps[keys.val[index].key]))
1757 return sub.Lookup (keys, index);
1759 return { map: this, index: index };
1762 MIM.Keymap.prototype = proto;
1765 MIM.State = function (name)
1768 this.keymap = new MIM.Keymap ();
1772 var proto = new Xex.Term ('state');
1774 proto.Parser = function (domain, node)
1776 var map_list = domain.map_list;
1777 var name = node.attributes['sname'].nodeValue;
1779 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1780 var state = new MIM.State (name);
1781 for (node = node.firstElement (); node; node = node.nextElement ())
1783 if (node.nodeName == 'title')
1784 state.title = node.firstChild.nodeValue;
1787 var n = node.firstElement ();
1788 if (node.nodeName == 'branch')
1789 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1790 Xex.Term.Parse (domain, n, null));
1791 else if (node.nodeName == 'state-hook')
1792 state.enter_actions = Xex.Term.Parse (domain, n, null);
1793 else if (node.nodeName == 'catch-all-branch')
1794 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1800 proto.toString = function ()
1802 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1805 MIM.State.prototype = proto;
1809 function Block (index, term)
1813 this.Data = term.val;
1814 else if (term.IsList)
1816 this.Data = new Array ();
1817 for (var i = 0; i < term.val.length; i++)
1818 this.Data.push (term.val[i].val);
1822 Block.prototype.Count = function () { return this.Data.length; }
1823 Block.prototype.get = function (i)
1825 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1828 MIM.Candidates = function (candidates, column)
1830 this.column = column;
1834 this.blocks = new Array ();
1836 for (var i = 0; i < candidates.length; i++)
1838 var block = new Block (this.total, candidates[i]);
1839 this.blocks.push (block);
1840 this.total += block.Count ();
1846 return (this.column > 0 ? this.index % this.column
1847 : this.index - this.blocks[this.row].Index);
1850 function prev_group ()
1852 var col = get_col.call (this);
1854 if (this.column > 0)
1856 this.index -= this.column;
1857 if (this.index >= 0)
1858 nitems = this.column;
1861 var lastcol = (this.total - 1) % this.column;
1862 this.index = (col < lastcol ? this.total - lastcol + col
1864 this.row = this.blocks.length - 1;
1865 nitems = lastcol + 1;
1867 while (this.blocks[this.row].Index > this.index)
1872 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1873 nitems = this.blocks[this.row].Count ();
1874 this.index = (this.blocks[this.row].Index
1875 + (col < nitems ? col : nitems - 1));
1880 function next_group ()
1882 var col = get_col.call (this);
1884 if (this.column > 0)
1886 this.index += this.column - col;
1887 if (this.index < this.total)
1889 if (this.index + col >= this.total)
1891 nitems = this.total - this.index;
1892 this.index = this.total - 1;
1896 nitems = this.column;
1905 while (this.blocks[this.row].Index > this.index)
1910 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1911 nitems = this.blocks[this.row].Count ();
1912 this.index = (this.blocks[this.row].Index
1913 + (col < nitems ? col : nitems - 1));
1920 if (this.index == 0)
1922 this.index = this.total - 1;
1923 this.row = this.blocks.length - 1;
1928 if (this.blocks[this.row].Index > this.index)
1936 if (this.index == this.total)
1943 var b = this.blocks[this.row];
1944 if (this.index == b.Index + b.Count ())
1951 this.index -= get_col.call (this);
1952 while (this.blocks[this.row].Index > this.index)
1958 var b = this.blocks[this.row];
1959 if (this.column > 0)
1961 if (this.index + 1 < this.total)
1963 this.index += this.column - get_col.call (this) + 1;
1964 while (b.Index + b.Count () <= this.index)
1965 b = this.blocks[++this.row];
1969 this.index = b.Index + b.Count () - 1;
1972 MIM.Candidates.prototype.Current = function ()
1974 var b = this.blocks[this.row];
1975 return b.get (this.index - b.Index);
1978 MIM.Candidates.prototype.Select = function (selector)
1980 if (selector.type == 'selector')
1982 switch (selector.val)
1984 case '@<': first.call (this); break;
1985 case '@>': last.call (this); break;
1986 case '@-': prev.call (this); break;
1987 case '@+': next.call (this); break;
1988 case '@[': prev_group.call (this); break;
1989 case '@]': next_group.cal (this); break;
1992 return this.Current ();
1995 if (this.column > 0)
1997 col = this.index % this.column;
1998 start = this.index - col;
1999 end = start + this.column;
2003 start = this.blocks[this.row].Index;
2004 col = this.index - start;
2005 end = start + this.blocks[this.row].Count;
2007 if (end > this.total)
2009 this.index += selector.val - col;
2010 if (this.index >= end)
2011 this.index = end - 1;
2012 if (this.column > 0)
2014 if (selector.val > col)
2015 while (this.blocks[this.row].Index + this.blocks[this.row].Count
2019 while (this.blocks[this.row].Index > this.index)
2022 return this.Current ();
2026 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2027 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2028 MIM.im_domain.DefType (MIM.Marker.prototype);
2029 MIM.im_domain.DefType (MIM.Selector.prototype);
2030 MIM.im_domain.DefType (MIM.Rule.prototype);
2031 MIM.im_domain.DefType (MIM.Map.prototype);
2032 MIM.im_domain.DefType (MIM.State.prototype);
2035 var im_domain = MIM.im_domain;
2037 function Finsert (domain, vari, args)
2040 if (args[0].type == 'integer')
2041 text = String.fromCharCode (args[0].val);
2044 domain.context.ins (text, null);
2048 function Finsert_candidates (domain, vari, args)
2050 var ic = domain.context;
2051 var gsize = domain.variables['candidates_group_size'];
2052 var candidates = new MIM.Candidates (args, gsize ? gsize.Intval () : 0);
2053 ic.ins (candidates.Current (), candidates);
2057 function Fdelete (domain, vari, args)
2059 var ic = domain.context;
2060 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2061 return new Xex.IntTerm (ic.del (pos));
2064 function Fselect (domain, vari, args)
2066 var ic = domain.context;
2067 var can = ic.candidates;
2071 var old_text = can.Current ();
2072 var new_text = can.Select (args[0]);
2073 ic.rep (old_text, new_text, can);
2076 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2080 function Fshow (domain, vari, args)
2082 domain.context.candidate_show = true;
2083 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2087 function Fhide (domain, vari, args)
2089 domain.context.candidate_show = false;
2090 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2094 function Fchar_at (domain, vari, args)
2096 return new Xex.IntTerm (args[0].CharAt (domain.context));
2099 function Fmove (domain, vari, args)
2101 var ic = domain.context;
2102 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2104 return new Xex.IntTerm (pos);
2107 function Fmark (domain, vari, args)
2109 args[0].Mark (domain.context);
2113 function Fpushback (domain, vari, args)
2115 var a = (args[0].IsInt ? args[0].Intval ()
2116 : args[0].IsStr ? new KeySeq (args[0])
2118 domain.context.pushback (a);
2122 function Fpop (domain, vari, args)
2124 var ic = domain.context;
2125 if (ic.key_head < ic.keys.val.length)
2126 ic.keys.val.splice (ic.keys_head, 1);
2130 function Fundo (domain, vari, args)
2132 var ic = domain.context;
2133 var n = args.length == 0 ? -2 : args[0].val;
2134 Xex.Log ('undo with arg ' + args[0]);
2136 ic.keys.val.splice (ic.keys.val.length + n, -n);
2138 ic.keys.val.splice (n, ic.keys.val.length);
2143 function Fcommit (domain, vari, args)
2145 domain.context.commit ();
2149 function Funhandle (domain, vari, args)
2151 domain.context.commit ();
2152 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2155 function Fshift (domain, vari, args)
2157 var ic = domain.context;
2158 var state_name = args[0].val;
2159 var state = ic.im.state_list[state_name];
2161 throw ("Unknown state: " + state_name);
2166 function Fshiftback (domain, vari, args)
2168 domain.context.shift (null);
2172 function Fkey_count (domain, vari, args)
2174 return new Xex.IntTerm (domain.context.key_head);
2177 function Fsurrounding_flag (domain, vari, args)
2179 return new Xex.IntTerm (-1);
2182 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2183 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2184 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2185 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2186 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2187 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2188 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2189 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2190 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2191 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2192 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2193 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2194 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2195 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2196 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2197 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2198 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2199 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2204 function get_global_var (vname)
2206 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2207 MIM.im_global.Load ()
2208 return MIM.im_global.domain.variables[vname];
2211 function include (node)
2213 node = node.firstElement ();
2214 if (node.nodeName != 'tags')
2217 var lang = null, name = null, extra = null;
2218 for (node = node.firstElement (); node; node = node.nextElement ())
2220 if (node.nodeName == 'language')
2221 lang = node.firstChild.nodeValue;
2222 else if (node.nodeName == 'name')
2223 name = node.firstChild.nodeValue;
2224 else if (node.nodeName == 'extra-id')
2225 extra = node.firstChild.nodeValue;
2227 if (! lang || ! MIM.imlist[lang])
2231 if (! name || ! (im = MIM.imlist[lang][name]))
2236 if (! (im = MIM.imextra[lang][extra]))
2239 if (im.load_status != MIM.LoadStatus.Loaded
2240 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2247 parsers['description'] = function (node)
2249 this.description = node.firstChild.nodeValue;
2251 parsers['variable-list'] = function (node)
2253 for (node = node.firstElement (); node; node = node.nextElement ())
2255 var vname = node.attributes['vname'].nodeValue;
2256 if (this != MIM.im_global)
2258 var vari = get_global_var (vname);
2260 this.domain.Defvar (vname);
2262 vname = Xex.Term.Parse (this.domain, node)
2265 parsers['command-list'] = function (node)
2268 parsers['macro-list'] = function (node)
2270 for (var n = node.firstElement (); n; n = n.nextElement ())
2271 if (n.nodeName == 'xi:include')
2273 var im = include (n);
2275 alert ('inclusion fail');
2277 for (var macro in im.domain.functions)
2279 var func = im.domain.functions[macro];
2280 if (func instanceof Xex.Macro)
2281 im.domain.CopyFunc (this.domain, macro);
2283 n = n.previousSibling;
2284 node.removeChild (n.nextSibling);
2286 Xex.Term.Parse (this.domain, node.firstElement (), null);
2288 parsers['title'] = function (node)
2290 this.title = node.firstChild.nodeValue;
2292 parsers['map-list'] = function (node)
2294 for (node = node.firstElement (); node; node = node.nextElement ())
2296 if (node.nodeName == 'xi:include')
2298 var im = include (node);
2301 alert ('inclusion fail');
2304 for (var mname in im.map_list)
2305 this.map_list[mname] = im.map_list[mname];
2309 var map = Xex.Term.Parse (this.domain, node);
2310 this.map_list[map.name] = map;
2314 parsers['state-list'] = function (node)
2316 this.domain.map_list = this.map_list;
2317 for (node = node.firstElement (); node; node = node.nextElement ())
2319 if (node.nodeName == 'xi:include')
2321 var im = include (node);
2323 alert ('inclusion fail');
2324 for (var sname in im.state_list)
2326 state = im.state_list[sname];
2327 if (! this.initial_state)
2328 this.initial_state = state;
2329 this.state_list[sname] = state;
2332 else if (node.nodeName == 'state')
2334 var state = Xex.Term.Parse (this.domain, node);
2336 state.title = this.title;
2337 if (! this.initial_state)
2338 this.initial_state = state;
2339 this.state_list[state.name] = state;
2342 delete this.domain.map_list;
2345 MIM.IM = function (lang, name, extra_id, file)
2349 this.extra_id = extra_id;
2351 this.load_status = MIM.LoadStatus.NotLoaded;
2352 this.domain = new Xex.Domain (this.lang + '-'
2353 + (this.name != 'nil'
2354 ? this.name : this.extra_id),
2355 MIM.im_domain, null);
2361 var node = Xex.Load (null, this.file);
2364 this.load_status = MIM.LoadStatus.Error;
2368 this.initial_state = null;
2369 this.state_list = {};
2370 for (node = node.firstElement (); node; node = node.nextElement ())
2372 var name = node.nodeName;
2373 var parser = parsers[name];
2375 parser.call (this, node);
2377 this.load_status = MIM.LoadStatus.Loaded;
2382 MIM.IM.prototype = proto;
2384 MIM.IC = function (im, target)
2386 if (im.load_status == MIM.LoadStatus.NotLoaded)
2388 if (im.load_status != MIM.LoadStatus.Loaded)
2389 alert ('im:' + im.name + ' error:' + im.load_status);
2391 this.target = target;
2392 this.domain = new Xex.Domain ('context', im.domain, this);
2394 this.range = new Array ();
2395 this.range[0] = this.range[1] = 0;
2397 this.initial_state = this.im.initial_state;
2398 this.keys = new MIM.KeySeq ();
2399 this.marker_positions = new Array ();
2400 this.candidate_table = new MIM.CandidateTable ();
2404 MIM.CandidateTable = function ()
2406 this.table = new Array ();
2409 MIM.CandidateTable.prototype.get = function (pos)
2411 for (var i = 0; i < this.table.length; i++)
2413 var elt = this.table[i];
2414 if (elt.from < pos && pos <= elt.to)
2419 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2421 for (var i = 0; i < this.table.length; i++)
2423 var elt = this.table[i];
2424 if (elt.from < to && elt.to > from)
2428 elt.val = candidates;
2432 this.table.push ({ from: from, to: to, val: candidates });
2435 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2437 var diff = inserted - (to - from);
2440 for (var i = 0; i < this.table.length; i++)
2442 var elt = this.table[i];
2451 MIM.CandidateTable.prototype.clear = function ()
2453 this.table.length = 0;
2456 function detach_candidates (ic)
2458 ic.candidate_table.clear ();
2459 ic.candidates = null;
2460 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2461 | MIM.ChangedStatus.CandidateList
2462 | MIM.ChangedStatus.CandidateIndex
2463 | MIM.ChangedStatus.CandidateShow);
2466 function set_cursor (prefix, pos)
2468 this.cursor_pos = pos;
2469 this.candidates = this.candidate_table.get (pos);
2472 function save_state ()
2474 this.state_var_values = this.domain.SaveValues ();
2475 this.state_preedit = this.preedit;
2476 this.state_key_head = this.key_head;
2477 this.state_pos = this.cursor_pos;
2480 function restore_state ()
2482 this.domain.RestoreValues (this.state_var_values);
2483 this.preedit = this.state_preedit;
2484 set_cursor.call (this, "restore", this.state_pos);
2487 function handle_key ()
2489 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
2490 + '" in ' + this.state.name + ':' + this.keymap.name
2491 + " key/state/commit-head/len:"
2492 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
2493 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
2496 if (out.index > this.key_head)
2498 this.key_head = out.index;
2499 Xex.Log (' with submap for ' + this.key_head + 'keys');
2500 restore_state.call (this);
2502 if (sub.map_actions)
2504 Xex.Log ('taking map actions:');
2505 if (! this.take_actions (sub.map_actions))
2508 else if (sub.submaps)
2510 Xex.Log ('no map actions');
2511 for (var i = this.state_key_head; i < this.key_head; i++)
2513 Xex.Log ('inserting key:' + this.keys.val[i].key);
2514 this.ins (this.keys.val[i].key, null);
2519 Xex.Log ('terminal:');
2520 if (this.keymap.branch_actions != null)
2522 Xex.Log ('branch actions:');
2523 if (! this.take_actions (this.keymap.branch_actions))
2526 if (sub != this.state.keymap)
2527 this.shift (this.state);
2532 Xex.Log (' without submap');
2534 var current_state = this.state;
2535 var map = this.keymap;
2537 if (map.branch_actions)
2539 Xex.Log ('branch actions');
2540 if (! this.take_actions (map.branch_actions))
2544 if (map == this.keymap)
2546 Xex.Log ('no state change');
2547 if (map == this.initial_state.keymap
2548 && this.key_head < this.keys.val.length)
2550 Xex.Log ('unhandled');
2553 if (this.keymap != current_state.keymap)
2554 this.shift (current_state);
2555 else if (this.keymap.actions == null)
2556 this.shift (this.initial_state);
2565 this.cursor_pos = 0;
2566 this.candidate_show = false;
2567 this.prev_state = null;
2568 this.title = this.initial_state.title;
2569 this.state_preedit = '';
2570 this.state_key_head = 0;
2571 this.state_var_values = {};
2574 this.commit_key_head = 0;
2575 this.key_unhandled = false;
2576 this.unhandled_key = null;
2577 this.changed = MIM.ChangedStatus.None;
2578 this.error_message = '';
2579 this.title = this.initial_state.title;
2582 this.preedit_saved = '';
2583 this.candidate_table.clear ();
2584 this.candidates = null;
2585 this.candidate_show = false;
2586 for (var elt in this.marker_positions)
2587 this.marker_positions[elt] = 0;
2588 this.shift (this.initial_state);
2591 catch_args: new Array (Xex.CatchTag._mimtag, null),
2593 take_actions: function (actions)
2595 var func_progn = this.domain.GetFunc ('progn');
2596 var func_catch = this.domain.GetFunc ('catch');
2597 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2598 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2599 term = term.Eval (this.domain);
2600 return (! term.IsSymbol || term.val != '@mimtag');
2603 GetSurroundingChar: function (pos)
2607 pos += this.range[0];
2613 pos += this.range[1];
2614 if (pos >= this.target.value.length)
2617 return this.target.value.charCodeAt (pos);
2620 DelSurroundText: function (pos)
2625 pos += this.range[0];
2631 text = this.target.value.substring (0, pos);
2632 if (this.range[0] < this.target.value.length)
2633 text += this.target.value.substring (this.range[0]);
2634 this.target.value = text;
2635 this.range[1] -= this.range[0] - pos;
2636 this.range[0] = pos;
2640 pos += this.range[1];
2641 text = this.target.value.substring (0, this.range[1]);
2642 if (pos >= this.target.value.length)
2643 pos = this.target.value.length;
2645 text += this.target.value.substring (pos);
2646 this.target.value = text;
2650 adjust_markers: function (from, to, inserted)
2652 var diff = inserted - (to - from);
2654 for (var name in this.marker_positions)
2656 var pos = this.marker_positions[name];
2660 this.marker_positions[name] += diff;
2661 else if (pos > from)
2662 this.marker_positions[name] = from;
2665 if (this.cursor_pos >= to)
2666 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2667 else if (this.cursor_pos > from)
2668 set_cursor.call (this, 'adjust', from)
2671 preedit_replace: function (from, to, text, candidates)
2673 this.preedit = (this.preedit.substring (0, from)
2674 + text + this.preedit.substring (to));
2675 this.adjust_markers (from, to, text.length);
2676 this.candidate_table.adjust (from, to, text.length);
2678 this.candidate_table.put (from, from + text.length, candidates)
2681 ins: function (text, candidates)
2683 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2684 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2687 rep: function (old_text, new_text, candidates)
2689 this.preedit_replace (this.cursor_pos - old_text.length,
2690 this.cursor_pos, new_text, candidates);
2691 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2696 var deleted = pos - this.cursor_pos;
2697 if (pos < this.cursor_pos)
2701 this.DelSurroundText (pos);
2702 deleted = - this.cursor_pos;
2705 if (pos < this.cursor_pos)
2706 this.preedit_replace (pos, this.cursor_pos, '', null);
2710 if (pos > this.preedit.length)
2712 this.DelSurroundText (pos - this.preedit.length);
2713 deleted = this.preedit.length - this.cursor_pos;
2714 pos = this.preedit.length;
2716 if (pos > this.cursor_pos)
2717 this.preedit_replace (this.cursor_pos, pos, '', null);
2720 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2726 this.candidate_show = true;
2727 this.changed |= MIM.ChangedStatus.CandidateShow;
2732 this.candidate_show = false;
2733 this.changed |= MIM.ChangedStatus.CandidateShow;
2736 move: function (pos)
2740 else if (pos > this.preedit.length)
2741 pos = this.preedit.length;
2742 if (pos != this.cursor_pos)
2744 set_cursor.call (this, 'move', pos);
2745 this.changed |= MIM.ChangedStatus.Preedit;
2749 pushback: function (n)
2751 if (n instanceof MIM.KeySeq)
2753 if (this.key_head > 0)
2755 if (this.key_head < this.keys.val.length)
2756 this.keys.val.splice (this.key_head,
2757 this.keys.val.length - this.key_head);
2758 for (var i = 0; i < n.val.length; i++)
2759 this.keys.val.push (n.val[i]);
2765 if (this.key_head < 0)
2772 this.key_head = - n;
2773 if (this.key_head > this.keys.val.length)
2774 this.key_head = this.keys.val.length;
2776 Xex.Log ('0: key head = ' + this.key_head);
2781 if (this.key_head < this.keys.val.length)
2782 this.keys.val.splice (this.key_head, 1);
2787 if (this.preedit.length > 0)
2789 this.candidate_table.clear ();
2790 this.produced += this.preedit;
2791 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2792 this.preedit_saved = '';
2794 this.commit_key_head = this.key_head;
2798 shift: function (state)
2802 if (this.prev_state == null)
2804 state = this.prev_state;
2807 if (state == this.initial_state)
2812 this.keys.val.splice (0, this.key_head);
2813 this.key_head = this.state_key_head = this.commit_key_head = 0;
2814 this.prev_state = null;
2819 if (state != this.state)
2820 this.prev_state = this.state;
2822 if (state != this.state && state.enter_actions)
2823 this.take_actions (state.enter_actions);
2824 if (! this.state || this.state.title != state.title)
2825 this.changed |= MIM.ChangedStatus.StateTitle;
2827 this.keymap = state.keymap;
2828 save_state.call (this);
2831 Filter: function (key)
2835 Xex.Log ("active = false");
2836 this.key_unhandled = true;
2837 this.unhandled_key = key;
2840 if (key.key == '_reload')
2842 this.changed = MIM.ChangedStatus.None;
2844 this.key_unhandled = false;
2845 this.keys.val.push (key);
2847 Xex.Log ("keyhead=" + this.key_head + " len="+this.keys.val.length);
2848 while (this.key_head < this.keys.val.length)
2850 if (! handle_key.call (this))
2852 if (this.key_head < this.keys.val.length)
2854 this.unhandled_key = this.keys.val[this.key_head];
2855 this.keys.val.splice (this.key_head, this.key_head + 1);
2857 if (this.state_key_head > 0)
2858 this.state_key_head--;
2859 if (this.commit_key_head > 0)
2860 this.commit_key_head--;
2861 this.key_unhandled = true;
2867 this.key_unhandled = true;
2871 if (this.keymap == this.initial_state.keymap)
2874 if (this.commit_key_head > 0)
2876 this.keys.val.splice (0, this.commit_key_head);
2877 this.key_head -= this.commit_key_head;
2878 this.state_key_head -= this.commit_key_head;
2879 this.commit_key_head = 0;
2881 if (this.key_unhandled)
2883 this.keys.val.length = 0;
2884 //this.keys.val.splice (0, this.keys.val.length);
2885 this.key_head = this.state_key_head = this.commit_key_head = 0;
2887 return (! this.key_unhandled
2888 && this.produced.length == 0);
2892 MIM.IC.prototype = proto;
2894 var node = Xex.Load (null, "imlist.xml");
2895 for (node = node.firstChild; node; node = node.nextSibling)
2896 if (node.nodeName == 'input-method')
2898 var lang = null, name = null, extra_id = null, file = null;
2900 for (var n = node.firstChild; n; n = n.nextSibling)
2902 if (n.nodeName == 'language')
2903 lang = n.firstChild.nodeValue;
2904 else if (n.nodeName == 'name')
2905 name = n.firstChild.nodeValue;
2906 else if (n.nodeName == 'extra-id')
2907 extra_id = n.firstChild.nodeValue;
2908 else if (n.nodeName == 'filename')
2909 file = n.firstChild.nodeValue;
2911 if (name && name != 'nil')
2913 if (! MIM.imlist[lang])
2914 MIM.imlist[lang] = {};
2915 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2917 else if (extra_id && extra_id != 'nil')
2919 if (! MIM.imextra[lang])
2920 MIM.imextra[lang] = {};
2921 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2924 if (MIM.imextra.t && MIM.imextra.t.global)
2925 MIM.im_global = MIM.imextra.t.global;
2928 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2929 MIM.im_global.load_status = MIM.LoadStatus.Error;
2935 var keys = new Array ();
2937 keys[0x08] = 'backspace';
2938 keys[0x0D] = 'return';
2939 keys[0x1B] = 'escape';
2940 keys[0x20] = 'space';
2941 keys[0x21] = 'pageup';
2942 keys[0x22] = 'pagedown';
2944 keys[0x24] = 'home';
2945 keys[0x25] = 'left';
2947 keys[0x27] = 'right';
2948 keys[0x28] = 'down';
2949 keys[0x2D] = 'insert';
2950 keys[0x2E] = 'delete';
2951 for (var i = 1; i <= 12; i++)
2952 keys[111 + i] = "f" + i;
2953 keys[0x90] = "numlock";
2954 keys[0xF0] = "capslock";
2957 keyids['U+0008'] = 'backspace';
2958 keyids['U+0009'] = 'tab';
2959 keyids['U+0018'] = 'cancel';
2960 keyids['U+001B'] = 'escape';
2961 keyids['U+0020'] = 'space';
2962 keyids['U+007F'] = 'delete';
2965 modifiers.Shift = 1;
2966 modifiers.Control = 1;
2968 modifiers.AltGraph = 1;
2971 MIM.decode_key_event = function (event)
2973 var key = event.keyIdentifier;
2975 if (key) // keydown event of Chrome
2980 if (event.ctrlKey) mod += 'C-';
2981 if (event.metaKey) mod += 'M-';
2982 if (event.altKey) mod += 'A-';
2983 var keysym = keyids[key];
2986 else if (key.match(/^U\+([0-9A-Z]+)$/))
2988 if (mod.length == 0)
2990 key = String.fromCharCode (parseInt (RegExp.$1, 16));
2993 key = key.toLowerCase ();
2994 if (event.shiftKey) mod += 'S-';
2995 return new MIM.Key (mod + key);
2999 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
3000 : event.charCode ? event.charCode
3004 if (event.type == 'keydown')
3009 if (event.shiftKey) key = "S-" + key ;
3012 key = String.fromCharCode (key);
3014 if (event.altKey) key = "A-" + key ;
3015 if (event.ctrlKey) key = "C-" + key ;
3016 return new MIM.Key (key);
3020 MIM.add_event_listener
3021 = (window.addEventListener
3022 ? function (target, type, listener) {
3023 target.addEventListener (type, listener, false);
3025 : window.attachEvent
3026 ? function (target, type, listener) {
3027 target.attachEvent ('on' + type,
3029 listener.call (target, window.event);
3032 : function (target, type, listener) {
3034 = function (e) { listener.call (target, e || window.event); };
3037 MIM.debug_print = function (event, ic)
3041 if (! MIM.debug_nodes)
3043 MIM.debug_nodes = new Array ();
3044 MIM.debug_nodes['status0'] = document.getElementById ('status0');
3045 MIM.debug_nodes['status1'] = document.getElementById ('status1');
3046 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3047 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3048 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3049 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3050 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3051 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3053 var target = event.target;
3054 var code = event.keyCode;
3055 var ch = event.type == 'keypress' ? event.charCode : 0;
3056 var key = MIM.decode_key_event (event);
3059 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3060 index = (event.type == 'keydown' ? '0' : '1');
3062 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3064 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3065 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3066 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3069 MIM.debug_nodes.keypress.innerHTML = '';
3070 MIM.debug_nodes.status1.innerHTML = '';
3071 MIM.debug_nodes.keymap1.innerHTML = '';
3072 MIM.debug_nodes.preedit1.innerHTML = ''
3076 MIM.get_range = function (target, ic)
3079 if (target.selectionStart != null) // for Mozilla
3081 from = target.selectionStart;
3082 to = target.selectionEnd;
3086 var r = document.selection.createRange ();
3087 var rr = r.duplicate ();
3089 rr.moveToElementText (target);
3090 rr.setEndPoint ('EndToEnd', range);
3091 from = rr.text.length - r.text.length;
3092 to = rr.text.length;
3094 if (ic.range[0] == from && ic.range[1] == to
3095 && (to == from || target.value.substring (from, to) == ic.preedit))
3102 MIM.set_caret = function (target, ic)
3104 if (target.setSelectionRange) // Mozilla
3106 var scrollTop = target.scrollTop;
3107 target.setSelectionRange (ic.range[0], ic.range[1]);
3108 target.scrollTop = scrollTop;
3112 var range = target.createTextRange ();
3113 range.moveStart ('character', ic.range[0]);
3114 range.moveEnd ('character', ic.range[1]);
3119 MIM.update = function (target, ic)
3121 var text = target.value;
3122 target.value = (text.substring (0, ic.range[0])
3125 + text.substring (ic.range[1]));
3126 ic.range[0] += ic.produced.length;
3127 ic.range[1] = ic.range[0] + ic.preedit.length;
3128 MIM.set_caret (target, ic);
3135 padingLeft: 'padding-left',
3136 paddingRight: 'padding-right',
3137 paddingTop: 'padding-top',
3138 paddintBottom: 'padding-bottom',
3139 borderLeftStyle: 'border-left-style',
3140 borderRightStyle: 'border-right-style',
3141 borderTopStyle: 'border-top-style',
3142 borderBottomStyle: 'border-bottom-style',
3143 borderLeftWidth: 'border-left-width',
3144 borderRightWidth: 'border-right-width',
3145 borderTopWidth: 'border-top-width',
3146 borderBottomWidth: 'border-bottom-width',
3147 fontFamily: 'font-family',
3148 fontSize: 'font-size',
3149 lineHeight: 'line-height',
3150 letterSpacing: 'letter-spacing',
3151 wordSpacing: 'word-spacing' };
3153 function copy_style (from, to)
3155 var from_style = getComputedStyle(from,'');
3157 for(var name in style_props)
3158 to.style[name] = from_style.getPropertyValue (style_props[name]);
3160 to.style.left = from.offsetLeft + 'px';
3161 to.style.top = from.offsetTop + 'px';
3162 to.style.width = from.offsetWidth;
3163 to.style.height = from.offsetHeight;
3164 to.scrollLeft = from.scrollLeft;
3165 to.scrollTop = from.scrollTop;
3168 MIM.show = function (target, ic)
3172 var elm = document.createTextNode ('');
3174 ic.can_node = document.createElement ('div');
3175 ic.can_node.style.visibility="hidden";
3176 ic.can_node.style.position = "absolute";
3177 ic.can_node.appendChild (elm)
3178 elm = document.createElement('span');
3179 elm.innerHTML = '.';
3180 ic.can_node.appendChild (elm)
3181 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
3184 copy_style (target, ic.can_node);
3185 ic.can_node.firstChild.value = target.value.substr (0, ic.range[0]);
3186 var x = target.offsetLeft + ic.can_node.lastChild.offsetLeft +2;
3187 var y = target.offsetTop + ic.can_node.lastChild.offsetTop -2;
3189 Xex.Log ("x:" + x + "px y:" + y + "px");
3193 MIM.focus_in = function (event)
3195 var target = event.target;
3196 var ic = target.mim_ic;
3197 if (ic.wait_update == true)
3199 Xex.Log ("Focus in " + target.tagName + ' IGNORED');
3200 event.preventDefault ();
3203 Xex.Log ("Focus in " + target.tagName);
3204 ic.Filter (MIM.Key.FocusIn);
3205 MIM.update (target, ic);
3208 MIM.focus_out = function (event)
3210 var target = event.target;
3211 var ic = target.mim_ic;
3212 function reset_update () { ic.wait_update = false; };
3213 if (ic.wait_update == true)
3215 Xex.Log ("Focus out " + target.tagName + ' IGNORED');
3216 event.preventDefault ();
3219 Xex.Log ("Focus out " + target.tagName);
3220 ic.Filter (MIM.Key.FocusOut);
3221 ic.wait_update = true;
3222 MIM.update (target, ic, true);
3223 setTimeout (reset_update, 1000);
3226 MIM.keydown = function (event)
3228 var target = event.target;
3229 if (target.id == 'log')
3231 if (! (target.type == "text" || target.type == "textarea"))
3234 var ic = target.mim_ic;
3235 if (! ic || ic.im != MIM.current)
3237 target.mim_ic = null;
3238 Xex.Log ('creating IC');
3239 ic = new MIM.IC (MIM.current, target);
3240 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3243 MIM.add_event_listener (target, 'focus', MIM.focus_in);
3244 MIM.add_event_listener (target, 'blur', MIM.focus_out);
3245 MIM.get_range (target, ic)
3249 if (! MIM.get_range (target, ic))
3252 MIM.show (target, ic);
3253 MIM.debug_print (event, ic);
3254 ic.key = MIM.decode_key_event (event);
3257 Xex.Log ("filtering " + ic.key);
3259 var result = ic.Filter (ic.key);
3261 Xex.Log ('Error' + e);
3264 MIM.update (target, ic);
3265 if (! ic.key_unhandled)
3266 event.preventDefault ();
3270 MIM.keypress = function (event)
3272 var target = event.target;
3273 if (target.id == 'log')
3275 if (! (target.type == "text" || target.type == "textarea"))
3278 var ic = target.mim_ic;
3282 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3285 ic.key = MIM.decode_key_event (event);
3292 Xex.Log ("filtering " + ic.key);
3294 var result = ic.Filter (ic.key);
3295 Xex.Log ("result = " + result);
3297 Xex.Log ('Error:' + e);
3300 MIM.update (target, ic);
3301 if (! ic.key_unhandled)
3302 event.preventDefault ();
3304 Xex.Log ("error:" + e);
3305 event.preventDefault ();
3307 MIM.debug_print (event, ic);
3314 var lang_category = {
3316 cs: { name: 'Czech' },
3317 da: { name: 'Danish' },
3318 el: { name: 'Greek' },
3319 en: { name: 'English' },
3320 eo: { name: 'Esperanto' },
3321 fr: { name: 'French' },
3322 grc: { name: 'ClassicGreek' },
3323 hr: { name: 'Croatian' },
3324 hy: { name: 'Armenian' },
3325 ka: { name: 'Georgian' },
3326 kk: { name: 'Kazakh' },
3327 ru: { name: 'Russian' },
3328 sk: { name: 'Slovak' },
3329 sr: { name: 'Serbian' },
3330 sv: { name: 'Swedish' },
3331 yi: { name: 'Yiddish' } },
3333 ar: { name: 'Arabic' },
3334 dv: { name: 'Divehi' },
3335 fa: { name: 'Persian' },
3336 he: { name: 'Hebrew' },
3337 kk: { name: 'Kazakh' },
3338 ps: { name: 'Pushto' },
3339 ug: { name: 'Uighur' },
3340 yi: { name: 'Yiddish' } },
3342 as: { name: 'Assamese' },
3343 bn: { name: 'Bengali' },
3344 bo: { name: 'Tibetan' },
3345 gu: { name: 'Gujarati' },
3346 hi: { name: 'Hindi' },
3347 kn: { name: 'Kannada' },
3348 ks: { name: 'Kashmiri' },
3349 ml: { name: 'Malayalam' },
3350 mr: { name: 'Marathi' },
3351 ne: { name: 'Nepali' },
3352 or: { name: 'Oriya' },
3353 pa: { name: 'Panjabi' },
3354 sa: { name: 'Sanskirit' },
3355 sd: { name: 'Sindhi' },
3356 si: { name: 'Sinhalese' },
3357 ta: { name: 'Tamil' },
3358 te: { name: 'Telugu' },
3359 ur: { name: 'Urdu' } },
3361 cmc: { name: 'Cham' },
3362 km: { name: 'Khmer'},
3363 lo: { name: 'Lao' },
3364 my: { name: 'Burmese' },
3365 tai: { name: 'Tai Viet' },
3366 th: { name: 'Thai' },
3367 vi: { name: 'Vietanamese' } },
3369 ii: { name: 'Yii' },
3370 ja: { name: 'Japanese' },
3371 ko: { name: 'Korean' },
3372 zh: { name: 'Chinese' } },
3374 am: { name: 'Amharic' },
3375 ath: { name: 'Carrier' },
3376 bla: { name: 'Blackfoot' },
3377 cr: { name: 'Cree' },
3378 eo: { name: 'Esperanto' },
3379 iu: { name: 'Inuktitut' },
3380 nsk: { name: 'Naskapi' },
3381 oj: { name: 'Ojibwe' },
3382 t: { name: 'Generic' } }
3385 function categorize_im ()
3387 var cat, lang, list, name;
3388 for (lang in MIM.imlist)
3391 for (cat in lang_category)
3392 if (lang_category[cat][lang])
3394 list = lang_category[cat][lang].list;
3396 list = lang_category[cat][lang].list = {};
3397 for (name in MIM.imlist[lang])
3398 list[name] = MIM.imlist[lang][name];
3401 for (name in MIM.imlist[lang])
3402 Xex.Log ('no category ' + lang + '-' + name);
3411 clearTimeout (destroy_timer);
3412 destroy_timer = null;
3413 var target = document.getElementById ('mim-menu');
3416 for (; last_target && last_target.menu_level;
3417 last_target = last_target.parentLi)
3418 last_target.style.backgroundColor = 'white';
3419 var nodes = target.getElementsByTagName ('ul');
3420 for (var i = 0; i < nodes.length; i++)
3421 nodes[i].style.visibility = 'hidden';
3422 document.getElementsByTagName ('body')[0].removeChild (target);
3426 function destroy_menu () {
3427 if (! destroy_timer)
3428 destroy_timer = setTimeout (destroy, 1000);
3431 function show_submenu (event)
3435 clearTimeout (destroy_timer);
3436 destroy_timer = null;
3438 var target = event.target;
3439 if (! target.menu_level)
3441 if (last_target && target.parentLi != last_target)
3443 last_target.style.backgroundColor = 'white';
3444 if (target.menu_level < last_target.menu_level)
3446 last_target = last_target.parentLi;
3447 last_target.style.backgroundColor = 'white';
3449 var uls = last_target.getElementsByTagName ('ul');
3450 for (var i = 0; i < uls.length; i++)
3451 uls[i].style.visibility = 'hidden';
3453 last_target = target;
3454 target.style.backgroundColor = 'yellow';
3455 if (target.menu_level < 3)
3457 target.lastChild.style.visibility = 'visible';
3458 target.lastChild.style.left = target.clientWidth + 'px';
3460 event.preventDefault ();
3463 function select_im (event)
3465 var target = event.target;
3468 MIM.current = target.im;
3471 event.preventDefault ();
3474 function create_ul (visibility)
3476 var ul = document.createElement ('ul');
3477 ul.style.position = 'absolute';
3478 ul.style.margin = '0px';
3479 ul.style.padding = '0px';
3480 ul.style.border = '1px solid gray';
3481 ul.style.borderBottom = 'none';
3482 ul.style.top = '-1px';
3483 ul.style.backgroundColor = 'white';
3484 ul.style.visibility = visibility;
3488 function create_li (level, text)
3490 var li = document.createElement ('li');
3491 li.style.position = 'relative';
3492 li.style.margin = '0px';
3493 li.style.padding = '1px';
3494 li.style.borderBottom = '1px solid gray';
3495 li.style.top = '0px';
3496 li.style.listStyle = 'none';
3497 li.menu_level = level;
3498 var nobr = document.createElement ('nobr');
3499 nobr.innerHTML = text;
3500 li.appendChild (nobr);
3506 function create_menu (event)
3508 var target = event.target;
3510 if (! ((target.type == "text" || target.type == "textarea")
3511 && event.which == 1 && event.ctrlKey))
3516 menu = create_ul ('visible');
3517 menu.style.fontFamily = 'sans-serif';
3518 menu.style.fontWeight = 'bold';
3519 menu.id = 'mim-menu';
3520 menu.onclick = select_im;
3521 menu.onmouseover = show_submenu;
3522 menu.onmouseout = destroy_menu;
3523 for (var catname in lang_category)
3525 var cat = lang_category[catname];
3526 var li = create_li (1, catname);
3527 var sub = create_ul ('hidden');
3528 for (var langname in cat)
3530 var lang = cat[langname];
3533 var sub_li = create_li (2, lang.name);
3534 sub_li.parentLi = li;
3535 var subsub = create_ul ('hidden');
3536 for (var name in lang.list)
3538 var im = lang.list[name];
3539 var subsub_li = create_li (3, im.name);
3540 subsub_li.parentLi = sub_li;
3542 subsub.appendChild (subsub_li);
3544 sub_li.appendChild (subsub);
3545 sub.appendChild (sub_li);
3547 li.appendChild (sub);
3548 menu.appendChild (li);
3550 lang_category = null;
3552 menu.style.left = (event.clientX - 10) + "px";
3553 menu.style.top = (event.clientY - 10) + "px";
3554 document.getElementsByTagName ('body')[0].appendChild (menu);
3557 MIM.init = function ()
3559 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3560 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3561 MIM.add_event_listener (window, 'mousedown', create_menu);
3562 if (window.location == 'http://localhost/mim/index.html')
3563 MIM.server = 'http://localhost/mim';
3564 MIM.current = MIM.imlist['zh']['tonepy'];
3568 MIM.test = function ()
3570 var im = MIM.imlist['t']['latn-post'];
3571 var ic = new MIM.IC (im, null);
3573 ic.Filter (new MIM.Key ('a'));
3574 ic.Filter (new MIM.Key ("'"));
3577 document.getElementById ('text').value = ic.produced + ic.preedit;
3580 document.getElementById ('text').value
3581 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3583 if (e instanceof Xex.ErrTerm)
3591 MIM.init_debug = function ()
3594 Xex.LogNode = document.getElementById ('log');