1 // -* coding: utf-8; -*
5 Log: function (arg, indent)
10 if (indent != undefined)
11 for (var i = 0; i <= indent; i++)
13 Xex.LogNode.value = str + arg + "\n" + Xex.LogNode.value;
18 UnknownError: "unknown-error",
19 WrongArgument: "wrong-argument",
21 InvalidInteger: "invalid-integer",
22 TermTypeInvalid: "term-type-invalid",
23 FunctionConflict: "function-conflict",
24 VariableTypeConflict: "variable-type-conflict",
25 VariableRangeConflict: "variable-range-conflict",
26 VariableWrongRange: "variable-wrong-range",
27 VariableWrongValue: "variable-wrong-value",
29 UnknownFunction: "unknown-function",
30 MacroExpansionError: "macro-expansion-error",
31 NoVariableName: "no-variable-name",
32 NoFunctionName: "no-funcion-name",
35 ArithmeticError: "arithmetic-error",
36 WrongType: "wrong-type",
37 IndexOutOfRange: "index-out-of-range",
38 ValueOutOfRange: "value-out-of-range",
39 NoLoopToBreak: "no-loop-to-break",
40 UncaughtThrow: "uncaught-throw"
43 Xex.Variable = function (domain, name, desc, val, range)
52 Xex.Variable.prototype.clone = function ()
54 return new Xex.Variable (this.domain, this.name, this.desc,
55 this.val, this.range);
58 Xex.Variable.prototype.Equals = function (obj)
60 return ((obj instanceof Xex.Variable)
61 && obj.name == this.name);
64 Xex.Variable.prototype.SetValue = function (term)
70 Xex.Function = function (name, with_var, min_args, max_args)
73 this.with_var = with_var;
74 this.min_args = min_args;
75 this.max_args = max_args;
78 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
81 this.with_var = with_var;
82 this.min_args = min_args;
83 this.max_args = max_args;
84 this.builtin = builtin;
87 Xex.Subrountine.prototype.Call = function (domain, vari, args)
89 var newargs = new Array ();
90 for (var i = 0; i < args.length; i++)
92 newargs[i] = args[i].Eval (domain);
96 return this.builtin (domain, vari, newargs)
99 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
102 this.with_var = with_var;
103 this.min_args = min_args;
104 this.max_args = max_args;
105 this.builtin = builtin;
108 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
110 return this.builtin (domain, vari, args)
113 Xex.Lambda = function (name, min_args, max_args, args, body)
116 this.min_args = min_args;
117 this.max_args = max_args;
122 Xex.Lambda.prototype.Call = function (domain, vari, args)
124 var current = domain.bindings;
125 var result = Xex.Zero;
126 var limit = max_args >= 0 ? args.length : args.length - 1;
130 for (i = 0; i < limit; i++)
132 result = args[i].Eval (domain);
133 if (domain.Thrown ())
135 domain.Bind (this.args[i], result);
139 var list = new Array ();
140 for (i = 0; i < args[limit].length; i++)
142 result = args[limit].Eval (domain);
143 if (domain.Thrown ())
147 domain.Bind (this.args[limit], list);
150 domain.Catch (Xex.CatchTag.Return);
151 for (var term in this.body)
153 result = term.Eval (domain);
154 if (domain.Thrown ())
161 domain.UnboundTo (current);
166 Xex.Macro = function (name, min_args, max_args, args, body)
169 this.min_args = min_args;
170 this.max_args = max_args;
175 Xex.Macro.prototype.Call = function (domain, vari, args)
177 var current = domain.bindings;
178 var result = Xex.Zero;
182 for (i = 0; i < args.length; i++)
183 domain.Bind (this.args[i], args[i]);
185 domain.Catch (Xex.CatchTag.Return);
186 for (var i in this.body)
188 result = this.body[i].Eval (domain);
189 if (domain.Thrown ())
196 domain.UnboundTo (current);
201 Xex.Bindings = function (vari)
204 this.old_value = vari.val;
207 Xex.Bindings.prototype.UnboundTo = function (boundary)
209 for (var b = this; b != boundary; b = b.next)
210 b.vari.val = b.old_value;
214 Xex.Bind = function (bindings, vari, val)
216 var b = new Xex.Bindings (vari);
227 Xex.Domain = function (name, parent, context)
230 this.context = context;
233 if (name != 'basic' && ! parent)
234 parent = Xex.BasicDomain
235 this.parent = parent;
242 for (elt in parent.termtypes)
243 this.termtypes[elt] = parent.termtypes[elt];
244 for (elt in parent.functions)
245 this.functions[elt] = parent.functions[elt];
246 for (elt in parent.variables)
247 this.variables[elt] = parent.variables[elt];
250 this.call_stack = new Array ();
251 this.bindings = null;
252 this.catch_stack = new Array ();
253 this.catch_count = 0;
257 Xex.Domain.prototype = {
258 CallStackCount: function () { return this.call_stack.length; },
259 CallStackPush: function (term) { this.call_stack.push (term); },
260 CallStackPop: function () { this.call_stack.pop (); },
261 Bind: function (vari, val)
263 this.bindings = Xex.Bind (this.bindings, vari, val);
265 UnboundTo: function (boundary)
268 this.bindings = this.bindings.UnboundTo (boundary);
270 Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
273 this.catch_stack.pop ();
274 if (this.catch_count > this.catch_stack.length)
279 if (this.catch_count < this.catch_stack.length)
281 this.caught = (this.catch_count == this.catch_stack.length - 1);
287 ThrowReturn: function ()
289 for (var i = this.catch_stack.length - 1; i >= 0; i--)
292 if (this.catch_stack[i] == Xex.CatchTag.Return)
296 ThrowBreak: function ()
298 if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
299 throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
300 "No surrounding loop to break");
303 ThrowSymbol: function (tag)
305 var i = this.catch_count;
306 for (var j = this.catch_stack.length - 1; j >= 0; j--)
309 if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
311 this.catch_count = i;
315 throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
316 "No corresponding catch: " + tag);
318 DefType: function (obj)
321 if (this.termtypes[type])
322 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
323 "Already defined: " + type);
324 if (this.functions[type])
325 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
326 "Already defined as a funciton or a macro: "
328 this.termtypes[type] = obj.Parser;
330 DefSubr: function (builtin, name, with_var, min_args, max_args)
332 this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
335 DefSpecial: function (builtin, name, with_var, min_args, max_args)
337 this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
340 Defun: function (name, min_args, max_args, args, body)
342 this.functions[name] = new Xex.Lambda (name, min_args, max_args,
345 DefunByFunc: function (func) { this.functions[func.name] = func; },
346 Defmacro: function (name, min_args, max_args, args, body)
348 this.functions[name] = new Xex.Macro (name, min_args, max_args,
351 DefAlias: function (alias, fname)
353 var func = this.functions[fname];
356 throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
357 if (this.termtypes[alias])
358 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
359 "Already defined as a term type: " + alias);
360 if (this.functions[alias])
361 throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
362 "Already defined as a function: " + alias);
363 this.functions[alias] = func;
365 Defvar: function (name, desc, val, range)
367 var vari = new Xex.Variable (this, name, desc, val, range);
368 this.variables[name] = vari;
371 GetFunc: function (name)
373 var func = this.functions[name];
375 throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
376 "Unknown function: " + name);
379 CopyFunc: function (domain, name)
381 var func = this.functions[name];
382 domain.DefunByFunc (func);
385 CopyFuncAll: function (domain)
387 for (var elt in this.functions)
388 domain.DefunByFunc (this.functions[elt]);
390 GetVarCreate: function (name)
392 var vari = this.variables[name];
394 vari = this.variables[name] = new Xex.Variable (this, name, null,
398 GetVar: function (name) { return this.variables[name]; },
399 SaveValues: function ()
402 for (var elt in this.variables)
403 values[elt] = this.variables[elt].val.Clone ();
406 RestoreValues: function (values)
411 var vari = this.variables[name];
412 vari.val = values[name];
417 Xex.Term = function (type) { this.type = type; }
418 Xex.Term.prototype = {
419 IsTrue: function () { return true; },
420 Eval: function (domain) { return this.Clone (); },
421 Clone: function (domain) { return this; },
422 Equals: function (obj)
424 return (this.type == obj.type
426 && obj.val == this.val);
428 Matches: function (obj) { return this.Equals (obj); },
429 toString: function ()
431 if (this.val != undefined)
432 return '<' + this.type + '>' + this.val + '</' + this.type + '>';
433 return '<' + this.type + '/>';
435 Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
436 "Not an integer"); },
437 Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
441 Node.prototype.firstElement = function ()
443 for (var n = this.firstChild; n; n = n.nextSibling)
449 Node.prototype.nextElement = function ()
451 for (var n = this.nextSibling; n; n = n.nextSibling)
458 function parse_defvar (domain, node)
460 var name = node.attributes['vname'].nodeValue;
462 throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
463 var vari = domain.variables[name];
464 var desc, val, range;
467 desc = vari.description;
471 node = node.firstElement ();
472 if (node && node.nodeName == 'description')
474 desc = node.firstChild.nodeValue;
475 node = node.nextElement ();
479 val = Xex.Term.Parse (domain, node);
480 node = node.nextElement ();
481 if (node && node.nodeName == 'possible-values')
482 for (node = node.firstElement (); node; node = node.nextElement ())
485 if (node.nodeName == 'range')
488 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
489 'Range not allowed for ' + name);
491 for (var n = node.firstElement (); n; n = n.nextElement ())
493 var v = Xex.Term.Parse (domain, n);
495 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
496 'Invalid range value: ' + val);
502 pval = Xex.Term.Parse (domain, node);
503 if (val.type != pval.type)
504 throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
505 'Invalid possible value: ' + pval);
508 range = new Array ();
514 domain.Defvar (name, desc, val, range);
518 function parse_defun_head (domain, node)
520 var name = node.attributes['fname'].nodeValue;
522 throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
523 var args = new Array ();
524 var nfixed = 0, noptional = 0, nrest = 0;
526 node = node.firstElement ();
527 if (node && node.nodeName == 'args')
530 for (n = n.firstElement (); n; n = n.nextElement ())
532 if (n.nodeName == 'fixed')
534 else if (n.nodeName == 'optional')
536 else if (n.nodeName == 'rest')
539 throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
542 throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
543 for (n = node.firstElement (); n; n = n.nextElement ())
544 args.push (domain.DefVar (n.attributes['vname'].nodeValue));
546 args.min_args = nfixed;
547 args.max_args = nrest == 0 ? nfixed + noptional : -1;
549 if (node.nodeName == 'defun')
550 domain.Defun (name, args, null);
552 domain.Defmacro (name, args, null);
556 function parse_defun_body (domain, node)
558 var name = node.attributes['fname'].nodeValue;
559 var func = domain.GetFunc (name);
561 for (node = node.firstElement (); node; node = node.nextElement ())
562 if (node.nodeName != 'description' && node.nodeName != 'args')
564 body = Xex.Term.Parse (domain, node, null);
568 Xex.Term.Parse = function (domain, node, stop)
570 if (arguments.length == 2)
572 var name = node.nodeName;
573 var parser = domain.termtypes[name];
576 return parser (domain, node);
577 if (name == 'defun' || name == 'defmacro')
579 name = parse_defun_head (domain, node);
580 parse_defun_body (domain, node);
581 return new Xex.StrTerm (name);
583 if (name == 'defvar')
585 name = parse_defvar (domain, node);
586 return new Xex.StrTerm (name);
588 return new Xex.Funcall.prototype.Parser (domain, node);
590 for (var n = node; n && n != stop; n = n.nextElement ())
591 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
592 parse_defun_head (domain, n);
594 for (var n = node; n && n != stop; n = n.nextElement ())
596 if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
597 parse_defun_body (domain, n);
598 else if (n.nodeName == 'defvar')
599 parse_defvar (domain, n);
603 terms = new Array ();
604 terms.push (Xex.Term.Parse (domain, n));
611 Xex.Varref = function (vname)
617 var proto = new Xex.Term ('varref');
619 proto.Clone = function () { return new Xex.Varref (this.val); }
620 proto.Eval = function (domain)
622 if (! this.vari || this.vari.domain != domain)
623 this.vari = domain.GetVarCreate (this.val);
624 Xex.Log (this.ToString () + '=>' + this.vari.val, domain.depth);
625 return this.vari.val;
628 proto.Parser = function (domain, node)
630 return new Xex.Varref (node.attributes['vname'].nodeValue);
633 proto.ToString = function ()
635 return '<varref vname="' + this.val + '"/>';
638 Xex.Varref.prototype = proto;
641 var null_args = new Array ();
643 Xex.Funcall = function (func, vari, args)
647 this.args = args || null_args;
651 var proto = new Xex.Term ('funcall');
653 proto.Parser = function (domain, node)
655 var fname = node.nodeName;
658 if (fname == 'funcall')
659 fname = node.attributes['fname'].nodeValue;
660 var func = domain.GetFunc (fname);
662 attr = node.attributes['vname'];
663 vari = attr != undefined ? domain.GetVarCreate (attr.nodeValue) : false;
664 var args = Xex.Term.Parse (domain, node.firstElement (), null);
665 return new Xex.Funcall (func, vari, args);
668 proto.New = function (domain, fname, vname, args)
670 var func = domain.GetFunc (fname);
671 var vari = vname ? domain.GetVarCreate (vname) : null;
672 var funcall = new Xex.Funcall (func, vari, args);
673 if (func instanceof Xex.Macro)
674 funcall = funcall.Eval (domain);
678 proto.Eval = function (domain)
680 if (! (this.func instanceof Xex.Subrountine))
681 Xex.Log (this, domain.depth);
685 result = this.func.Call (domain, this.vari, this.args);
687 Xex.Log (this + ' => ' + result, --domain.depth);
692 proto.Clone = function ()
694 return new Xex.Funcall (this.func, this.vari, this.args);
697 proto.Equals = function (obj)
699 return (obj.type == 'funcall'
700 && obj.func == this.func
701 && obj.vari.Equals (this.vari)
702 && obj.args.length == this.func.length);
705 proto.toString = function ()
708 var len = this.args.length;
709 var str = '<' + this.func.name;
711 str += ' vname="' + this.vari.name + '"';
714 if (this.func instanceof Xex.Subrountine)
715 for (var i = 0; i < len; i++)
716 arglist += this.args[i].toString ();
718 for (var i = 0; i < len; i++)
720 return str + '>' + arglist + '</' + this.func.name + '>';
723 Xex.Funcall.prototype = proto;
726 Xex.ErrTerm = function (ename, message, stack)
729 this.message = message;
734 var proto = new Xex.Term ('error');
736 proto.IsError = true;
738 proto.Parser = function (domain, node)
740 return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
741 node.innerText, false);
744 proto.CallStack = function () { return stack; }
746 proto.SetCallStack = function (value) { statck = value; }
748 proto.Clone = function ()
750 return new Xex.ErrTerm (ename, message, false);
753 proto.Equals = function (obj)
756 && obj.ename == ename && obj.message == message
757 && (obj.stack ? (stack && stack.length == obj.stack.length)
761 proto.Matches = function (obj)
763 return (obj.IsError && obj.ename == ename);
766 proto.toString = function ()
768 return '<error ename="' + this.ename + '">' + this.message + '</error>';
771 Xex.ErrTerm.prototype = proto;
774 Xex.IntTerm = function (num) { this.val = num; };
776 var proto = new Xex.Term ('integer');
778 proto.Intval = function () { return this.val; };
779 proto.IsTrue = function () { return this.val != 0; }
780 proto.Clone = function () { return new Xex.IntTerm (this.val); }
781 proto.Parser = function (domain, node)
783 var str = node.firstChild.nodeValue;
785 if (str.charAt (0) == '?' && str.length == 2)
786 return new Xex.IntTerm (str.charCodeAt (1));
787 return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
789 Xex.IntTerm.prototype = proto;
792 Xex.StrTerm = function (str) { this.val = str; };
794 var proto = new Xex.Term ('string');
796 proto.Strval = function () { return this.val; };
797 proto.IsTrue = function () { return this.val.length > 0; }
798 proto.Clone = function () { return new Xex.StrTerm (this.val); }
799 proto.Parser = function (domain, node)
801 return new Xex.StrTerm (node.firstChild.nodeValue);
803 Xex.StrTerm.prototype = proto;
806 Xex.SymTerm = function (str) { this.val = str; };
808 var proto = new Xex.Term ('symbol');
809 proto.IsSymbol = true;
810 proto.IsTrue = function () { return this.val != 'nil'; }
811 proto.Clone = function () { return new Xex.SymTerm (this.val); }
812 proto.Parser = function (domain, node)
814 return new Xex.SymTerm (node.firstChild.nodeValue);
816 Xex.SymTerm.prototype = proto;
819 Xex.LstTerm = function (list) { this.val = list; };
821 var proto = new Xex.Term ('list');
823 proto.IsTrue = function () { return this.val.length > 0; }
824 proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
826 proto.Equals = function (obj)
828 if (obj.type != 'list' || obj.val.length != this.val.length)
830 var i, len = this.val.length;
831 for (i = 0; i < len; i++)
832 if (! this.val[i].Equals (obj.val[i]))
837 proto.Parser = function (domain, node)
839 var list = Xex.Term.Parse (domain, node.firstElement (), null);
840 return new Xex.LstTerm (list);
843 proto.toString = function ()
845 var len = this.val.length;
850 for (var i = 0; i < len; i++)
851 str += this.val[i].toString ();
852 return str + '</list>';
854 Xex.LstTerm.prototype = proto;
858 var basic = new Xex.Domain ('basic', null, null);
860 function Fset (domain, vari, args)
863 throw new Xex.ErrTerm (Xex.Error.NoVariableName,
864 'No variable name to set');
865 vari.SetValue (args[0]);
869 function maybe_set_intvar (vari, n)
871 var term = new Xex.IntTerm (n);
873 vari.SetValue (term);
877 function Fadd (domain, vari, args)
879 var n = vari ? vari.val.Intval () : 0;
880 var len = args.length;
882 for (var i = 0; i < len; i++)
883 n += args[i].Intval ();
884 return maybe_set_intvar (vari, n);
887 function Fmul (domain, vari, args)
889 var n = vari ? vari.val.Intval () : 1;
890 for (var i = 0; i < args.length; i++)
892 return maybe_set_intvar (vari, n);
895 function Fsub (domain, vari, args)
901 n = args[0].Intval ();
906 n = vari.val.Intval ();
909 while (i < args.length)
910 n -= args[i++].Intval ();
911 return maybe_set_intvar (vari, n);
914 function Fdiv (domain, vari, args)
920 n = args[0].Intval ();
925 n = vari.val.Intval ();
928 while (i < args.length)
929 n /= args[i++].Intval ();
930 return maybe_set_intvar (vari, n);
933 function Fmod (domain, vari, args)
935 return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
938 function Flogior (domain, vari, args)
940 var n = vari == null ? 0 : vari.val;
941 for (var i = 0; i < args.length; i++)
943 return maybe_set_intvar (vari, n);
946 function Fand (domain, vari, args)
948 var len = args.length;
949 for (var i = 0; i < len; i++)
951 var result = args[i].Eval (domain);
952 if (domain.Thrown ())
954 if (! result.IsTrue ())
960 function For (domain, vari, args)
962 var len = args.length;
963 for (var i = 0; i < len; i++)
965 var result = args[i].Eval (domain);
966 if (domain.Thrown ())
968 if (result.IsTrue ())
974 function Feq (domain, vari, args)
976 for (var i = 1; i < args.length; i++)
977 if (! args[i - 1].Equals (args[i]))
982 function Flt (domain, vari, args)
984 var n = args[0].Intval;
986 for (var i = 1; i < args.length; i++)
988 var n1 = args[i].Intval;
996 function Fle (domain, vari, args)
998 var n = args[0].Intval;
999 for (var i = 1; i < args.length; i++)
1001 var n1 = args[i].Intval;
1009 function Fgt (domain, vari, args)
1011 var n = args[0].Intval;
1012 for (var i = 1; i < args.length; i++)
1014 var n1 = args[i].Intval;
1022 function Fge (domain, vari, args)
1024 var n = args[0].Intval;
1025 for (var i = 1; i < args.Length; i++)
1027 var n1 = args[i].Intval;
1035 function Fprogn (domain, vari, args)
1037 var result = Xex.One;
1038 var len = args.length;
1040 for (var i = 0; i < len; i++)
1042 result = args[i].Eval (domain);
1043 if (domain.Thrown ())
1049 function Fif (domain, vari, args)
1051 var result = args[0].Eval (domain);
1053 if (domain.Thrown ())
1055 if (result.IsTrue ())
1056 return args[1].Eval (domain);
1057 if (args.length == 2)
1059 return args[2].Eval (domain);
1062 function Fcond (domain, vari, args)
1064 for (var i = 0; i < args.length; i++)
1067 var result = list.val[0].Eval (domain);
1068 if (result.IsTrue ())
1070 for (var j = 1; j < list.val.length; j++)
1073 result = list.val[j].Eval (domain);
1075 if (domain.Thrown ())
1084 function eval_terms (domain, terms, idx)
1086 var result = Xex.Zero;
1087 domain.caught = false;
1088 for (var i = idx; i < terms.length; i++)
1090 result = terms[i].Eval (domain);
1091 if (domain.Thrown ())
1097 function Fcatch (domain, vari, args)
1102 if (args[0].IsError)
1105 result = eval_terms (domain, args, 1);
1107 if (e instanceof Xex.ErrTerm)
1109 if (! args[0].Matches (e))
1117 else if (args[0].IsSymbol)
1120 domain.Catch (args[0].val);
1121 result = eval_terms (domain, args, 1);
1125 vari.SetValue (result);
1133 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1134 "Not a symbol nor an error: " + args[0]);
1137 function Fthrow (domain, vari, args)
1139 if (args[0].IsSymbl)
1141 domain.ThrowSymbol (args[0]);
1142 return (args[args.length - 1]);
1144 if (args[0].IsError)
1148 throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1149 "Not a symbol nor an error:" + args[0]);
1152 Xex.BasicDomain = basic;
1154 basic.DefSubr (Fset, "set", true, 1, 1);
1155 basic.DefAlias ("=", "set");
1156 //basic.DefSubr (Fnot, "not", false, 1, 1);
1157 //basic.DefAlias ("!", "not");
1158 basic.DefSubr (Fadd, "add", true, 1, -1);
1159 basic.DefSubr (Fmul, "mul", true, 1, -1);
1160 basic.DefAlias ("*", "mul");
1161 basic.DefSubr (Fsub, "sub", true, 1, -1);
1162 basic.DefAlias ("-", "sub");
1163 basic.DefSubr (Fdiv, "div", true, 1, -1);
1164 basic.DefAlias ("/", "div");
1165 basic.DefSubr (Fmod, "mod", true, 1, 2);
1166 basic.DefAlias ("%", "mod");
1167 basic.DefSubr (Flogior, "logior", true, 1, -1);
1168 basic.DefAlias ('|', "logior");
1169 //basic.DefSubr (Flogand, "logand", true, 1, -1);
1170 //basic.DefAlias ("&", "logand");
1171 //basic.DefSubr (Flsh, "lsh", true, 1, 2);
1172 //basic.DefAlias ("<<", "lsh");
1173 //basic.DefSubr (Frsh, "rsh", true, 1, 2);
1174 //basic.DefAlias (">>", "rsh");
1175 basic.DefSubr (Feq, "eq", false, 2, -1);
1176 basic.DefAlias ("==", "eq");
1177 //basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1178 //basic.DefAlias ("!=", "noteq");
1179 basic.DefSubr (Flt, "lt", false, 2, -1);
1180 basic.DefAlias ("<", "lt");
1181 basic.DefSubr (Fle, "le", false, 2, -1);
1182 basic.DefAlias ("<=", "le");
1183 basic.DefSubr (Fgt, "gt", false, 2, -1);
1184 basic.DefAlias (">", "gt");
1185 basic.DefSubr (Fge, "ge", false, 2, -1);
1186 basic.DefAlias (">=", "ge");
1187 basic.DefSubr (Fthrow, "throw", false, 1, 2);
1189 //basic.DefSubr (Fappend, "append", true, 0, -1);
1190 //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1191 //basic.DefSubr (Fnth, "nth", false, 2, 2);
1192 //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1193 //basic.DefSubr (Fins, "ins", true, 2, 2);
1194 //basic.DefSubr (Fdel, "del", true, 2, 2);
1195 //basic.DefSubr (Feval, "eval", false, 1, 1);
1196 //basic.DefSubr (Fbreak, "break", false, 0, 1);
1197 //basic.DefSubr (Freturn, "return", false, 0, 1);
1198 //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1200 basic.DefSpecial (Fand, "and", false, 1, -1);
1201 basic.DefAlias ("&&", "and");
1202 basic.DefSpecial (For, "or", false, 1, -1);
1203 basic.DefAlias ("||", "or");
1204 basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1205 basic.DefAlias ("expr", "progn");
1206 basic.DefSpecial (Fif, "if", false, 2, 3);
1207 //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1208 //basic.DefSpecial (Floop, "loop", false, 1, -1);
1209 //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1210 basic.DefSpecial (Fcond, "cond", false, 1, -1);
1211 //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1212 //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1213 //basic.DefSpecial (Ftype, "type", false, 1, 1);
1214 basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1216 basic.DefType (Xex.Funcall.prototype);
1217 basic.DefType (Xex.Varref.prototype);
1218 basic.DefType (Xex.ErrTerm.prototype);
1219 basic.DefType (Xex.IntTerm.prototype);
1220 basic.DefType (Xex.StrTerm.prototype);
1221 basic.DefType (Xex.SymTerm.prototype);
1222 basic.DefType (Xex.LstTerm.prototype);
1226 Xex.Zero = new Xex.IntTerm (0);
1227 Xex.One = new Xex.IntTerm (1);
1228 Xex.nil = new Xex.SymTerm ('nil');
1230 Xex.Load = function (server, file)
1232 var obj = new XMLHttpRequest ();
1233 var url = server ? server + '/' + file : file;
1234 obj.open ('GET', url, false);
1235 obj.overrideMimeType ('text/xml');
1237 return obj.responseXML.firstChild;
1241 // URL of the input method server.
1242 server: "http://www.m17n.org/common/mim-js",
1243 // Boolean flag to tell if MIM is active or not.
1245 // Boolean flag to tell if MIM is running in debug mode or not.
1247 // List of main input methods.
1249 // List of extra input methods;
1251 // Global input method data
1253 // Currently selected input method.
1257 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1264 CandidateIndex:0x10,
1266 Preedit: 0x06, // PreeditText | CursorPos
1267 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
1289 ParseError: "parse-error"
1294 var keysyms = new Array ();
1295 keysyms["bs"] = "backspace";
1296 keysyms["lf"] = "linefeed";
1297 keysyms["cr"] = keysyms["enter"] = "return";
1298 keysyms["esc"] = "escape";
1299 keysyms["spc"] = "space";
1300 keysyms["del"] = "delete";
1302 function decode_keysym (str) {
1303 var parts = str.split ("-");
1304 var len = parts.length, i;
1305 var has_modifier = len > 1;
1307 for (i = 0; i < len - 1; i++)
1308 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1310 var key = parts[len - 1];
1313 key = keysyms[key.toLowerCase ()];
1319 for (i = 1; i < len - 1; i++)
1320 str += '-' + parts[i];
1329 parts = new Array ();
1336 MIM.Key = function (val)
1339 this.has_modifier = false;
1340 if (typeof val == 'string' || val instanceof String)
1342 this.key = decode_keysym (val);
1344 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1345 if (this.key instanceof Array)
1347 this.key = this.key[0];
1348 this.has_modifier = true;
1351 else if (typeof val == 'number' || val instanceof Number)
1352 this.key = String.fromCharCode (val);
1354 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1357 MIM.Key.prototype.toString = function () { return this.key; };
1361 MIM.KeySeq = function (seq)
1363 this.val = new Array ();
1364 this.has_modifier = false;
1370 var len = seq.val.length;
1371 for (var i = 0; i < len; i++)
1374 if (v.type != 'string' && v.type != 'integer'
1375 && v.type != 'symbol')
1376 throw new Xex.ErrTerm (MIM.Error.ParseError,
1377 "Invalid key: " + v);
1378 var key = new MIM.Key (v.val);
1379 this.val.push (key);
1380 if (key.has_modifier)
1381 this.has_modifier = true;
1386 var len = seq.val.length;
1387 for (var i = 0; i < len; i++)
1388 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1391 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1395 var proto = new Xex.Term ('keyseq');
1396 proto.Clone = function () { return this; }
1397 proto.Parser = function (domain, node)
1399 var seq = new Array ();
1400 for (node = node.firstChild; node; node = node.nextSibling)
1401 if (node.nodeType == 1)
1403 var term = Xex.Term.Parse (domain, node);
1404 return new MIM.KeySeq (term);
1406 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1408 proto.toString = function ()
1410 var len = this.val.length;
1414 var str = '<keyseq>';
1415 for (var i = 0; i < len; i++)
1419 else if (this.has_modifier)
1421 str += this.val[i].toString ();
1423 return str + '</keyseq>';
1426 MIM.KeySeq.prototype = proto;
1430 MIM.Marker = function () { }
1431 MIM.Marker.prototype = new Xex.Term ('marker');
1432 MIM.Marker.prototype.CharAt = function (ic)
1434 var p = this.Position (ic);
1435 if (p < 0 || p >= ic.preedit.length)
1437 return ic.preedit.charCodeAt (p);
1440 MIM.FloatingMarker = function (name) { this.val = name; };
1441 var proto = new MIM.Marker ();
1442 MIM.FloatingMarker.prototype = proto;
1443 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1444 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1446 MIM.PredefinedMarker = function (name) { this.val = name; }
1447 MIM.PredefinedMarker.prototype = new MIM.Marker ();
1448 MIM.PredefinedMarker.prototype.Position = function (ic)
1450 if (typeof this.pos == 'number')
1452 return this.pos (ic);
1455 var predefined = { }
1457 function def_predefined (name, position)
1459 predefined[name] = new MIM.PredefinedMarker (name);
1460 predefined[name].pos = position;
1463 def_predefined ('@<', 0);
1464 def_predefined ('@>', function (ic) { return ic.preedit.length; });
1465 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1466 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1467 def_predefined ('@[', function (ic) {
1468 if (ic.cursor_pos > 0)
1470 var pos = ic.cursor_pos;
1471 return ic.preedit.FindProp ('candidates', pos - 1).from;
1475 def_predefined ('@]', function (ic) {
1476 if (ic.cursor_pos < ic.preedit.length - 1)
1478 var pos = ic.cursor_pos;
1479 return ic.preedit.FindProp ('candidates', pos).to;
1481 return ic.preedit.length;
1483 for (var i = 0; i < 10; i++)
1484 def_predefined ("@" + i, i);
1485 predefined['@first'] = predefined['@<'];
1486 predefined['@last'] = predefined['@>'];
1487 predefined['@previous'] = predefined['@-'];
1488 predefined['@next'] = predefined['@+'];
1489 predefined['@previous-candidate-change'] = predefined['@['];
1490 predefined['@next-candidate-change'] = predefined['@]'];
1492 MIM.SurroundMarker = function (name)
1495 this.distance = parseInt (name.slice (1));
1496 if (isNaN (this.distance))
1497 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1499 MIM.SurroundMarker.prototype = new MIM.Marker ();
1500 MIM.SurroundMarker.prototype.Position = function (ic)
1502 return ic.cursor_pos + this.distance;
1504 MIM.SurroundMarker.prototype.CharAt = function (ic)
1506 if (this.val == '@-0')
1508 var p = this.Position (ic);
1510 return ic.GetSurroundingChar (p);
1511 else if (p >= ic.preedit.length)
1512 return ic.GetSurroundingChar (p - ic.preedit.length);
1513 return ic.preedit.charCodeAt (p);
1516 MIM.Marker.prototype.Parser = function (domain, node)
1518 var name = node.firstChild.nodeValue;
1519 if (name.charAt (0) == '@')
1521 var n = predefined[name];
1524 if (name.charAt (1) == '-')
1525 return new MIM.SurroundMarker (name);
1526 throw new Xex.ErrTerm (MIM.Error.ParseError,
1527 "Invalid marker: " + name);
1529 return new MIM.FloatingMarker (name);;
1533 MIM.Selector = function (name)
1537 MIM.Selector.prototype = new Xex.Term ('selector');
1541 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1542 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1543 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1544 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1545 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1546 selectors["@["] = selectors["@previous-candidate-change"]
1547 = new MIM.Selector ('@[');
1548 selectors["@]"] = selectors["@next-candidate-change"]
1549 = new MIM.Selector ('@]');
1551 MIM.Selector.prototype.Parser = function (domain, node)
1553 var name = node.firstChild.nodeValue;
1554 var s = selectors[name];
1556 throw new Xex.ErrTerm (MIM.Error.ParseError,
1557 "Invalid selector: " + name);
1562 MIM.Rule = function (keyseq, actions)
1564 this.keyseq = keyseq;
1565 this.actions = actions;
1567 MIM.Rule.prototype = new Xex.Term ('rule');
1568 MIM.Rule.prototype.Parser = function (domain, node)
1571 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1573 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1574 var keyseq = Xex.Term.Parse (domain, n);
1575 if (keyseq.type != 'keyseq')
1576 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1577 var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1578 return new MIM.Rule (keyseq, actions);
1580 MIM.Rule.prototype.toString = function ()
1585 MIM.Map = function (name)
1588 this.rules = new Array ();
1592 var proto = new Xex.Term ('map');
1594 proto.Parser = function (domain, node)
1596 var name = node.attributes['mname'].nodeValue;
1598 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1599 var map = new MIM.Map (name);
1600 for (var n = node.firstChild; n; n = n.nextSibling)
1601 if (n.nodeType == 1)
1602 map.rules.push (Xex.Term.Parse (domain, n));
1606 proto.toString = function ()
1608 var str = '<map mname="' + this.name + '">';
1609 var len = this.rules.length;
1610 for (i = 0; i < len; i++)
1611 str += this.rules[i];
1612 return str + '</map>';
1615 MIM.Map.prototype = proto;
1618 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1620 MIM.Action = function (domain, terms)
1622 var args = new Array ();
1623 args.push (Xex.CatchTag_.mimtag);
1624 for (var i = 0; i < terms.length; i++)
1625 args.push (terms[i]);
1626 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1629 MIM.Action.prototype.Run = function (domain)
1631 var result = this.action.Eval (domain);
1632 if (result.type == 'error')
1634 domain.context.Error = result.toString ();
1637 return (result != Xex.CatchTag._mimtag);
1640 MIM.Keymap = function ()
1643 this.submaps = null;
1649 function add_rule (keymap, rule, branch_actions)
1651 var keyseq = rule.keyseq;
1652 var len = keyseq.val.length;
1655 for (var i = 0; i < len; i++)
1657 var key = keyseq.val[i];
1661 if (! keymap.submaps)
1662 keymap.submaps = {};
1664 sub = keymap.submaps[key.key];
1666 keymap.submaps[key.key] = sub = new MIM.Keymap ();
1670 keymap.map_actions = rule.actions;
1671 keymap.branch_actions = branch_actions;
1674 proto.Add = function (map, branch_actions)
1676 var rules = map.rules;
1677 var len = rules.length;
1679 for (var i = 0; i < len; i++)
1680 add_rule (this, rules[i], branch_actions);
1682 proto.Lookup = function (keys, index)
1686 if (index < keys.val.length && this.submaps
1687 && (sub = this.submaps[keys.val[index].key]))
1690 return sub.Lookup (keys, index);
1692 return { map: this, index: index };
1695 MIM.Keymap.prototype = proto;
1698 MIM.State = function (name)
1701 this.keymap = new MIM.Keymap ();
1705 var proto = new Xex.Term ('state');
1707 proto.Parser = function (domain, node)
1709 var map_list = domain.map_list;
1710 var name = node.attributes['sname'].nodeValue;
1712 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1713 var state = new MIM.State (name);
1714 for (node = node.firstElement (); node; node = node.nextElement ())
1716 if (node.nodeName == 'title')
1717 state.title = node.firstChild.nodeValue;
1720 var n = node.firstElement ();
1721 if (node.nodeName == 'branch')
1722 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1723 Xex.Term.Parse (domain, n, null));
1724 else if (node.nodeName == 'state-hook')
1725 state.enter_actions = Xex.Term.Parse (domain, n, null);
1726 else if (node.nodeName == 'catch-all-branch')
1727 state.fallback_actions = Xex.Term.Parse (domain, n, null);
1733 proto.toString = function ()
1735 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1738 MIM.State.prototype = proto;
1742 function Block (index, term)
1746 this.Data = term.val;
1747 else if (term.IsList)
1749 this.Data = new Array ();
1750 for (var i = 0; i < term.val.length; i++)
1751 this.Data.push (term.val[i].val);
1755 Block.prototype.Count = function () { return this.Data.length; }
1756 Block.prototype.get = function (i)
1758 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1761 MIM.Candidates = function (candidates, column)
1763 this.column = column;
1767 this.blocks = new Array ();
1769 for (var i = 0; i < candidates.length; i++)
1771 var block = new Block (this.total, candidates[i]);
1772 this.blocks.push (block);
1773 this.total += block.Count ();
1779 return (this.column > 0 ? this.index % this.column
1780 : this.index - this.blocks[this.row].Index);
1783 function prev_group ()
1785 var col = get_col.call (this);
1787 if (this.column > 0)
1789 this.index -= this.column;
1790 if (this.index >= 0)
1791 nitems = this.column;
1794 var lastcol = (this.total - 1) % this.column;
1795 this.index = (col < lastcol ? this.total - lastcol + col
1797 this.row = this.blocks.length - 1;
1798 nitems = lastcol + 1;
1800 while (this.blocks[this.row].Index > this.index)
1805 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1806 nitems = this.blocks[this.row].Count ();
1807 this.index = (this.blocks[this.row].Index
1808 + (col < nitems ? col : nitems - 1));
1813 function next_group ()
1815 var col = get_col.call (this);
1817 if (this.column > 0)
1819 this.index += this.column - col;
1820 if (this.index < this.total)
1822 if (this.index + col >= this.total)
1824 nitems = this.total - this.index;
1825 this.index = this.total - 1;
1829 nitems = this.column;
1838 while (this.blocks[this.row].Index > this.index)
1843 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1844 nitems = this.blocks[this.row].Count ();
1845 this.index = (this.blocks[this.row].Index
1846 + (col < nitems ? col : nitems - 1));
1853 if (this.index == 0)
1855 this.index = this.total - 1;
1856 this.row = this.blocks.length - 1;
1861 if (this.blocks[this.row].Index > this.index)
1869 if (this.index == this.total)
1876 var b = this.blocks[this.row];
1877 if (this.index == b.Index + b.Count ())
1884 this.index -= get_col.call (this);
1885 while (this.blocks[this.row].Index > this.index)
1891 var b = this.blocks[this.row];
1892 if (this.column > 0)
1894 if (this.index + 1 < this.total)
1896 this.index += this.column - get_col.call (this) + 1;
1897 while (b.Index + b.Count () <= this.index)
1898 b = this.blocks[++this.row];
1902 this.index = b.Index + b.Count () - 1;
1905 MIM.Candidates.prototype.Current = function ()
1907 var b = this.blocks[this.row];
1908 return b.get (this.index - b.Index);
1911 MIM.Candidates.prototype.Select = function (selector)
1913 if (selector.type == 'selector')
1915 switch (selector.val)
1917 case '@<': first.call (this); break;
1918 case '@>': last.call (this); break;
1919 case '@-': prev.call (this); break;
1920 case '@+': next.call (this); break;
1921 case '@[': prev_group.call (this); break;
1922 case '@]': next_group.cal (this); break;
1925 return this.Current ();
1928 if (this.column > 0)
1930 col = this.index % this.column;
1931 start = this.index - col;
1932 end = start + this.column;
1936 start = this.blocks[this.row].Index;
1937 col = this.index - start;
1938 end = start + this.blocks[this.row].Count;
1940 if (end > this.total)
1942 this.index += selector.val - col;
1943 if (this.index >= end)
1944 this.index = end - 1;
1945 if (this.column > 0)
1947 if (selector.val > col)
1948 while (this.blocks[this.row].Index + this.blocks[this.row].Count
1952 while (this.blocks[this.row].Index > this.index)
1955 return this.Current ();
1959 MIM.im_domain = new Xex.Domain ('input-method', null, null);
1960 MIM.im_domain.DefType (MIM.KeySeq.prototype);
1961 MIM.im_domain.DefType (MIM.Marker.prototype);
1962 MIM.im_domain.DefType (MIM.Selector.prototype);
1963 MIM.im_domain.DefType (MIM.Rule.prototype);
1964 MIM.im_domain.DefType (MIM.Map.prototype);
1965 MIM.im_domain.DefType (MIM.State.prototype);
1968 var im_domain = MIM.im_domain;
1970 function Finsert (domain, vari, args)
1973 if (args[0].type == 'integer')
1974 text = String.fromCharCode (args[0].val);
1977 domain.context.ins (text, null);
1981 function Finsert_candidates (domain, vari, args)
1983 var ic = domain.context;
1984 var gsize = domain.variables['candidates_group_size'];
1985 var candidates = new MIM.Candidates (args, gsize ? gsize.Intval : 0);
1986 ic.ins (candidates.Current (), candidates);
1990 function Fdelete (domain, vari, args)
1992 var ic = domain.context;
1993 var pos = args[0].IsInt ? args[0].Intval : args[0].Position (ic);
1994 return new Xex.IntTerm (ic.del (pos));
1997 function Fselect (domain, vari, args)
1999 var ic = domain.context;
2000 var can = ic.candidates;
2004 var old_text = can.Current ();
2005 var new_text = can.Select (args[0]);
2006 ic.rep (old_text, new_text, can);
2009 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2013 function Fchar_at (domain, vari, args)
2015 return new Xex.IntTerm (args[0].CharAt (domain.context));
2018 function Fmove (domain, vari, args)
2020 var ic = domain.context;
2021 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2023 return new Xex.IntTerm (pos);
2026 function Fmark (domain, vari, args)
2028 args[0].Mark (domain.context);
2032 function Fpushback (domain, vari, args)
2034 var a = (args[0].IsInt ? args[0].Intval ()
2035 : args[0].IsStr ? new KeySeq (args[0])
2037 domain.context.pushback (a);
2041 function Fundo (domain, vari, args)
2043 var ic = domain.context;
2044 var n = args.length == 0 ? -2 : args[0].val;
2046 ic.keys.val.splice (ic.keys.length + n, -n);
2048 ic.keys.val.splice (n, ic.keys.length);
2053 function Fcommit (domain, vari, args)
2055 domain.context.commit ();
2059 function Funhandle (domain, vari, args)
2061 domain.context.commit ();
2062 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2065 function Fshift (domain, vari, args)
2067 var ic = domain.context;
2068 var state_name = args[0].val;
2069 var state = ic.im.state_list[state_name];
2071 throw ("Unknown state: " + state_name);
2076 function Fsurrounding_flag (domain, vari, args)
2078 return new Xex.IntTerm (-1);
2081 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2082 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2083 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2084 im_domain.DefSubr (Fselect, "select", false, 1, 1);
2085 //im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2086 //im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2087 im_domain.DefSubr (Fmove, "move", false, 1, 1);
2088 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2089 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2090 //im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2091 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2092 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2093 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2094 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2095 //im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2096 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2097 //im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2098 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2103 function get_global_var (vname)
2105 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2106 MIM.im_global.Load ()
2107 return MIM.im_global.domain.variables[vname];
2110 function include (node)
2112 node = node.firstElement ();
2113 if (node.nodeName != 'tags')
2116 var lang = null, name = null, extra = null;
2117 for (node = node.firstElement (); node; node = node.nextElement ())
2119 if (node.nodeName == 'language')
2120 lang = node.firstChild.nodeValue;
2121 else if (node.nodeName == 'name')
2122 name = node.firstChild.nodeValue;
2123 else if (node.nodeName == 'extra-id')
2124 extra = node.firstChild.nodeValue;
2126 if (! lang || ! MIM.imlist[lang])
2130 if (! name || ! (im = MIM.imlist[lang][name]))
2135 if (! (im = MIM.imextra[lang][extra]))
2138 if (im.load_status != MIM.LoadStatus.Loaded
2139 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2146 parsers['description'] = function (node)
2148 this.description = node.firstChild.nodeValue;
2150 parsers['variable-list'] = function (node)
2152 for (node = node.firstElement (); node; node = node.nextElement ())
2154 var vname = node.attributes['vname'].nodeValue;
2155 if (this != MIM.im_global)
2157 var vari = get_global_var (vname);
2159 this.domain.Defvar (vname);
2161 vname = Xex.Term.Parse (this.domain, node)
2164 parsers['command-list'] = function (node)
2167 parsers['macro-list'] = function (node)
2169 for (var n = node.firstElement (); n; n = n.nextElement ())
2170 if (n.nodeName == 'xi:include')
2172 var im = include (n);
2174 alert ('inclusion fail');
2176 for (var macro in im.domain.functions)
2178 var func = im.domain.functions[macro];
2179 if (func instanceof Xex.Macro)
2180 im.domain.CopyFunc (this.domain, macro);
2182 n = n.previousSibling;
2183 node.removeChild (n.nextSibling);
2185 Xex.Term.Parse (this.domain, node.firstElement (), null);
2187 parsers['title'] = function (node)
2189 this.title = node.firstChild.nodeValue;
2191 parsers['map-list'] = function (node)
2193 for (node = node.firstElement (); node; node = node.nextElement ())
2195 if (node.nodeName == 'xi:include')
2197 var im = include (node);
2200 alert ('inclusion fail');
2203 for (var mapname in im.map_list)
2204 this.map_list[mapname] = im.map_list[mapname];
2208 var map = Xex.Term.Parse (this.domain, node);
2209 this.map_list[map.name] = map;
2213 parsers['state-list'] = function (node)
2215 this.domain.map_list = this.map_list;
2216 for (node = node.firstElement (); node; node = node.nextElement ())
2218 if (node.nodeName == 'state')
2220 var state = Xex.Term.Parse (this.domain, node);
2222 state.title = this.title;
2223 if (! this.initial_state)
2224 this.initial_state = state;
2225 this.state_list[state.name] = state;
2228 delete this.domain.map_list;
2231 MIM.IM = function (lang, name, extra_id, file)
2235 this.extra_id = extra_id;
2237 this.load_status = MIM.LoadStatus.NotLoaded;
2238 this.domain = new Xex.Domain (this.lang + '-'
2239 + (this.name != 'nil'
2240 ? this.name : this.extra_id),
2241 MIM.im_domain, null);
2247 var node = Xex.Load (null, this.file);
2250 this.load_status = MIM.LoadStatus.Error;
2254 this.initial_state = null;
2255 this.state_list = {};
2256 for (node = node.firstElement (); node; node = node.nextElement ())
2258 var name = node.nodeName;
2259 var parser = parsers[name];
2261 parser.call (this, node);
2263 this.load_status = MIM.LoadStatus.Loaded;
2268 MIM.IM.prototype = proto;
2270 MIM.IC = function (im, target)
2272 if (im.load_status == MIM.LoadStatus.NotLoaded)
2274 if (im.load_status != MIM.LoadStatus.Loaded)
2275 alert ('im:' + im.name + ' error:' + im.load_status);
2277 this.target = target;
2278 this.domain = new Xex.Domain ('context', im.domain, this);
2280 this.range = new Array ();
2281 this.range[0] = this.range[1] = 0;
2283 this.initial_state = this.im.initial_state;
2284 this.keys = new MIM.KeySeq ();
2285 this.marker_positions = new Array ();
2286 this.candidate_table = new MIM.CandidateTable ();
2290 MIM.CandidateTable = function ()
2292 this.table = new Array ();
2295 MIM.CandidateTable.prototype.get = function (pos)
2297 for (var i = 0; i < this.table.length; i++)
2299 var elt = this.table[i];
2300 if (elt.from < pos && pos <= elt.to)
2305 MIM.CandidateTable.prototype.put = function (from, to, candidates)
2307 for (var i = 0; i < this.table.length; i++)
2309 var elt = this.table[i];
2310 if (elt.from < to && elt.to > from)
2314 elt.val = candidates;
2318 this.table.push ({ from: from, to: to, val: candidates });
2321 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2323 var diff = inserted - (to - from);
2326 for (var i = 0; i < this.table.length; i++)
2328 var elt = this.table[i];
2337 MIM.CandidateTable.prototype.clear = function ()
2339 this.table.length = 0;
2342 function detach_candidates (ic)
2344 ic.candidate_table.clear ();
2345 ic.candidates = null;
2346 ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2347 | ChangedStatus.CandidateList
2348 | ChangedStatus.CandidateIndex
2349 | ChangedStatus.CandidateShow);
2352 function set_cursor (prefix, pos)
2354 this.cursor_pos = pos;
2355 this.candidates = this.candidate_table.get (pos);
2358 function save_state ()
2360 this.state_var_values = this.domain.SaveValues ();
2361 this.state_preedit = this.preedit;
2362 this.state_key_head = this.key_head;
2363 this.state_pos = this.cursor_pos;
2366 function restore_state ()
2368 this.domain.RestoreValues (this.state_var_values);
2369 this.preedit = this.state_preedit;
2370 set_cursor.call (this, "restore", this.state_pos);
2373 function handle_key ()
2375 var out = this.keymap.Lookup (this.keys, this.key_head);
2378 Xex.Log ('handling ' + this.keys.val[this.key_head]
2379 + ' in ' + this.state.name + ':' + this.keymap.name);
2380 this.key_head = out.index;
2381 if (sub != this.keymap)
2384 restore_state.call (this);
2386 Xex.Log ('submap found');
2387 if (this.keymap.map_actions)
2389 Xex.Log ('taking map actions:');
2390 if (! this.take_actions (this.keymap.map_actions))
2393 else if (this.keymap.submaps)
2395 Xex.Log ('no map actions');
2396 for (var i = this.state_key_head; i < this.key_head; i++)
2398 Xex.Log ('inserting key:' + this.keys.val[i].key);
2399 this.ins (this.keys.val[i].key, null);
2402 if (! this.keymap.submaps)
2404 Xex.Log ('terminal:');
2405 if (this.keymap.branch_actions != null)
2407 Xex.Log ('branch actions:');
2408 if (! this.take_actions (this.keymap.branch_actions))
2411 if (this.keymap != this.state.keymap)
2412 this.shift (this.state);
2417 Xex.Log ('no submap');
2418 var current_state = this.state;
2419 var map = this.keymap;
2421 if (map.branch_actions)
2423 Xex.Log ('branch actions');
2424 if (! this.take_actions (map.branch_actions))
2428 if (map == this.keymap)
2430 Xex.Log ('no state change');
2431 if (map == this.initial_state.keymap
2432 && this.key_head < this.keys.val.length)
2434 Xex.Log ('unhandled');
2437 if (this.keymap != current_state.keymap)
2438 this.shift (current_state);
2439 else if (this.keymap.actions == null)
2440 this.shift (this.initial_state);
2449 this.cursor_pos = 0;
2450 this.candidate_show = false;
2451 this.prev_state = null;
2452 this.title = this.initial_state.title;
2453 this.state_preedit = '';
2454 this.state_key_head = 0;
2455 this.state_var_values = {};
2458 this.keys.val.length = 0;
2459 this.key_unhandled = false;
2460 this.unhandled_key = null;
2461 this.changed = MIM.ChangedStatus.None;
2462 this.error_message = '';
2463 this.title = this.initial_state.title;
2466 this.preedit_saved = '';
2467 this.candidate_table.clear ();
2468 this.candidates = null;
2469 this.candidate_show = false;
2470 for (var elt in this.marker_positions)
2471 this.marker_positions[elt] = 0;
2472 this.shift (this.initial_state);
2475 catch_args: new Array (Xex.CatchTag._mimtag, null),
2477 take_actions: function (actions)
2479 var func_progn = this.domain.GetFunc ('progn');
2480 var func_catch = this.domain.GetFunc ('catch');
2481 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2482 var term = new Xex.Funcall (func_catch, null, this.catch_args);
2483 term = term.Eval (this.domain);
2484 return (! term.IsSymbol || term.val != '@mimtag');
2487 GetSurroundingChar: function (pos)
2491 pos += this.range[0];
2497 pos += this.range[1];
2498 if (pos >= this.target.value.length)
2501 return this.target.value.charCodeAt (pos);
2504 DelSurroundText: function (pos)
2509 pos += this.range[0];
2515 text = this.target.value.substring (0, pos);
2516 if (this.range[0] < this.target.value.length)
2517 text += this.target.value.substring (this.range[0]);
2518 this.target.value = text;
2519 this.range[1] -= this.range[0] - pos;
2520 this.range[0] = pos;
2524 pos += this.range[1];
2525 text = this.target.value.substring (0, this.range[1]);
2526 if (pos >= this.target.value.length)
2527 pos = this.target.value.length;
2529 text += this.target.value.substring (pos);
2530 this.target.value = text;
2534 adjust_markers: function (from, to, inserted)
2536 var diff = inserted - (to - from);
2538 for (var name in this.marker_positions)
2540 var pos = this.marker_positions[name];
2544 this.marker_positions[name] += diff;
2545 else if (pos > from)
2546 this.marker_positions[name] = from;
2549 if (this.cursor_pos >= to)
2550 set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2551 else if (this.cursor_pos > from)
2552 set_cursor.call (this, 'adjust', from)
2555 preedit_replace: function (from, to, text, candidates)
2557 this.preedit = (this.preedit.substring (0, from)
2558 + text + this.preedit.substring (to));
2559 this.adjust_markers (from, to, text.length);
2560 this.candidate_table.adjust (from, to, text.length);
2562 this.candidate_table.put (from, from + text.length, candidates)
2565 ins: function (text, candidates)
2567 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2568 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2571 rep: function (old_text, new_text, candidates)
2573 this.preedit_replace (this.cursor_pos - old_text.length,
2574 this.cursor_pos, new_text, candidates);
2575 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2580 var deleted = pos - this.cursor_pos;
2581 if (pos < this.cursor_pos)
2585 this.DelSurroundText (pos);
2586 deleted = - this.cursor_pos;
2589 if (pos < this.cursor_pos)
2590 this.preedit_replace (pos, this.cursor_pos, '', null);
2594 if (pos > this.preedit.length)
2596 this.DelSurroundText (pos - this.preedit.length);
2597 deleted = this.preedit.length - this.cursor_pos;
2598 pos = this.preedit.length;
2600 if (pos > this.cursor_pos)
2601 this.preedit_replace (this.cursor_pos, pos, '', null);
2604 this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2610 this.candidate_show = true;
2611 this.changed |= MIM.ChangedStatus.CandidateShow;
2616 this.candidate_show = false;
2617 this.changed |= MIM.ChangedStatus.CandidateShow;
2620 move: function (pos)
2624 else if (pos > this.preedit.length)
2625 pos = this.preedit.length;
2626 if (pos != this.cursor_pos)
2628 set_cursor.call (this, 'move', pos);
2629 this.changed |= MIM.ChangedStatus.Preedit;
2633 pushback: function (n)
2635 if (n instanceof MIM.KeySeq)
2637 if (this.key_head > 0)
2639 if (this.key_head < this.keys.val.length)
2640 this.keys.val.splice (this.key_head,
2641 this.keys.val.length - this.key_head);
2642 for (var i = 0; i < n.val.length; i++)
2643 this.keys.val.push (n.val[i]);
2649 if (this.key_head < 0)
2656 this.key_head = - n;
2657 if (this.key_head > this.keys.val.length)
2658 this.key_head = this.keys.val.length;
2664 if (this.key_head < this.keys.val.length)
2665 this.keys.val.splice (this.key_head, 1);
2670 if (this.preedit.length > 0)
2672 this.candidate_table.clear ();
2673 this.produced += this.preedit;
2674 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2678 shift: function (state)
2682 if (this.prev_state == null)
2684 state = this.prev_state;
2687 if (state == this.initial_state)
2692 this.keys.val.splice (0, this.key_head);
2694 this.prev_state = null;
2699 if (state != this.state)
2700 this.prev_state = this.state;
2702 if (state != this.state && state.enter_actions)
2703 this.take_actions (state.enter_actions);
2704 if (! this.state || this.state.title != state.title)
2705 this.changed |= MIM.ChangedStatus.StateTitle;
2707 this.keymap = state.keymap;
2708 this.state_key_head = this.key_head;
2709 save_state.call (this);
2712 Filter: function (key)
2716 this.key_unhandled = true;
2717 this.unhandled_key = key;
2720 if (key.key == '_reload')
2722 this.changed = MIM.ChangedStatus.None;
2724 this.key_unhandled = false;
2725 this.keys.val.push (key);
2727 while (this.key_head < this.keys.val.length)
2729 if (! handle_key.call (this))
2731 if (this.key_head < this.keys.val.length)
2733 this.unhandled_key = this.keys.val[this.key_head];
2734 this.keys.val.splice (this.key_head, this.key_head + 1);
2736 this.key_unhandled = true;
2742 this.key_unhandled = true;
2746 if (this.key_unhandled)
2748 this.keys.val.length = 0;
2749 this.key_head = this.state_key_head = this.commit_key_head = 0;
2751 return (! this.key_unhandled
2752 && this.produced.length == 0
2753 && this.preedit.length == 0);
2757 MIM.IC.prototype = proto;
2759 var node = Xex.Load (null, "imlist.xml");
2760 for (node = node.firstChild; node; node = node.nextSibling)
2761 if (node.nodeName == 'input-method')
2763 var lang = null, name = null, extra_id = null, file = null;
2765 for (var n = node.firstChild; n; n = n.nextSibling)
2767 if (n.nodeName == 'language')
2768 lang = n.firstChild.nodeValue;
2769 else if (n.nodeName == 'name')
2770 name = n.firstChild.nodeValue;
2771 else if (n.nodeName == 'extra-id')
2772 extra_id = n.firstChild.nodeValue;
2773 else if (n.nodeName == 'filename')
2774 file = n.firstChild.nodeValue;
2776 if (name && name != 'nil')
2778 if (! MIM.imlist[lang])
2779 MIM.imlist[lang] = {};
2780 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2782 else if (extra_id && extra_id != 'nil')
2784 if (! MIM.imextra[lang])
2785 MIM.imextra[lang] = {};
2786 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2789 if (MIM.imextra.t && MIM.imextra.t.global)
2790 MIM.im_global = MIM.imextra.t.global;
2793 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2794 MIM.im_global.load_status = MIM.LoadStatus.Error;
2800 var keys = new Array ();
2802 keys[0x08] = 'backspace';
2803 keys[0x0D] = 'return';
2804 keys[0x1B] = 'escape';
2805 keys[0x20] = 'space';
2806 keys[0x21] = 'pageup';
2807 keys[0x22] = 'pagedown';
2809 keys[0x24] = 'home';
2810 keys[0x25] = 'left';
2812 keys[0x27] = 'right';
2813 keys[0x28] = 'down';
2814 keys[0x2D] = 'insert';
2815 keys[0x2E] = 'delete';
2816 for (var i = 1; i <= 12; i++)
2817 keys[111 + i] = "f" + i;
2818 keys[0x90] = "numlock";
2819 keys[0xF0] = "capslock";
2821 MIM.decode_key_event = function (event)
2823 var key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
2824 : event.charCode ? event.charCode
2828 if (event.type == 'keydown')
2833 if (event.shiftKey) key = "S-" + key ;
2836 key = String.fromCharCode (key);
2837 if (event.altKey) key = "A-" + key ;
2838 if (event.ctrlKey) key = "C-" + key ;
2839 return new MIM.Key (key);
2843 MIM.add_event_listener
2844 = (window.addEventListener
2845 ? function (target, type, listener) {
2846 target.addEventListener (type, listener, false);
2848 : window.attachEvent
2849 ? function (target, type, listener) {
2850 target.attachEvent ('on' + type,
2852 listener.call (target, window.event);
2855 : function (target, type, listener) {
2857 = function (e) { listener.call (target, e || window.event); };
2860 MIM.debug_print = function (event, ic)
2864 if (! MIM.debug_nodes)
2866 MIM.debug_nodes = new Array ();
2867 MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
2868 MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
2869 MIM.debug_nodes['status0'] = document.getElementById ('status0');
2870 MIM.debug_nodes['status1'] = document.getElementById ('status1');
2871 MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
2872 MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
2873 MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
2874 MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
2876 var target = event.target;
2877 var code = event.keyCode;
2878 var ch = event.type == 'keydown' ? 0 : event.charCode;
2879 var key = MIM.decode_key_event (event);
2882 MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + " : " + key;
2883 index = (event.type == 'keydown' ? '0' : '1');
2885 MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
2887 MIM.debug_nodes['status' + index].innerHTML = 'no IM';
2888 MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
2889 MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
2892 MIM.debug_nodes.keypress.innerHTML = '';
2893 MIM.debug_nodes.status1.innerHTML = '';
2894 MIM.debug_nodes.keymap1.innerHTML = '';
2895 MIM.debug_nodes.preedit1.innerHTML = ''
2899 MIM.get_range = function (target, ic)
2902 if (target.selectionStart != null) // for Mozilla
2904 from = target.selectionStart;
2905 to = target.selectionEnd;
2909 var r = document.selection.createRange ();
2910 var rr = r.duplicate ();
2912 rr.moveToElementText (target);
2913 rr.setEndPoint ('EndToEnd', range);
2914 from = rr.text.length - r.text.length;
2915 to = rr.text.length;
2917 if (ic.range[0] == from && ic.range[1] == to
2918 && (to == from || target.value.substring (from, to) == ic.preedit))
2925 MIM.set_caret = function (target, ic)
2927 if (target.setSelectionRange) // Mozilla
2929 var scrollTop = target.scrollTop;
2930 target.setSelectionRange (ic.range[0], ic.range[1]);
2931 target.scrollTop = scrollTop;
2935 var range = target.createTextRange ();
2936 range.moveStart ('character', ic.range[0]);
2937 range.moveEnd ('character', ic.range[1]);
2942 MIM.update = function (target, ic)
2944 var text = target.value;
2945 target.value = (text.substring (0, ic.range[0])
2948 + text.substring (ic.range[1]));
2949 ic.range[0] += ic.produced.length;
2950 ic.range[1] = ic.range[0] + ic.preedit.length;
2951 MIM.set_caret (target, ic);
2954 MIM.reset_ic = function (event)
2956 if (event.target.mim_ic)
2958 var target = event.target;
2959 var ic = target.mim_ic;
2960 if (ic.preedit.length > 0)
2961 event.target.setSelectionRange (ic.range[1], ic.range[1]);
2966 MIM.keydown = function (event)
2968 var target = event.target;
2969 if (target.id == 'log')
2971 if (! (target.type == "text" || target.type == "textarea"))
2974 var ic = target.mim_ic;
2975 if (! ic || ic.im != MIM.current)
2977 target.mim_ic = null;
2978 Xex.Log ('creating IC');
2979 ic = new MIM.IC (MIM.current, target);
2980 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2983 MIM.add_event_listener (target, 'blur', MIM.reset_ic);
2984 MIM.get_range (target, ic)
2988 if (! MIM.get_range (target, ic))
2991 MIM.debug_print (event, ic);
2992 ic.key = MIM.decode_key_event (event);
2995 MIM.keypress = function (event)
2997 var target = event.target;
2998 if (target.id == 'log')
3000 if (! (target.type == "text" || target.type == "textarea"))
3003 var ic = target.mim_ic;
3007 if (ic.im.load_status != MIM.LoadStatus.Loaded)
3010 ic.key = MIM.decode_key_event (event);
3017 Xex.Log ("filtering " + ic.key);
3018 var result = ic.Filter (ic.key);
3019 MIM.update (target, ic);
3020 if (! ic.key_unhandled)
3021 event.preventDefault ();
3023 MIM.debug_print (event, ic);
3028 MIM.select_im = function (event)
3030 var target = event.target.parentNode;
3031 while (target.tagName != "SELECT")
3032 target = target.parentNode;
3035 for (var lang in MIM.imlist)
3036 for (var name in MIM.imlist[lang])
3037 if (idx++ == target.selectedIndex)
3039 im = MIM.imlist[lang][name];
3042 document.getElementsByTagName ('body')[0].removeChild (target);
3043 target.target.focus ();
3044 if (im && im != MIM.current)
3047 Xex.Log ('select IM: ' + im.name);
3051 MIM.destroy_menu = function (event)
3053 if (event.target.tagName == "SELECT")
3054 document.getElementsByTagName ('body')[0].removeChild (event.target);
3057 MIM.select_menu = function (event)
3059 var target = event.target;
3062 if (! ((target.type == "text" || target.type == "textarea")
3063 && event.which == 1 && event.ctrlKey))
3067 sel = document.createElement ('select');
3068 sel.onclick = MIM.select_im;
3069 sel.onmouseout = MIM.destroy_menu;
3070 sel.style.position='absolute';
3071 sel.style.left = (event.clientX - 10) + "px";
3072 sel.style.top = (event.clientY - 10) + "px";
3073 sel.target = target;
3075 for (var lang in MIM.imlist)
3076 for (var name in MIM.imlist[lang])
3078 var option = document.createElement ('option');
3079 var imname = lang + "-" + name;
3080 option.appendChild (document.createTextNode (imname));
3081 option.value = imname;
3082 sel.appendChild (option);
3083 if (MIM.imlist[lang][name] == MIM.current)
3084 sel.selectedIndex = idx;
3089 document.getElementsByTagName ('body')[0].appendChild (sel);
3092 MIM.select_menu = function (event)
3094 var target = event.target;
3097 if (! ((target.type == "text" || target.type == "textarea")
3098 && event.which == 1 && event.ctrlKey))
3102 menu = document.createElement ('select');
3103 menu.onclick = MIM.select_im;
3104 menu.onmouseout = MIM.destroy_menu;
3105 menu.style.position='absolute';
3106 menu.style.left = (event.clientX - 10) + "px";
3107 menu.style.top = (event.clientY - 10) + "px";
3108 menu.target = target;
3110 for (var lang in MIM.imlist)
3112 var sub = document.createElement ('option');
3113 sub.appendChild (document.createTextNode ('+ ' + lang));
3114 for (var name in MIM.imlist[lang])
3116 var option = document.createElement ('option');
3117 var imname = ' ' + lang + "-" + name;
3118 option.appendChild (document.createTextNode (imname));
3119 option.value = imname;
3120 sub.appendChild (option);
3121 if (MIM.imlist[lang][name] == MIM.current)
3122 menu.selectedIndex = idx;
3125 menu.appendChild (sub);
3129 document.getElementsByTagName ('body')[0].appendChild (menu);
3132 MIM.test = function ()
3134 var im = MIM.imlist['t']['latn-post'];
3135 var ic = new MIM.IC (im, null);
3137 ic.Filter (new MIM.Key ('a'));
3138 ic.Filter (new MIM.Key ("'"));
3141 document.getElementById ('text').value = ic.produced + ic.preedit;
3144 document.getElementById ('text').value
3145 = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3147 if (e instanceof Xex.ErrTerm)
3155 MIM.init = function ()
3157 MIM.add_event_listener (window, 'keydown', MIM.keydown);
3158 MIM.add_event_listener (window, 'keypress', MIM.keypress);
3159 MIM.add_event_listener (window, 'mousedown', MIM.select_menu);
3160 if (window.location == 'http://localhost/mim/index.html')
3161 MIM.server = 'http://localhost/mim';
3162 MIM.current = MIM.imlist['vi']['telex'];
3165 MIM.init_debug = function ()
3168 Xex.LogNode = document.getElementById ('log');