*** empty log message ***
[m17n/m17n-lib-js.git] / xex.js
1 // -* coding: utf-8; -*
2
3 var Xex = {
4   LogNode: null,
5   Log: function (arg, indent, cont)
6   {
7     if (! Xex.LogNode)
8       return;
9     if (! arg)
10       Xex.LogNode.value = '';
11     else
12       {
13         var str = '';
14         if (! cont)
15           {
16             Xex.LogNode.value += "\n";
17             if (indent != undefined)
18               for (var i = 0; i <= indent; i++)
19                 str += '  ';
20           }
21         Xex.LogNode.value += str + arg;
22         Xex.LogNode.scrollTop = Xex.LogNode.scrollHeight;
23       }
24   }
25 };
26
27 Xex.Error = {
28   UnknownError: "unknown-error",
29   WrongArgument: "wrong-argument",
30   // Load time errors.
31   InvalidInteger: "invalid-integer",
32   TermTypeInvalid: "term-type-invalid",
33   FunctionConflict: "function-conflict",
34   VariableTypeConflict: "variable-type-conflict",
35   VariableRangeConflict: "variable-range-conflict",
36   VariableWrongRange: "variable-wrong-range",
37   VariableWrongValue: "variable-wrong-value",
38
39   UnknownFunction: "unknown-function",
40   MacroExpansionError: "macro-expansion-error",
41   NoVariableName: "no-variable-name",
42   NoFunctionName: "no-funcion-name",
43
44   // Run time errors.
45   ArithmeticError: "arithmetic-error",
46   WrongType: "wrong-type",
47   IndexOutOfRange: "index-out-of-range",
48   ValueOutOfRange: "value-out-of-range",
49   NoLoopToBreak: "no-loop-to-break",
50   UncaughtThrow: "uncaught-throw"
51 };
52
53 Xex.Variable = function (domain, name, desc, val, range)
54 {
55   this.domain = domain;
56   this.name = name;
57   this.desc = desc;
58   this.val = val;
59   this.range = range;
60 }
61
62 Xex.Variable.prototype.clone = function ()
63 {
64   return new Xex.Variable (this.domain, this.name, this.desc,
65                            this.val, this.range);
66 }
67     
68 Xex.Variable.prototype.Equals = function (obj)
69 {
70   return ((obj instanceof Xex.Variable)
71           && obj.name == this.name);
72 }
73
74 Xex.Variable.prototype.SetValue = function (term)
75 {
76   this.val = term;
77   return term;
78 }
79
80 Xex.Function = function (name, with_var, min_args, max_args)
81 {
82   this.name = name;
83   this.with_var = with_var;
84   this.min_args = min_args;
85   this.max_args = max_args;
86 };  
87
88 Xex.Subrountine = function (builtin, name, with_var, min_args, max_args)
89 {
90   this.name = name;
91   this.with_var = with_var;
92   this.min_args = min_args;
93   this.max_args = max_args;
94   this.builtin = builtin;
95 }
96
97 Xex.Subrountine.prototype.Call = function (domain, vari, args)
98 {
99   var newargs = new Array ();
100   for (var i = 0; i < args.length; i++)
101     {
102       newargs[i] = args[i].Eval (domain);
103       if (domain.Thrown ())
104         return newargs[i];
105     }
106   return this.builtin (domain, vari, newargs)
107 }
108
109 Xex.SpecialForm = function (builtin, name, with_var, min_args, max_args)
110 {
111   this.name = name;
112   this.with_var = with_var;
113   this.min_args = min_args;
114   this.max_args = max_args;
115   this.builtin = builtin;
116 }
117
118 Xex.SpecialForm.prototype.Call = function (domain, vari, args)
119 {
120   return this.builtin (domain, vari, args)
121 }
122
123 Xex.Lambda = function (name, min_args, max_args, args, body)
124 {
125   this.name = name;
126   this.min_args = min_args;
127   this.max_args = max_args;
128   this.args = args;
129   this.body = body;
130 }
131
132 Xex.Lambda.prototype.Call = function (domain, vari, args)
133 {
134   var current = domain.bindings;
135   var result = Xex.Zero;
136   var limit = max_args >= 0 ? args.length : args.length - 1;
137   var i;
138   
139   try {
140     for (i = 0; i < limit; i++)
141       {
142         result = args[i].Eval (domain);
143         if (domain.Thrown ())
144           return result;
145         domain.Bind (this.args[i], result);
146       }
147     if (max_args < 0)
148       {
149         var list = new Array ();
150         for (i = 0; i < args[limit].length; i++)
151           {
152             result = args[limit].Eval (domain);
153             if (domain.Thrown ())
154               return result;
155             list[i] = result;
156           }
157         domain.Bind (this.args[limit], list);
158       }
159     try {
160       domain.Catch (Xex.CatchTag.Return);
161       for (var term in this.body)
162         {
163           result = term.Eval (domain);
164           if (domain.Thrown ())
165             return result;
166         }
167     } finally {
168       domain.Uncatch ();
169     }
170   } finally {
171     domain.UnboundTo (current);
172   }
173   return result;
174 }
175
176 Xex.Macro = function (name, min_args, max_args, args, body)
177 {
178   this.name = name;
179   this.min_args = min_args;
180   this.max_args = max_args;
181   this.args = args;
182   this.body = body;
183 }
184
185 Xex.Macro.prototype.Call = function (domain, vari, args)
186 {
187   var current = domain.bindings;
188   var result = Xex.Zero;
189   var i;
190
191   try {
192     for (i = 0; i < args.length; i++)
193       domain.Bind (this.args[i], args[i]);
194     try {
195       domain.Catch (Xex.CatchTag.Return);
196       for (var i in this.body)
197         {
198           result = this.body[i].Eval (domain);
199           if (domain.Thrown ())
200             break;
201         }
202     } finally {
203       domain.Uncatch ();
204     }
205   } finally {
206     domain.UnboundTo (current);
207   }
208   return result;
209 }
210
211 Xex.Bindings = function (vari)
212 {
213   this.vari = vari;
214   this.old_value = vari.val;
215 }
216
217 Xex.Bindings.prototype.UnboundTo = function (boundary)
218 {
219   for (var b = this; b != boundary; b = b.next)
220     b.vari.val = b.old_value;
221   return boundary;
222 }
223
224 Xex.Bind = function (bindings, vari, val)
225 {
226   var b = new Xex.Bindings (vari);
227   b.vari.val = val;
228   b.next = bindings;
229   return b;
230 }
231
232 Xex.CatchTag = {
233   Return: 0,
234   Break: 1
235 }
236
237 Xex.Domain = function (name, parent, context)
238 {
239   this.name = name;
240   this.context = context;
241   this.depth = 0;
242
243   if (name != 'basic' && ! parent)
244     parent = Xex.BasicDomain
245   this.parent = parent;
246   this.termtypes = {};
247   this.functions = {};
248   this.variables = {};
249   if (parent)
250     {
251       var elt;
252       for (elt in parent.termtypes)
253         this.termtypes[elt] = parent.termtypes[elt];
254       for (elt in parent.functions)
255         this.functions[elt] = parent.functions[elt];
256       for (elt in parent.variables)
257         {
258           var vari = parent.variables[elt];
259           this.variables[elt] = new Xex.Variable (this, vari.name, vari.desc,
260                                                   vari.val, vari.range);
261         }
262     }
263
264   this.call_stack = new Array ();
265   this.bindings = null;
266   this.catch_stack = new Array ();
267   this.catch_count = 0;
268   this.caught = false;
269 };
270
271 Xex.Domain.prototype = {
272   CallStackCount: function () { return this.call_stack.length; },
273   CallStackPush: function (term) { this.call_stack.push (term); },
274   CallStackPop: function () { this.call_stack.pop (); },
275   Bind: function (vari, val)
276   {
277     this.bindings = Xex.Bind (this.bindings, vari, val);
278   },
279   UnboundTo: function (boundary)
280   {
281     if (this.bindings)
282       this.bindings = this.bindings.UnboundTo (boundary);
283   },
284   Catch: function (tag) { this.catch_stack.push (tag); this.catch_count++; },
285   Uncatch: function ()
286   {
287     this.catch_stack.pop ();
288     if (this.catch_count > this.catch_stack.length)
289       this.catch_count--;
290   },
291   Thrown: function ()
292   {
293     if (this.catch_count < this.catch_stack.length)
294       {
295         this.caught = (this.catch_count == this.catch_stack.length - 1);
296         return true;
297       }
298     this.caught = false;
299     return false;
300   },
301   ThrowReturn: function ()
302   {
303     for (var i = this.catch_stack.length - 1; i >= 0; i--)
304       {
305         this.catch_count--;
306         if (this.catch_stack[i] == Xex.CatchTag.Return)
307           break;
308       }
309   },
310   ThrowBreak: function ()
311   {
312     if (this.catch_stack[this.catch_stack.length - 1] != Xex.CatchTag.Break)
313       throw new Xex.ErrTerm (Xex.Error.NoLoopToBreak,
314                            "No surrounding loop to break");
315     this.catch_count--;
316   },
317   ThrowSymbol: function (tag)
318   {
319     var i = this.catch_count;
320     for (var j = this.catch_stack.length - 1; j >= 0; j--)
321       {
322         i--;
323         if (Xex.CatchTag.Matches (this.catch_stack[i], tag))
324           {
325             this.catch_count = i;
326             return;
327           }
328       }
329     throw new Xex.ErrTerm (Xex.Error.UncaughtThrow,
330                          "No corresponding catch: " + tag);
331   },
332   DefType: function (obj)
333   {
334     var type = obj.type;
335     if (this.termtypes[type])
336       throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
337                            "Already defined: " + type);
338     if (this.functions[type])
339       throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
340                            "Already defined as a funciton or a macro: "
341                            + type);
342     this.termtypes[type] = obj.Parser;
343   },
344   DefSubr: function (builtin, name, with_var, min_args, max_args)
345   {
346     this.functions[name] = new Xex.Subrountine (builtin, name, with_var,
347                                                 min_args, max_args);
348   },
349   DefSpecial: function (builtin, name, with_var, min_args, max_args)
350   {
351     this.functions[name] = new Xex.SpecialForm (builtin, name, with_var,
352                                                 min_args, max_args);
353   },
354   Defun: function (name, min_args, max_args, args, body)
355   {
356     this.functions[name] =  new Xex.Lambda (name, min_args, max_args,
357                                             args, body);
358   },
359   DefunByFunc: function (func) { this.functions[func.name] = func; },
360   Defmacro: function (name, min_args, max_args, args, body)
361   {
362     this.functions[name] = new Xex.Macro (name, min_args, max_args,
363                                           args, body);
364   },
365   DefAlias: function (alias, fname)
366   {
367     var func = this.functions[fname];
368
369     if (! func)
370       throw new Xex.ErrTerm (Xex.Error.UnknownFunction, fname);
371     if (this.termtypes[alias])
372       throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
373                            "Already defined as a term type: " + alias);
374     if (this.functions[alias])
375       throw new Xex.ErrTerm (Xex.Error.FunctionConflict,
376                            "Already defined as a function: " + alias);
377     this.functions[alias] = func;
378   },
379   Defvar: function (name, desc, val, range)
380   {
381     var vari = new Xex.Variable (this, name, desc, val, range);
382     this.variables[name] = vari;
383     return vari;
384   },
385   GetFunc: function (name)
386   {
387     var func = this.functions[name];
388     if (! func)
389       throw new Xex.ErrTerm (Xex.Error.UnknownFunction,
390                              "Unknown function: " + name);
391     return func;
392   },
393   CopyFunc: function (domain, name)
394   {
395     var func = this.functions[name];
396     domain.DefunByFunc (func);
397     return true;
398   },
399   CopyFuncAll: function (domain)
400   {
401     for (var elt in this.functions)
402       domain.DefunByFunc (this.functions[elt]);
403   },
404   GetVarCreate: function (name)
405   {
406     var vari = this.variables[name];
407     if (! vari)
408       vari = this.variables[name] = new Xex.Variable (this, name, null,
409                                                       Xex.Zero, null);
410     return vari;
411   },
412   GetVar: function (name) { return this.variables[name]; },
413   SaveValues: function ()
414   {
415     values = {};
416     for (var elt in this.variables)
417       values[elt] = this.variables[elt].val.Clone ();
418     return values;
419   },
420   RestoreValues: function (values)
421   {
422     var name;
423     for (name in values)
424       {
425         var vari = this.variables[name];
426         vari.val = values[name];
427       }
428   }
429 };
430
431 Xex.Term = function (type) { this.type = type; }
432 Xex.Term.prototype = {
433   IsTrue: function () { return true; },
434   Eval: function (domain) { return this.Clone (); },
435   Clone: function (domain) { return this; },
436   Equals: function (obj)
437   {
438     return (this.type == obj.type
439             && this.val
440             && obj.val == this.val);
441   },
442   Matches: function (obj) { return this.Equals (obj); },
443   toString: function ()
444   {
445     if (this.val != undefined)
446       return '<' + this.type + '>' + this.val + '</' + this.type + '>';
447     return '<' + this.type + '/>';
448   },
449   Intval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
450                                                "Not an integer"); },
451   Strval: function () { throw new Xex.ErrTerm (Xex.Error.WrongType,
452                                                "Not a string"); }
453 };
454
455 Node.prototype.firstElement = function ()
456 {
457   for (var n = this.firstChild; n; n = n.nextSibling)
458     if (n.nodeType == 1)
459       return n;
460   return null;
461 }
462
463 Node.prototype.nextElement = function ()
464 {
465   for (var n = this.nextSibling; n; n = n.nextSibling)
466     if (n.nodeType == 1)
467       return n;
468   return null;
469 };
470
471 (function () {
472   function parse_defvar (domain, node)
473   {
474     var name = node.attributes['vname'].nodeValue;
475     if (! name)
476       throw new Xex.ErrTerm (Xex.Error.NoVariableName, node, '');
477     var vari = domain.variables[name];
478     var desc, val = null, range;
479     if (vari)
480       {
481         desc = vari.description;
482         val = vari.val;
483         range = vari.range;
484       }
485     node = node.firstElement ();
486     if (node && node.nodeName == 'description')
487       {
488         desc = node.firstChild.nodeValue;
489         node = node.nextElement ();
490       }
491     if (node)
492       {
493         val = Xex.Term.Parse (domain, node);
494         node = node.nextElement ();
495         if (node && node.nodeName == 'possible-values')
496           for (node = node.firstElement (); node; node = node.nextElement ())
497             {
498               var pval;
499               if (node.nodeName == 'range')
500                 {
501                   if (! val.IsInt)
502                     throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
503                                            'Range not allowed for ' + name);
504                   pval = new Array ();
505                   for (var n = node.firstElement (); n; n = n.nextElement ())
506                     {
507                       var v = Xex.Term.Parse (domain, n);
508                       if (! v.IsInt)
509                         throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
510                                                'Invalid range value: ' + val);
511                       pval.push (v);
512                     }
513                   }
514               else
515                 {
516                   pval = Xex.Term.Parse (domain, node);
517                   if (val.type != pval.type)
518                     throw new Xex.ErrTerm (Xex.Error.TermTypeInvalid,
519                                            'Invalid possible value: ' + pval);
520                 }
521               if (! range)
522                 range = new Array ();
523               range.push (pval);
524           }
525       }
526     if (val == null)
527       val = Xex.Zero;
528     domain.Defvar (name, desc, val, range);
529     return name;
530   }
531
532   function parse_defun_head (domain, node)
533   {
534     var name = node.attributes['fname'].nodeValue;
535     if (! name)
536       throw new Xex.ErrTerm (Xex.Error.NoFunctionName, node, '');
537     var args = new Array ();
538     var nfixed = 0, noptional = 0, nrest = 0;
539
540     node = node.firstElement ();
541     if (node && node.nodeName == 'args')
542       {
543         var n;
544         for (n = n.firstElement (); n; n = n.nextElement ())
545           {
546             if (n.nodeName == 'fixed')
547               nfixed++;
548             else if (n.nodeName == 'optional')
549               noptional++;
550             else if (n.nodeName == 'rest')
551               nrest++;
552             else
553               throw new Xex.ErrTerm (Xex.Error.WrongType, n, n.nodeName);
554           }
555         if (nrest > 1)
556           throw new Xex.ErrTerm (Xex.Error.WrongType, n, 'Too many <rest>');
557         for (n = node.firstElement (); n; n = n.nextElement ())
558           args.push (domain.DefVar (n.attributes['vname'].nodeValue));
559       }
560     args.min_args = nfixed;
561     args.max_args = nrest == 0 ? nfixed + noptional : -1;
562
563     if (node.nodeName == 'defun')
564       domain.Defun (name, args, null);
565     else
566       domain.Defmacro (name, args, null);
567     return name;
568   }
569
570   function parse_defun_body (domain, node)
571   {
572     var name = node.attributes['fname'].nodeValue;
573     var func = domain.GetFunc (name);
574     var body;
575     for (node = node.firstElement (); node; node = node.nextElement ())
576       if (node.nodeName != 'description' && node.nodeName != 'args')
577         break;
578     body = Xex.Term.Parse (domain, node, null);
579     func.body = body;
580   }
581
582   Xex.Term.Parse = function (domain, node, stop)
583   {
584     if (arguments.length == 2)
585       {
586         var name = node.nodeName;
587         var parser = domain.termtypes[name];
588
589         if (parser)
590           return parser (domain, node);
591         if (name == 'defun' || name == 'defmacro')
592           {
593             name = parse_defun_head (domain, node);
594             parse_defun_body (domain, node);
595             return new Xex.StrTerm (name);
596           }
597         if (name == 'defvar')
598           {
599             name = parse_defvar (domain, node);
600             return new Xex.StrTerm (name);
601           }
602         return new Xex.Funcall.prototype.Parser (domain, node);
603       }
604     for (var n = node; n && n != stop; n = n.nextElement ())
605       if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
606         parse_defun_head (domain, n);
607     var terms = null;
608     for (var n = node; n && n != stop; n = n.nextElement ())
609       {
610         if (n.nodeName == 'defun' || n.nodeName == 'defmacro')
611           parse_defun_body (domain, n);
612         else if (n.nodeName == 'defvar')
613           parse_defvar (domain, n);
614         else
615           {
616             if (! terms)
617               terms = new Array ();
618             terms.push (Xex.Term.Parse (domain, n));
619           }
620       }
621     return terms;
622   }
623 }) ();
624
625 Xex.Varref = function (vname)
626 {
627   this.val = vname;
628 };
629
630 (function () {
631   var proto = new Xex.Term ('varref');
632
633   proto.Clone = function () { return new Xex.Varref (this.val); }
634   proto.Eval = function (domain)
635   {
636     var vari = domain.GetVarCreate (this.val);
637     Xex.Log (this.ToString () + '=>' + vari.val, domain.depth);
638     return vari.val;
639   }
640
641   proto.Parser = function (domain, node)
642   {
643     return new Xex.Varref (node.attributes['vname'].nodeValue);
644   }
645
646   proto.ToString = function ()
647   {
648     return '<varref vname="' + this.val + '"/>';
649   }
650
651   Xex.Varref.prototype = proto;
652 }) ();
653
654 var null_args = new Array ();
655   
656 Xex.Funcall = function (func, vname, args)
657 {
658   this.func = func;
659   this.vname = vname;
660   this.args = args || null_args;
661 };
662
663 (function () {
664   var proto = new Xex.Term ('funcall');
665
666   proto.Parser = function (domain, node)
667   {
668     var fname = node.nodeName;
669     var attr;
670
671     if (fname == 'funcall')
672       fname = node.attributes['fname'].nodeValue;
673     var func = domain.GetFunc (fname);
674     var vname;
675     attr = node.attributes['vname'];
676     vname = attr != undefined ? attr.nodeValue : null;
677     var args = Xex.Term.Parse (domain, node.firstElement (), null);
678     return new Xex.Funcall (func, vname, args);
679   }
680
681   proto.New = function (domain, fname, vname, args)
682   {
683     var func = domain.GetFunc (fname);
684     var funcall = new Xex.Funcall (func, vname, args);
685     if (func instanceof Xex.Macro)
686       funcall = funcall.Eval (domain);
687     return funcall;
688   }
689
690   proto.Eval = function (domain)
691   {
692     Xex.Log (this, domain.depth);
693     var vari;
694     if (this.vname)
695       vari = domain.GetVarCreate (this.vname);
696     domain.depth++;
697     var result;
698     try {
699       result = this.func.Call (domain, vari, this.args);
700     } finally {
701       Xex.Log (' => ' + result, --domain.depth,
702                this.func instanceof Xex.Subrountine);
703     }
704     return result;
705   }
706
707   proto.Clone = function ()
708   {
709     return new Xex.Funcall (this.func, this.vari, this.args);
710   }
711
712   proto.Equals = function (obj)
713   {
714     return (obj.type == 'funcall'
715             && obj.func == this.func
716             && obj.vari.Equals (this.vari)
717             && obj.args.length == this.func.length);
718   }
719
720   proto.toString = function ()
721   {
722     var arglist = ''
723     var len = this.args.length;
724     var str = '<' + this.func.name;
725     if (this.vari)
726       str += ' vname="' + this.vari.name + '"';
727     if (len == 0)
728       return str + '/>';
729     if (this.func instanceof Xex.Subrountine)
730       for (var i = 0; i < len; i++)
731         arglist += this.args[i].toString ();
732     else
733       for (var i = 0; i < len; i++)
734         arglist += '.';
735     return str + '>' + arglist + '</' + this.func.name + '>';
736   }
737
738   Xex.Funcall.prototype = proto;
739 }) ();
740
741 Xex.ErrTerm = function (ename, message, stack)
742 {
743   this.ename = ename;
744   this.message = message;
745   this.stack = stack;
746 };
747
748 (function () {
749   var proto = new Xex.Term ('error');
750
751   proto.IsError = true;
752
753   proto.Parser = function (domain, node)
754   {
755     return new Xex.ErrTerm (node.attributes['ename'].nodeValue,
756                             node.innerText, false);
757   }
758
759   proto.CallStack = function () { return stack; }
760
761   proto.SetCallStack = function (value) { statck = value; }
762
763   proto.Clone = function ()
764   {
765     return new Xex.ErrTerm (ename, message, false);
766   }
767
768   proto.Equals = function (obj)
769   {
770     return (obj.IsError
771             && obj.ename == ename && obj.message == message
772             && (obj.stack ? (stack && stack.length == obj.stack.length)
773                 : ! stack));
774   }
775
776   proto.Matches = function (obj)
777   {
778     return (obj.IsError && obj.ename == ename);
779   }
780
781   proto.toString = function ()
782   {
783     return '<error ename="' + this.ename + '">' + this.message + '</error>';
784   }
785
786   Xex.ErrTerm.prototype = proto;
787 }) ();
788
789 Xex.IntTerm = function (num) { this.val = num; };
790 (function () {
791   var proto = new Xex.Term ('integer');
792   proto.IsInt = true;
793   proto.Intval = function () { return this.val; };
794   proto.IsTrue = function () { return this.val != 0; }
795   proto.Clone = function () { return new Xex.IntTerm (this.val); }
796   proto.Parser = function (domain, node)
797   {
798     var str = node.firstChild.nodeValue;
799
800     if (str.charAt (0) == '?' && str.length == 2)
801       return new Xex.IntTerm (str.charCodeAt (1));
802     return new Xex.IntTerm (parseInt (node.firstChild.nodeValue));
803   }
804   Xex.IntTerm.prototype = proto;
805 }) ();
806
807 Xex.StrTerm = function (str) { this.val = str; };
808 (function () {
809   var proto = new Xex.Term ('string');
810   proto.IsStr = true;
811   proto.Strval = function () { return this.val; };
812   proto.IsTrue = function () { return this.val.length > 0; }
813   proto.Clone = function () { return new Xex.StrTerm (this.val); }
814   proto.Parser = function (domain, node)
815   {
816     return new Xex.StrTerm (node.firstChild ? node.firstChild.nodeValue : '');
817   }
818   Xex.StrTerm.prototype = proto;
819 }) ();
820
821 Xex.SymTerm = function (str) { this.val = str; };
822 (function () {
823   var proto = new Xex.Term ('symbol');
824   proto.IsSymbol = true;
825   proto.IsTrue = function () { return this.val != 'nil'; }
826   proto.Clone = function () { return new Xex.SymTerm (this.val); }
827   proto.Parser = function (domain, node)
828   {
829     return new Xex.SymTerm (node.firstChild.nodeValue);
830   }
831   Xex.SymTerm.prototype = proto;
832 }) ();
833
834 Xex.LstTerm = function (list) { this.val = list; };
835 (function () {
836   var proto = new Xex.Term ('list');
837   proto.IsList = true;
838   proto.IsTrue = function () { return this.val.length > 0; }
839   proto.Clone = function () { return new Xex.LstTerm (this.val.slice (0)); }
840
841   proto.Equals = function (obj)
842   {
843     if (obj.type != 'list' || obj.val.length != this.val.length)
844       return false;
845     var i, len = this.val.length;
846     for (i = 0; i < len; i++)
847       if (! this.val[i].Equals (obj.val[i]))
848         return false;
849     return true;
850   }
851
852   proto.Parser = function (domain, node)
853   {
854     var list = Xex.Term.Parse (domain, node.firstElement (), null);
855     return new Xex.LstTerm (list);
856   }
857
858   proto.toString = function ()
859   {
860     var len = this.val.length;
861
862     if (len == 0)
863       return '<list/>';
864     var str = '<list>';
865     for (var i = 0; i < len; i++)
866       str += this.val[i].toString ();
867     return str + '</list>';
868   }
869   Xex.LstTerm.prototype = proto;
870 }) ();
871
872 (function () {
873   var basic = new Xex.Domain ('basic', null, null);
874
875   function Fset (domain, vari, args)
876   {
877     if (! vari)
878       throw new Xex.ErrTerm (Xex.Error.NoVariableName,
879                              'No variable name to set');
880     vari.SetValue (args[0]);
881     return args[0];
882   }
883
884   function Fnot (domain, vari, args)
885   {
886     return (args[0].IsTrue () ? Xex.Zero : Xex.One);
887   }
888
889   function maybe_set_intvar (vari, n)
890   {
891     var term = new Xex.IntTerm (n);
892     if (vari)
893       vari.SetValue (term);
894     return term;
895   }
896
897   function Fadd (domain, vari, args)
898   {
899     var n = vari ? vari.val.Intval () : 0;
900     var len = args.length;
901
902     for (var i = 0; i < len; i++)
903       n += args[i].Intval ();
904     return maybe_set_intvar (vari, n);
905   }
906
907   function Fmul (domain, vari, args)
908   {
909     var n = vari ? vari.val.Intval () : 1;
910     for (var i = 0; i < args.length; i++)
911       n *= arg.Intval ();
912     return maybe_set_intvar (vari, n);
913   }
914
915   function Fsub (domain, vari, args)
916   {
917     var n, i;
918
919     if (! vari)
920       {
921         n = args[0].Intval ();
922         i = 1;
923       }
924     else
925       {
926         n = vari.val.Intval ();
927         i = 0;
928       }
929     while (i < args.length)
930       n -= args[i++].Intval ();
931     return maybe_set_intvar (vari, n);
932   }
933
934   function Fdiv (domain, vari, args)
935   {
936     var n, i;
937
938     if (! vari == null)
939       {
940         n = args[0].Intval ();
941         i = 1;
942       }
943     else
944       {
945         n = vari.val.Intval ();
946         i = 0;
947       }
948     while (i < args.length)
949       n /= args[i++].Intval ();
950     return maybe_set_intvar (vari, n);
951   }
952
953   function Fmod (domain, vari, args)
954   {
955     return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
956   }
957
958   function Flogior (domain, vari, args)
959   {
960     var n = vari == null ? 0 : vari.val;
961     for (var i = 0; i < args.length; i++)
962       n |= args[i].val;
963     return maybe_set_intvar (vari, n);
964   }
965
966   function Flogand (domain, vari, args)
967   {
968     var n, i;
969     if (vari == null)
970       {
971         Xex.Log ("logand arg args[0]" + args[0]);
972         n = args[0].Intval ()
973         i = 1;
974       }
975     else
976       {
977         Xex.Log ("logand arg var " + vari);
978         n = vari.val.Intval ();
979         i = 0;
980       }
981     while (n > 0 && i < args.length)
982       {
983         Xex.Log ("logand arg " + args[i]);
984         n &= args[i++].val;
985       }
986     return maybe_set_intvar (vari, n);
987   }
988
989   function Flsh (domain, vari, args)
990   {
991     return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
992   }
993
994   function Frsh (domain, vari, args)
995   {
996     return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
997   }
998
999   function Fand (domain, vari, args)
1000   {
1001     var len = args.length;
1002     for (var i = 0; i < len; i++)
1003     {
1004       var result = args[i].Eval (domain);
1005       if (domain.Thrown ())
1006         return result;
1007       if (! result.IsTrue ())
1008         return Xex.Zero;
1009     }
1010     return Xex.One;
1011   }
1012
1013   function For (domain, vari, args)
1014   {
1015     var len = args.length;
1016     for (var i = 0; i < len; i++)
1017     {
1018       var result = args[i].Eval (domain);
1019       if (domain.Thrown ())
1020         return result;
1021       if (result.IsTrue ())
1022         return Xex.One;
1023     }
1024     return Xex.Zero;
1025   }
1026
1027   function Feq (domain, vari, args)
1028   {
1029     for (var i = 1; i < args.length; i++)
1030       if (! args[i - 1].Equals (args[i]))
1031         return Xex.Zero;
1032     return Xex.One;
1033   }
1034
1035   function Fnoteq (domain, vari, args)
1036   {
1037     return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1038   }
1039
1040   function Flt (domain, vari, args)
1041   {
1042     var n = args[0].Intval ();
1043
1044     for (var i = 1; i < args.length; i++)
1045       {
1046         var n1 = args[i].Intval ();
1047         if (n >= n1)
1048           return Xex.Zero;
1049         n = n1;
1050       }
1051     return Xex.One;
1052   }
1053
1054   function Fle (domain, vari, args)
1055   {
1056     var n = args[0].Intval ();
1057     for (var i = 1; i < args.length; i++)
1058       {
1059         var n1 = args[i].Intval ();
1060         if (n > n1)
1061           return Xex.Zero;
1062         n = n1;
1063       }
1064     return Xex.One;
1065   }
1066
1067   function Fgt (domain, vari, args)
1068   {
1069     var n = args[0].Intval ();
1070     for (var i = 1; i < args.length; i++)
1071       {
1072         var n1 = args[i].Intval ();
1073         if (n <= n1)
1074           return Xex.Zero;
1075         n = n1;
1076       }
1077     return Xex.One;
1078   }
1079
1080   function Fge (domain, vari, args)
1081   {
1082     var n = args[0].Intval ();
1083     for (var i = 1; i < args.Length; i++)
1084       {
1085         var n1 = args[i].Intval ();
1086         if (n < n1)
1087           return Xex.Zero;
1088         n = n1;
1089       }
1090     return Xex.One;
1091   }
1092
1093   function Fprogn (domain, vari, args)
1094   {
1095     var result = Xex.One;
1096     var len = args.length;
1097
1098     for (var i = 0; i < len; i++)
1099       {
1100         result = args[i].Eval (domain);
1101         if (domain.Thrown ())
1102           return result;
1103       }
1104     return result;
1105   }
1106
1107   function Fif (domain, vari, args)
1108   {
1109     var result = args[0].Eval (domain);
1110
1111     if (domain.Thrown ())
1112       return result;
1113     if (result.IsTrue ())
1114       return args[1].Eval (domain);
1115     if (args.length == 2)
1116       return Zero;
1117     return args[2].Eval (domain);
1118   }
1119
1120   function Fcond (domain, vari, args)
1121   {
1122     for (var i = 0; i < args.length; i++)
1123       {
1124         var list = args[i];
1125         var result = list.val[0].Eval (domain);
1126         if (result.IsTrue ())
1127           {
1128             for (var j = 1; j < list.val.length; j++)
1129               {
1130                 domain.depth++;
1131                 result = list.val[j].Eval (domain);
1132                 domain.depth--;
1133                 if (domain.Thrown ())
1134                   return result;
1135                 }
1136             return result;
1137           }
1138       }
1139     return Xex.Zero;
1140   }
1141
1142   function eval_terms (domain, terms, idx)
1143   {
1144     var result = Xex.Zero;
1145     domain.caught = false;
1146     for (var i = idx; i < terms.length; i++)
1147       {
1148         result = terms[i].Eval (domain);
1149         if (domain.Thrown ())
1150           return result;
1151       }
1152     return result;
1153   }
1154
1155   function Fcatch (domain, vari, args)
1156   {
1157     var caught = false;
1158     var result;
1159
1160     if (args[0].IsError)
1161       {
1162         try {
1163           result = eval_terms (domain, args, 1);
1164         } catch (e) {
1165           if (e instanceof Xex.ErrTerm)
1166             {
1167               if (! args[0].Matches (e))
1168                 throw e;
1169               if (vari)
1170                 vari.SetValue (e);
1171               return Xex.One;
1172             }
1173         }
1174       }
1175     else if (args[0].IsSymbol)
1176       {
1177         try {
1178           domain.Catch (args[0].val);
1179           result = eval_terms (domain, args, 1);
1180           if (domain.caught)
1181             {
1182               if (vari != null)
1183                 vari.SetValue (result);
1184               return Xex.One;
1185             }
1186           return Xex.Zero;
1187         } finally {
1188           domain.Uncatch ();
1189         }
1190       }
1191     throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1192                            "Not a symbol nor an error: " + args[0]);
1193   }
1194
1195   function Fthrow (domain, vari, args)
1196   {
1197     if (args[0].IsSymbl)
1198       {
1199         domain.ThrowSymbol (args[0]);
1200         return (args[args.length - 1]);
1201       }
1202     if (args[0].IsError)
1203       {
1204         throw args[0];
1205       }
1206     throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1207                            "Not a symbol nor an error:" + args[0]);
1208   }
1209
1210   Xex.BasicDomain = basic;
1211
1212   basic.DefSubr (Fset, "set", true, 1, 1);
1213   basic.DefAlias ("=", "set");
1214   basic.DefSubr (Fnot, "not", false, 1, 1);
1215   basic.DefAlias ("!", "not");
1216   basic.DefSubr (Fadd, "add", true, 1, -1);
1217   basic.DefSubr (Fmul, "mul", true, 1, -1);
1218   basic.DefAlias ("*", "mul");
1219   basic.DefSubr (Fsub, "sub", true, 1, -1);
1220   basic.DefAlias ("-", "sub");
1221   basic.DefSubr (Fdiv, "div", true, 1, -1);
1222   basic.DefAlias ("/", "div");
1223   basic.DefSubr (Fmod, "mod", true, 1, 2);
1224   basic.DefAlias ("%", "mod");
1225   basic.DefSubr (Flogior, "logior", true, 1, -1);
1226   basic.DefAlias ('|', "logior");
1227   basic.DefSubr (Flogand, "logand", true, 1, -1);
1228   basic.DefAlias ("&", "logand");
1229   basic.DefSubr (Flsh, "lsh", true, 1, 2);
1230   basic.DefAlias ("<<", "lsh");
1231   basic.DefSubr (Frsh, "rsh", true, 1, 2);
1232   basic.DefAlias (">>", "rsh");
1233   basic.DefSubr (Feq, "eq", false, 2, -1);
1234   basic.DefAlias ("==", "eq");
1235   basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1236   basic.DefAlias ("!=", "noteq");
1237   basic.DefSubr (Flt, "lt", false, 2, -1);
1238   basic.DefAlias ("<", "lt");
1239   basic.DefSubr (Fle, "le", false, 2, -1);
1240   basic.DefAlias ("<=", "le");
1241   basic.DefSubr (Fgt, "gt", false, 2, -1);
1242   basic.DefAlias (">", "gt");
1243   basic.DefSubr (Fge, "ge", false, 2, -1);
1244   basic.DefAlias (">=", "ge");
1245   basic.DefSubr (Fthrow, "throw", false, 1, 2);
1246
1247   //basic.DefSubr (Fappend, "append", true, 0, -1);
1248   //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1249   //basic.DefSubr (Fnth, "nth", false, 2, 2);
1250   //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1251   //basic.DefSubr (Fins, "ins", true, 2, 2);
1252   //basic.DefSubr (Fdel, "del", true, 2, 2);
1253   //basic.DefSubr (Feval, "eval", false, 1, 1);
1254   //basic.DefSubr (Fbreak, "break", false, 0, 1);
1255   //basic.DefSubr (Freturn, "return", false, 0, 1);
1256   //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1257
1258   basic.DefSpecial (Fand, "and", false, 1, -1);
1259   basic.DefAlias ("&&", "and");
1260   basic.DefSpecial (For, "or", false, 1, -1);
1261   basic.DefAlias ("||", "or");
1262   basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1263   basic.DefAlias ("expr", "progn");
1264   basic.DefSpecial (Fif, "if", false, 2, 3);
1265   //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1266   //basic.DefSpecial (Floop, "loop", false, 1, -1);
1267   //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1268   basic.DefSpecial (Fcond, "cond", false, 1, -1);
1269   //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1270   //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1271   //basic.DefSpecial (Ftype, "type", false, 1, 1);
1272   basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1273
1274   basic.DefType (Xex.Funcall.prototype);
1275   basic.DefType (Xex.Varref.prototype);
1276   basic.DefType (Xex.ErrTerm.prototype);
1277   basic.DefType (Xex.IntTerm.prototype);
1278   basic.DefType (Xex.StrTerm.prototype);
1279   basic.DefType (Xex.SymTerm.prototype);
1280   basic.DefType (Xex.LstTerm.prototype);
1281
1282 }) ();
1283
1284 Xex.Zero = new Xex.IntTerm (0);
1285 Xex.One = new Xex.IntTerm (1);
1286 Xex.nil = new Xex.SymTerm ('nil');
1287
1288 Xex.Load = function (server, file)
1289 {
1290   var obj = new XMLHttpRequest ();
1291   var url = server ? server + '/' + file : file;
1292   obj.open ('GET', url, false);
1293   obj.overrideMimeType ('text/xml');
1294   obj.send ('');
1295   return obj.responseXML.firstChild;
1296 }
1297
1298 var MIM = {
1299   // URL of the input method server.
1300   server: "http://www.m17n.org/common/mim-js",
1301   // Boolean flag to tell if MIM is active or not.
1302   enabled: true,
1303   // Boolean flag to tell if MIM is running in debug mode or not.
1304   debug: false,
1305   // List of main input methods.
1306   imlist: {},
1307   // List of extra input methods;
1308   imextra: {},
1309   // Global input method data
1310   im_global: null,
1311   // Currently selected input method.
1312   current: false,
1313
1314   // enum
1315   LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1316   ChangedStatus: {
1317     None:       0x00,
1318     StateTitle: 0x01,
1319     PreeditText:0x02,
1320     CursorPos:  0x04,
1321     CandidateList:0x08,
1322     CandidateIndex:0x10,
1323     CandidateShow:0x20,
1324     Preedit:    0x06,           // PreeditText | CursorPos
1325     Candidate:  0x38 // CandidateList | CandidateIndex | CandidateShow
1326   },
1327   KeyModifier: {
1328     SL: 0x00400000,
1329     SR: 0x00800000,
1330     S:  0x00C00000,
1331     CL: 0x01000000,
1332     CR: 0x02000000,
1333     C:  0x03000000,
1334     AL: 0x04000000,
1335     AR: 0x08000000,
1336     A:  0x0C000000,
1337     ML: 0x04000000,
1338     MR: 0x08000000,
1339     M:  0x0C000000,
1340     G:  0x10000000,
1341     s:  0x20000000,
1342     H:  0x40000000,
1343     High:       0x70000000,
1344     All:        0x7FC00000
1345   },
1346   Error: {
1347     ParseError: "parse-error"
1348   }
1349 };
1350   
1351 (function () {
1352   var keysyms = new Array ();
1353   keysyms["bs"] = "backspace";
1354   keysyms["lf"] = "linefeed";
1355   keysyms["cr"] = keysyms["enter"] = "return";
1356   keysyms["esc"] = "escape";
1357   keysyms["spc"] = "space";
1358   keysyms["del"] = "delete";
1359
1360   function decode_keysym (str) {
1361     if (str.length == 1)
1362       return str;
1363     var parts = str.split ("-");
1364     var len = parts.length, i;
1365     var has_modifier = len > 1;
1366
1367     for (i = 0; i < len - 1; i++)
1368       if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1369         return false;
1370     var key = parts[len - 1];
1371     if (key.length > 1)
1372       {
1373         key = keysyms[key.toLowerCase ()];
1374         if (key)
1375           {
1376             if (len > 1)
1377               {
1378                 str = parts[0];
1379                 for (i = 1; i < len - 1; i++)
1380                   str += '-' + parts[i];
1381                 str += '-' + key;
1382               }
1383             else
1384               str = key;
1385           }
1386       }
1387     if (has_modifier)
1388       {
1389         parts = new Array ();
1390         parts.push (str);
1391         return parts;
1392       }
1393     return str;
1394   }
1395
1396   MIM.Key = function (val)
1397   {
1398     this.key;
1399     if (val instanceof Xex.Term)
1400       this.key = val.val;
1401     else if (typeof val == 'string' || val instanceof String)
1402       {
1403         this.key = decode_keysym (val);
1404         if (! this.key)
1405           throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1406         if (this.key instanceof Array)
1407           {
1408             this.key = this.key[0];
1409             this.has_modifier = true;
1410           }
1411       }
1412     else if (typeof val == 'number' || val instanceof Number)
1413       this.key = String.fromCharCode (val);
1414     else
1415       throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1416   }
1417
1418   MIM.Key.prototype.toString = function () { return this.key; };
1419
1420   MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1421   MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1422   MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1423 }) ();
1424
1425 (function () {
1426   MIM.KeySeq = function (seq)
1427   {
1428     this.val = new Array ();
1429
1430     if (seq)
1431       {
1432         if (seq.IsList)
1433           {
1434             var len = seq.val.length;
1435             for (var i = 0; i < len; i++)
1436               {
1437                 var v = seq.val[i], key;
1438                 if (v.type == 'symbol' || v.type == 'string')
1439                   key = new MIM.Key (v);
1440                 else if (v.type == 'integer')
1441                   key = new MIM.Key (v.val);
1442                 else
1443                   throw new Xex.ErrTerm (MIM.Error.ParseError,
1444                                          "Invalid key: " + v);
1445                 this.val.push (key);
1446                 if (key.has_modifier)
1447                   this.has_modifier = true;
1448               }
1449           }
1450         else if (seq.IsStr)
1451           {
1452             var len = seq.val.length;
1453             for (var i = 0; i < len; i++)
1454               this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1455           }
1456         else
1457           throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1458       }
1459   }
1460
1461   var proto = new Xex.Term ('keyseq');
1462   proto.Clone = function () { return this; }
1463   proto.Parser = function (domain, node)
1464   {
1465     var seq = new Array ();
1466     for (node = node.firstChild; node; node = node.nextSibling)
1467       if (node.nodeType == 1)
1468         {
1469           var term = Xex.Term.Parse (domain, node);
1470           return new MIM.KeySeq (term);
1471         }
1472     throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1473   }
1474   proto.toString = function ()
1475   {
1476     var len = this.val.length;
1477     if (len == 0)
1478       return '<keyseq/>';
1479     var first = true;
1480     var str = '<keyseq>';
1481     for (var i = 0; i < len; i++)
1482       {
1483         if (first)
1484           first = false;
1485         else if (this.has_modifier)
1486           str += ' ';
1487         str += this.val[i].toString ();
1488       }
1489     return str + '</keyseq>';
1490   }
1491
1492   MIM.KeySeq.prototype = proto;
1493 }) ();
1494
1495 (function () {
1496   MIM.Marker = function () { }
1497   MIM.Marker.prototype = new Xex.Term ('marker');
1498   MIM.Marker.prototype.CharAt = function (ic)
1499   {
1500     var p = this.Position (ic);
1501     if (p < 0 || p >= ic.preedit.length)
1502       return 0;
1503     return ic.preedit.charCodeAt (p);
1504   }
1505
1506   MIM.FloatingMarker = function (name) { this.val = name; };
1507   var proto = new MIM.Marker ();
1508   MIM.FloatingMarker.prototype = proto;
1509   proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1510   proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1511
1512   MIM.PredefinedMarker = function (name) { this.val = name; }
1513   MIM.PredefinedMarker.prototype = new MIM.Marker ();
1514   MIM.PredefinedMarker.prototype.Position = function (ic)
1515   {
1516     if (typeof this.pos == 'number')
1517       return this.pos;
1518     return this.pos (ic);
1519   }
1520
1521   var predefined = { }
1522
1523   function def_predefined (name, position)
1524   {
1525     predefined[name] = new MIM.PredefinedMarker (name);
1526     predefined[name].pos = position;
1527   }
1528
1529   def_predefined ('@<', 0);
1530   def_predefined ('@>', function (ic) { return ic.preedit.length; });
1531   def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1532   def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1533   def_predefined ('@[', function (ic) {
1534     if (ic.cursor_pos > 0)
1535       {
1536         var pos = ic.cursor_pos;
1537         return ic.preedit.FindProp ('candidates', pos - 1).from;
1538       }
1539     return 0;
1540   });
1541   def_predefined ('@]', function (ic) {
1542     if (ic.cursor_pos < ic.preedit.length - 1)
1543       {
1544         var pos = ic.cursor_pos;
1545         return ic.preedit.FindProp ('candidates', pos).to;
1546       }
1547     return ic.preedit.length;
1548   });
1549   for (var i = 0; i < 10; i++)
1550     def_predefined ("@" + i, i);
1551   predefined['@first'] = predefined['@<'];
1552   predefined['@last'] = predefined['@>'];
1553   predefined['@previous'] = predefined['@-'];
1554   predefined['@next'] = predefined['@+'];
1555   predefined['@previous-candidate-change'] = predefined['@['];
1556   predefined['@next-candidate-change'] = predefined['@]'];
1557
1558   MIM.SurroundMarker = function (name)
1559   {
1560     this.val = name;
1561     this.distance = parseInt (name.slice (1));
1562     if (isNaN (this.distance))
1563       throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1564   }
1565   MIM.SurroundMarker.prototype = new MIM.Marker ();
1566   MIM.SurroundMarker.prototype.Position = function (ic)
1567   {
1568     return ic.cursor_pos + this.distance;
1569   }
1570   MIM.SurroundMarker.prototype.CharAt = function (ic)
1571   {
1572     if (this.val == '@-0')
1573       return -1;
1574     var p = this.Position (ic);
1575     if (p < 0)
1576       return ic.GetSurroundingChar (p);
1577     else if (p >= ic.preedit.length)
1578       return ic.GetSurroundingChar (p - ic.preedit.length);
1579     return ic.preedit.charCodeAt (p);
1580   }
1581
1582   MIM.Marker.prototype.Parser = function (domain, node)
1583   {
1584     var name = node.firstChild.nodeValue;
1585     if (name.charAt (0) == '@')
1586       {
1587         var n = predefined[name];
1588         if (n)
1589           return n;
1590         if (name.charAt (1) == '-' || name.charAt (1) == '+')
1591           return new MIM.SurroundMarker (name);
1592         throw new Xex.ErrTerm (MIM.Error.ParseError,
1593                                "Invalid marker: " + name);
1594       }
1595     return new MIM.FloatingMarker (name);;
1596   }
1597 }) ();
1598
1599 MIM.Selector = function (name)
1600 {
1601   this.val = name;
1602 }
1603 MIM.Selector.prototype = new Xex.Term ('selector');
1604
1605 (function () {
1606   var selectors = {};
1607   selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1608   selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1609   selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1610   selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1611   selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1612   selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
1613   selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
1614
1615   MIM.Selector.prototype.Parser = function (domain, node)
1616   {
1617     var name = node.firstChild.nodeValue;
1618     var s = selectors[name];
1619     if (! s)
1620       throw new Xex.ErrTerm (MIM.Error.ParseError,
1621                              "Invalid selector: " + name);
1622     return s;
1623   }
1624 }) ();
1625
1626 MIM.Rule = function (keyseq, actions)
1627 {
1628   this.keyseq = keyseq;
1629   if (actions)
1630     this.actions = actions;
1631 }
1632 MIM.Rule.prototype = new Xex.Term ('rule');
1633 MIM.Rule.prototype.Parser = function (domain, node)
1634 {
1635   var n;
1636   for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1637   if (! n)
1638     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1639   var keyseq = Xex.Term.Parse (domain, n);
1640   if (keyseq.type != 'keyseq')
1641     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1642   var actions = null;
1643   n = n.nextElement ();
1644   if (n)
1645     actions = Xex.Term.Parse (domain, n, null);
1646   return new MIM.Rule (keyseq, actions);
1647 }
1648 MIM.Rule.prototype.toString = function ()
1649 {
1650   return '<rule/>';
1651 }
1652
1653 MIM.Map = function (name)
1654 {
1655   this.name = name;
1656   this.rules = new Array ();
1657 };
1658
1659 (function () {
1660   var proto = new Xex.Term ('map');
1661
1662   proto.Parser = function (domain, node)
1663   {
1664     var name = node.attributes['mname'].nodeValue;
1665     if (! name)
1666       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1667     var map = new MIM.Map (name);
1668     for (var n = node.firstChild; n; n = n.nextSibling)
1669       if (n.nodeType == 1)
1670         map.rules.push (Xex.Term.Parse (domain, n));
1671     return map;
1672   }
1673
1674   proto.toString = function ()
1675   {
1676     var str = '<map mname="' + this.name + '">';
1677     var len = this.rules.length;
1678     for (i = 0; i < len; i++)
1679       str += this.rules[i];
1680     return str + '</map>';
1681   }
1682
1683   MIM.Map.prototype = proto;
1684 }) ();
1685
1686 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1687
1688 MIM.Action = function (domain, terms)
1689 {
1690   var args = new Array ();
1691   args.push (Xex.CatchTag_.mimtag);
1692   for (var i = 0; i < terms.length; i++)
1693     args.push (terms[i]);
1694   this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1695 }
1696
1697 MIM.Action.prototype.Run = function (domain)
1698 {
1699   var result = this.action.Eval (domain);
1700   if (result.type == 'error')
1701     {
1702       domain.context.Error = result.toString ();
1703       return false;
1704     }
1705   return (result != Xex.CatchTag._mimtag);
1706 }
1707
1708 MIM.Keymap = function ()
1709 {
1710   this.name = 'TOP';
1711   this.submaps = null;
1712 };
1713
1714 (function () {
1715   var proto = {};
1716
1717   function add_rule (keymap, rule, branch_actions)
1718   {
1719     var keyseq = rule.keyseq;
1720     var len = keyseq.val.length;
1721     var name = '';
1722
1723     for (var i = 0; i < len; i++)
1724       {
1725         var key = keyseq.val[i];
1726         var sub = false;
1727
1728         name += key.key;
1729         if (! keymap.submaps)
1730           keymap.submaps = {};
1731         else
1732           sub = keymap.submaps[key.key];
1733         if (! sub)
1734           keymap.submaps[key.key] = sub = new MIM.Keymap ();
1735         keymap = sub;
1736         keymap.name = name;
1737       }
1738     keymap.map_actions = rule.actions;
1739     if (branch_actions)
1740       keymap.branch_actions = branch_actions;
1741   }
1742
1743   proto.Add = function (map, branch_actions)
1744   {
1745     var rules = map.rules;
1746     var len = rules.length;
1747
1748     for (var i = 0; i < len; i++)
1749       add_rule (this, rules[i], branch_actions);
1750   }
1751   proto.Lookup = function (keys, index)
1752   {
1753     var sub;
1754
1755     if (index < keys.val.length && this.submaps
1756         && ! keys.val[index])
1757       {
1758         Xex.Log ('invalid key at ' + index);
1759         throw 'invalid key';
1760       }
1761
1762     if (index < keys.val.length && this.submaps
1763         && (sub = this.submaps[keys.val[index].key]))
1764       {
1765         index++;
1766         return sub.Lookup (keys, index);
1767       }
1768     return { map: this, index: index };
1769   }
1770
1771   MIM.Keymap.prototype = proto;
1772 }) ();
1773
1774 MIM.State = function (name)
1775 {
1776   this.name = name;
1777   this.keymap = new MIM.Keymap ();
1778 };
1779
1780 (function () {
1781   var proto = new Xex.Term ('state');
1782
1783   proto.Parser = function (domain, node)
1784   {
1785     var map_list = domain.map_list;
1786     var name = node.attributes['sname'].nodeValue;
1787     if (! name)
1788       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1789     var state = new MIM.State (name);
1790     for (node = node.firstElement (); node; node = node.nextElement ())
1791       {
1792         if (node.nodeName == 'title')
1793           state.title = node.firstChild.nodeValue;
1794         else
1795           {
1796             var n = node.firstElement ();
1797             var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
1798             if (node.nodeName == 'branch')
1799               state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1800                                 branch_actions);
1801             else if (node.nodeName == 'state-hook')
1802               state.enter_actions = branch_actions;
1803             else if (node.nodeName == 'catch-all-branch')
1804               state.fallback_actions = branch_actions;
1805           }
1806       }
1807     return state;
1808   }
1809
1810   proto.toString = function ()
1811   {
1812     return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1813   }
1814
1815   MIM.State.prototype = proto;
1816 }) ();
1817
1818 (function () {
1819   function Block (index, term)
1820   {
1821     this.Index = index;
1822     if (term.IsStr)
1823       this.Data = term.val;
1824     else if (term.IsList)
1825       {
1826         this.Data = new Array ();
1827         for (var i = 0; i < term.val.length; i++)
1828           this.Data.push (term.val[i].val);
1829       }
1830   }
1831
1832   Block.prototype.Count = function () { return this.Data.length; }
1833   Block.prototype.get = function (i)
1834   {
1835     return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1836   }
1837
1838   MIM.Candidates = function (ic, candidates, column)
1839   {
1840     this.ic = ic;
1841     this.column = column;
1842     this.row = 0;
1843     this.index = 0;
1844     this.total = 0;
1845     this.blocks = new Array ();
1846
1847     for (var i = 0; i < candidates.length; i++)
1848       {
1849         var block = new Block (this.total, candidates[i]);
1850         this.blocks.push (block);
1851         this.total += block.Count ();
1852       }
1853   }
1854
1855   function get_col ()
1856   {
1857     return (this.column > 0 ? this.index % this.column
1858             : this.index - this.blocks[this.row].Index);
1859   }
1860
1861   function prev_group ()
1862   {
1863     var col = get_col.call (this);
1864     var nitems;
1865     if (this.column > 0)
1866       {
1867         this.index -= this.column;
1868         if (this.index >= 0)
1869           nitems = this.column;
1870         else
1871           {
1872             var lastcol = (this.total - 1) % this.column;
1873             this.index = (col < lastcol ? this.total - lastcol + col
1874                           : this.total - 1);
1875             this.row = this.blocks.length - 1;
1876             nitems = lastcol + 1;
1877           }
1878         while (this.blocks[this.row].Index > this.index)
1879           this.row--;
1880       }
1881     else
1882       {
1883         this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1884         nitems = this.blocks[this.row].Count ();
1885         this.index = (this.blocks[this.row].Index
1886                       + (col < nitems ? col : nitems - 1));
1887       }
1888     return nitems;
1889   }
1890
1891   function next_group ()
1892   {
1893     var col = get_col.call (this);
1894     var nitems;
1895     if (this.column > 0)
1896       {
1897         this.index += this.column - col;
1898         if (this.index < this.total)
1899           {
1900             if (this.index + col >= this.total)
1901               {
1902                 nitems = this.total - this.index;
1903                 this.index = this.total - 1;
1904               }
1905             else
1906               {
1907                 nitems = this.column;
1908                 this.index += col;
1909               }
1910           }
1911         else
1912           {
1913             this.index = col;
1914             this.row = 0;
1915           }
1916         while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
1917                <= this.index)
1918           this.row++;
1919       }
1920     else
1921       {
1922         this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1923         nitems = this.blocks[this.row].Count ();
1924         this.index = (this.blocks[this.row].Index
1925                       + (col < nitems ? col : nitems - 1));
1926       }
1927     return nitems;
1928   }
1929
1930   function prev ()
1931   {
1932     if (this.index == 0)
1933       {
1934         this.index = this.total - 1;
1935         this.row = this.blocks.length - 1;
1936       }
1937     else
1938       {
1939         this.index--;
1940         if (this.blocks[this.row].Index > this.index)
1941           this.row--;
1942       }
1943     }
1944
1945   function next ()
1946   {
1947     this.index++;
1948     if (this.index == this.total)
1949       {
1950         this.index = 0;
1951         this.row = 0;
1952       }
1953     else
1954       {
1955         var b = this.blocks[this.row];
1956         if (this.index == b.Index + b.Count ())
1957           this.row++;
1958       }
1959   }
1960
1961   function first ()
1962   {
1963     this.index -= get_col.call (this);
1964     while (this.blocks[this.row].Index > this.index)
1965       this.row--;
1966   }
1967
1968   function last ()
1969   {
1970     var b = this.blocks[this.row];
1971     if (this.column > 0)
1972       {
1973         if (this.index + 1 < this.total)
1974           {
1975             this.index += this.column - get_col.call (this) + 1;
1976             while (b.Index + b.Count () <= this.index)
1977               b = this.blocks[++this.row];
1978           }
1979       }
1980     else
1981       this.index = b.Index + b.Count () - 1;
1982   }
1983
1984   MIM.Candidates.prototype.Current = function ()
1985   {
1986     var b = this.blocks[this.row];
1987     return b.get (this.index - b.Index);
1988   }
1989
1990   MIM.Candidates.prototype.Select = function (selector)
1991   {
1992     var idx = this.index;
1993     var gidx = this.column > 0 ? idx / this.column : this.row;
1994     if (selector.type == 'selector')
1995       {
1996         switch (selector.val)
1997           {
1998           case '@<': first.call (this); break;
1999           case '@>': last.call (this); break;
2000           case '@-': prev.call (this); break;
2001           case '@+': next.call (this); break;
2002           case '@[': prev_group.call (this); break;
2003           case '@]': next_group.call (this); break;
2004           default: break;
2005           }
2006       }
2007     else
2008       {
2009         var col, start, end
2010         if (this.column > 0)
2011           {
2012             col = this.index % this.column;
2013             start = this.index - col;
2014             end = start + this.column;
2015           }
2016         else
2017           {
2018             start = this.blocks[this.row].Index;
2019             col = this.index - start;
2020             end = start + this.blocks[this.row].Count;
2021           }
2022         if (end > this.total)
2023           end = this.total;
2024         this.index += selector.val - col;
2025         if (this.index >= end)
2026           this.index = end - 1;
2027         if (this.column > 0)
2028           {
2029             if (selector.val > col)
2030               while (this.blocks[this.row].Index + this.blocks[this.row].Count
2031                      < this.index)
2032                 this.row++;
2033             else
2034               while (this.blocks[this.row].Index > this.index)
2035                 this.row--;
2036           }
2037       }
2038     var newgidx = this.column > 0 ? this.index / this.column : this.row;
2039     if (this.index != idx)
2040       this.ic.changed |= (gidx == newgidx
2041                           ? MIM.ChangedStatus.CandidateIndex
2042                           : MIM.ChangedStatus.CandidateList);
2043     return this.Current ();
2044   }
2045
2046   MIM.Candidates.prototype.CurrentCol = function ()
2047   {
2048     return get_col.call (this);
2049   }
2050
2051   MIM.Candidates.prototype.CurrentGroup = function ()
2052   {
2053     var col, start, end, gnum, gidx;
2054     if (this.column > 0)
2055       {
2056         gnum = Math.floor ((this.total - 1) / this.column) + 1;
2057         col = this.index % this.column;
2058         start = this.index - col;
2059         gidx = start / this.column + 1;
2060         end = start + this.column;
2061         if (end > this.total)
2062           end = this.total;
2063       }
2064     else
2065       {
2066         gnum = this.blocks.length;
2067         gidx = this.row + 1;
2068         start = this.blocks[this.row].Index;
2069         col = this.index - start;
2070         end = start + this.blocks[this.row].Count ();
2071       }
2072     var group = new Array ();
2073     var indices = new Array (gnum, gidx, col);
2074     group.push (indices);
2075     var row = this.row;
2076     var block = this.blocks[row++];
2077     while (start < end)
2078       {
2079         var c = block.get (start - block.Index);
2080         group.push (c);
2081         start++;
2082         if (start == block.Index + block.Count ())
2083           block = this.blocks[row++];
2084       }
2085     return group;
2086   }
2087 }) ();
2088
2089 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2090 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2091 MIM.im_domain.DefType (MIM.Marker.prototype);
2092 MIM.im_domain.DefType (MIM.Selector.prototype);
2093 MIM.im_domain.DefType (MIM.Rule.prototype);
2094 MIM.im_domain.DefType (MIM.Map.prototype);
2095 MIM.im_domain.DefType (MIM.State.prototype);
2096
2097 (function () {
2098   var im_domain = MIM.im_domain;
2099
2100   function Finsert (domain, vari, args)
2101   {
2102     var text;
2103     if (args[0].type == 'integer')
2104       text = String.fromCharCode (args[0].val);
2105     else
2106       text = args[0].val;
2107     domain.context.ins (text, null);
2108     return args[0];
2109   }
2110
2111   function Finsert_candidates (domain, vari, args)
2112   {
2113     var ic = domain.context;
2114     var gsize = domain.variables['candidates-group-size'];
2115     var candidates = new MIM.Candidates (ic, args,
2116                                          gsize ? gsize.val.Intval () : 0);
2117     ic.ins (candidates.Current (), candidates);
2118     return args[0];
2119   }
2120
2121   function Fdelete (domain, vari, args)
2122   {
2123     var ic = domain.context;
2124     var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2125     return new Xex.IntTerm (ic.del (pos));
2126   }
2127
2128   function Fselect (domain, vari, args)
2129   {
2130     var ic = domain.context;
2131     var can = ic.candidates;
2132
2133     if (can)
2134       {
2135         var old_text = can.Current ();
2136         var new_text = can.Select (args[0]);
2137         ic.rep (old_text, new_text, can);
2138       }
2139     else
2140       Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2141     return args[0];
2142   }
2143
2144   function Fshow (domain, vari, args)
2145   {
2146     domain.context.candidate_show = true;
2147     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2148     return Xex.nil;
2149   }
2150
2151   function Fhide (domain, vari, args)
2152   {
2153     domain.context.candidate_show = false;
2154     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2155     return Xex.nil;
2156   }
2157
2158   function Fchar_at (domain, vari, args)
2159   {
2160     return new Xex.IntTerm (args[0].CharAt (domain.context));
2161   }
2162
2163   function Fmove (domain, vari, args)
2164   {
2165     var ic = domain.context;
2166     var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2167     ic.move (pos);
2168     return new Xex.IntTerm (pos);
2169   }
2170
2171   function Fmark (domain, vari, args)
2172   {
2173     args[0].Mark (domain.context);
2174     return args[0];
2175   }
2176
2177   function Fpushback (domain, vari, args)
2178   {
2179     var a = (args[0].IsInt ? args[0].Intval ()
2180              : args[0].IsStr ? new KeySeq (args[0])
2181              : args[0]);
2182     domain.context.pushback (a);
2183     return args[0];
2184   }
2185
2186   function Fpop (domain, vari, args)
2187   {
2188     var ic = domain.context;
2189     if (ic.key_head < ic.keys.val.length)
2190       ic.keys.val.splice (ic.keys_head, 1);
2191     return Xex.nil;
2192   }
2193
2194   function Fundo  (domain, vari, args)
2195   {
2196     var ic = domain.context;
2197     var n = args.length == 0 ? -2 : args[0].val;
2198     Xex.Log ('undo with arg ' + args[0]);
2199     if (n < 0)
2200       ic.keys.val.splice (ic.keys.val.length + n, -n);
2201     else
2202       ic.keys.val.splice (n, ic.keys.val.length);
2203     ic.reset ();
2204     return Xex.nil;
2205   }
2206
2207   function Fcommit (domain, vari, args)
2208   {
2209     domain.context.commit ();
2210     return Xex.nil;
2211   }
2212
2213   function Funhandle (domain, vari, args)
2214   {
2215     domain.context.commit ();
2216     return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2217   }
2218
2219   function Fshift (domain, vari, args)
2220   {
2221     var ic = domain.context;
2222     var state_name = args[0].val;
2223     var state = ic.im.state_list[state_name];
2224     if (! state)
2225       throw ("Unknown state: " + state_name);
2226       ic.shift (state);
2227     return args[0];
2228   }
2229
2230   function Fshiftback (domain, vari, args)
2231   {
2232     domain.context.shift (null);
2233     return Xex.nil;
2234   }
2235
2236   function Fkey_count (domain, vari, args)
2237   {
2238     return new Xex.IntTerm (domain.context.key_head);
2239   }
2240
2241   function Fsurrounding_flag (domain, vari, args)
2242   {
2243     return new Xex.IntTerm (-1);
2244   }
2245
2246   im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2247   im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2248   im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2249   im_domain.DefSubr (Fselect, "select", false, 1, 1);
2250   im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2251   im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2252   im_domain.DefSubr (Fmove, "move", false, 1, 1);
2253   im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2254   im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2255   im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2256   im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2257   im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2258   im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2259   im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2260   im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2261   im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2262   im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2263   im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2264 }) ();
2265
2266
2267 (function () {
2268   function get_global_var (vname)
2269   {
2270     if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2271       MIM.im_global.Load ()
2272     return MIM.im_global.domain.variables[vname];
2273   }
2274
2275   function include (node)
2276   {
2277     node = node.firstElement ();
2278     if (node.nodeName != 'tags')
2279       return null;
2280     
2281     var lang = null, name = null, extra = null;
2282     for (node = node.firstElement (); node; node = node.nextElement ())
2283       {
2284         if (node.nodeName == 'language')
2285           lang = node.firstChild.nodeValue;
2286         else if (node.nodeName == 'name')
2287           name = node.firstChild.nodeValue;
2288         else if (node.nodeName == 'extra-id')
2289           extra = node.firstChild.nodeValue;
2290       }
2291     if (! lang || ! MIM.imlist[lang])
2292       return null;
2293     if (! extra)
2294       {
2295         if (! name || ! (im = MIM.imlist[lang][name]))
2296           return null;
2297       }
2298     else
2299       {
2300         if (! (im = MIM.imextra[lang][extra]))
2301           return null;
2302       }
2303     if (im.load_status != MIM.LoadStatus.Loaded
2304         && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2305       return null;
2306     return im;
2307   }
2308
2309   var parsers = { };
2310
2311   parsers['description'] = function (node)
2312   {
2313     this.description = node.firstChild.nodeValue;
2314   }
2315   parsers['variable-list'] = function (node)
2316   {
2317     for (node = node.firstElement (); node; node = node.nextElement ())
2318       {
2319         var vname = node.attributes['vname'].nodeValue;
2320         if (this != MIM.im_global)
2321           {
2322             var vari = get_global_var (vname);
2323             if (vari != null)
2324               this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
2325           }
2326         vname = Xex.Term.Parse (this.domain, node)
2327       }
2328   }
2329   parsers['command-list'] = function (node)
2330   {
2331   }
2332   parsers['macro-list'] = function (node)
2333   {
2334     for (var n = node.firstElement (); n; n = n.nextElement ())
2335       if (n.nodeName == 'xi:include')
2336         {
2337           var im = include (n);
2338           if (! im)
2339             alert ('inclusion fail');
2340           else
2341             for (var macro in im.domain.functions)
2342               {
2343                 var func = im.domain.functions[macro];
2344                 if (func instanceof Xex.Macro)
2345                   im.domain.CopyFunc (this.domain, macro);
2346               }
2347           n = n.previousSibling;
2348           node.removeChild (n.nextSibling);
2349         }
2350     Xex.Term.Parse (this.domain, node.firstElement (), null);
2351   }
2352   parsers['title'] = function (node)
2353   {
2354     this.title = node.firstChild.nodeValue;
2355   }
2356   parsers['map-list'] = function (node)
2357   {
2358     for (node = node.firstElement (); node; node = node.nextElement ())
2359       {
2360         if (node.nodeName == 'xi:include')
2361           {
2362             var im = include (node);
2363             if (! im)
2364               {
2365                 alert ('inclusion fail');
2366                 continue;
2367               }
2368             for (var mname in im.map_list)
2369               this.map_list[mname] = im.map_list[mname];
2370           }
2371         else
2372           {
2373             var map = Xex.Term.Parse (this.domain, node);
2374             this.map_list[map.name] = map;
2375           }
2376       }
2377   }
2378   parsers['state-list'] = function (node)
2379   {
2380     this.domain.map_list = this.map_list;
2381     for (node = node.firstElement (); node; node = node.nextElement ())
2382       {
2383         if (node.nodeName == 'xi:include')
2384           {
2385             var im = include (node);
2386             if (! im)
2387               alert ('inclusion fail');
2388             for (var sname in im.state_list)
2389               {
2390                 state = im.state_list[sname];
2391                 if (! this.initial_state)
2392                   this.initial_state = state;
2393                 this.state_list[sname] = state;
2394               }
2395           }
2396         else if (node.nodeName == 'state')
2397           {
2398             var state = Xex.Term.Parse (this.domain, node);
2399             if (! state.title)
2400               state.title = this.title;
2401             if (! this.initial_state)
2402               this.initial_state = state;
2403             this.state_list[state.name] = state;
2404           }
2405       }
2406     delete this.domain.map_list;
2407   }
2408
2409   MIM.IM = function (lang, name, extra_id, file)
2410   {
2411     this.lang = lang;
2412     this.name = name;
2413     this.extra_id = extra_id;
2414     this.file = file;
2415     this.load_status = MIM.LoadStatus.NotLoaded;
2416     this.domain = new Xex.Domain (this.lang + '-'
2417                                   + (this.name != 'nil'
2418                                      ? this.name : this.extra_id),
2419                                   MIM.im_domain, null);
2420   }
2421
2422   var proto = {
2423     Load: function ()
2424     {
2425       var node = Xex.Load (null, this.file);
2426       if (! node)
2427         {
2428           this.load_status = MIM.LoadStatus.Error;
2429           return false;
2430         }
2431       this.map_list = {};
2432       this.initial_state = null;
2433       this.state_list = {};
2434       for (node = node.firstElement (); node; node = node.nextElement ())
2435         {
2436           var name = node.nodeName;
2437           var parser = parsers[name];
2438           if (parser)
2439             parser.call (this, node);
2440         }
2441       this.load_status = MIM.LoadStatus.Loaded;
2442       return true;
2443     }
2444   }
2445
2446   MIM.IM.prototype = proto;
2447
2448   MIM.IC = function (im, target)
2449   {
2450     if (im.load_status == MIM.LoadStatus.NotLoaded)
2451       im.Load ();
2452     if (im.load_status != MIM.LoadStatus.Loaded)
2453       alert ('im:' + im.name + ' error:' + im.load_status);
2454     this.im = im;
2455     this.target = target;
2456     this.domain = new Xex.Domain ('context', im.domain, this);
2457     this.active = true;
2458     this.range = new Array ();
2459     this.range[0] = this.range[1] = 0;
2460     this.state = null;
2461     this.initial_state = this.im.initial_state;
2462     this.keys = new MIM.KeySeq ();
2463     this.marker_positions = new Array ();
2464     this.candidate_table = new MIM.CandidateTable ();
2465     this.reset ();
2466   }
2467
2468   MIM.CandidateTable = function ()
2469   {
2470     this.table = new Array ();
2471   }
2472
2473   MIM.CandidateTable.prototype.get = function (pos)
2474   {
2475     for (var i = 0; i < this.table.length; i++)
2476       {
2477         var elt = this.table[i];
2478         if (elt.from < pos && pos <= elt.to)
2479           return elt.val;
2480       }
2481   }
2482
2483   MIM.CandidateTable.prototype.put = function (from, to, candidates)
2484   {
2485     for (var i = 0; i < this.table.length; i++)
2486       {
2487         var elt = this.table[i];
2488         if (elt.from < to && elt.to > from)
2489           {
2490             elt.from = from;
2491             elt.to = to;
2492             elt.val = candidates;
2493             return;
2494           }
2495       }
2496     this.table.push ({ from: from, to: to, val: candidates });
2497   }
2498
2499   MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2500   {
2501     var diff = inserted - (to - from);
2502     if (diff == 0)
2503       return;
2504     for (var i = 0; i < this.table.length; i++)
2505       {
2506         var elt = this.table[i];
2507         if (elt.from >= to)
2508           {
2509             elt.from += diff;
2510             elt.to += diff;
2511           }
2512       }
2513   }
2514
2515   MIM.CandidateTable.prototype.clear = function ()
2516   {
2517     this.table.length = 0;
2518   }
2519
2520   function set_cursor (prefix, pos)
2521   {
2522     this.cursor_pos = pos;
2523     var candidates = this.candidate_table.get (pos);
2524     if (this.candidates != candidates)
2525       {
2526         this.candidates = candidates;
2527         this.changed |= MIM.ChangedStatus.CandidateList;
2528       }
2529   }
2530
2531   function save_state ()
2532   {
2533     this.state_var_values = this.domain.SaveValues ();
2534     this.state_preedit = this.preedit;
2535     this.state_key_head = this.key_head;
2536     this.state_pos = this.cursor_pos;
2537   }
2538
2539   function restore_state ()
2540   {
2541     this.domain.RestoreValues (this.state_var_values);
2542     this.preedit = this.state_preedit;
2543     set_cursor.call (this, "restore", this.state_pos);
2544   }
2545
2546   function handle_key ()
2547   {
2548     Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
2549              + '" in ' + this.state.name + ':' + this.keymap.name
2550              + " key/state/commit-head/len:"
2551              + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
2552     var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
2553     var sub = out.map;
2554
2555     if (out.index > this.key_head)
2556       {
2557         this.key_head = out.index;
2558         Xex.Log (' with submap', false, true);
2559         restore_state.call (this);
2560         this.keymap = sub;
2561         if (sub.map_actions)
2562           {
2563             Xex.Log ('taking map actions:');
2564             if (! this.take_actions (sub.map_actions))
2565               return false;
2566           }
2567         else if (sub.submaps)
2568           {
2569             Xex.Log ('no map actions');
2570             for (var i = this.state_key_head; i < this.key_head; i++)
2571               {
2572                 Xex.Log ('inserting key:' + this.keys.val[i].key);
2573                 this.ins (this.keys.val[i].key, null);
2574               }
2575           }
2576         if (! sub.submaps)
2577           {
2578             Xex.Log ('terminal:');
2579             if (this.keymap.branch_actions)
2580               {
2581                 Xex.Log ('branch actions:');
2582                 if (! this.take_actions (this.keymap.branch_actions))
2583                   return false;
2584               }
2585             if (sub != this.state.keymap)
2586               this.shift (this.state);
2587           }
2588       }
2589     else
2590       {
2591         Xex.Log (' without submap', false, true);
2592         this.keymap = sub;
2593         var current_state = this.state;
2594         var map = this.keymap;
2595
2596         if (map.branch_actions)
2597           {
2598             Xex.Log ('branch actions:');
2599             if (! this.take_actions (map.branch_actions))
2600               return false;
2601           }
2602
2603         if (map == this.keymap)
2604           {
2605             Xex.Log ('no state change');
2606             if (map == this.initial_state.keymap
2607                 && this.key_head < this.keys.val.length)
2608               {
2609                 Xex.Log ('unhandled');
2610                 return false;
2611               }
2612             if (this.keymap != current_state.keymap)
2613               this.shift (current_state);
2614             else if (this.keymap.actions == null)
2615               this.shift (this.initial_state);
2616           }
2617       }
2618     return true;
2619   }
2620
2621   proto = {
2622     reset: function ()
2623     {
2624       this.cursor_pos = 0;
2625       this.candidate_show = false;
2626       this.prev_state = null;
2627       this.title = this.initial_state.title;
2628       this.state_preedit = '';
2629       this.state_key_head = 0;
2630       this.state_var_values = {};
2631       this.state_pos = 0;
2632       this.key_head = 0;
2633       this.commit_key_head = 0;
2634       this.key_unhandled = false;
2635       this.unhandled_key = null;
2636       this.changed = MIM.ChangedStatus.None;
2637       this.error_message = '';
2638       this.title = this.initial_state.title;
2639       this.produced = '';
2640       this.preedit = '';
2641       this.preedit_saved = '';
2642       this.candidate_table.clear ();
2643       this.candidates = null;
2644       this.candidate_show = false;
2645       for (var elt in this.marker_positions)
2646         this.marker_positions[elt] = 0;
2647       this.shift (this.initial_state);
2648     },
2649
2650     catch_args: new Array (Xex.CatchTag._mimtag, null),
2651
2652     take_actions: function (actions)
2653     {
2654       if (actions.length == 0)
2655         return true;;
2656       var func_progn = this.domain.GetFunc ('progn');
2657       var func_catch = this.domain.GetFunc ('catch');
2658       this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2659       var term = new Xex.Funcall (func_catch, null, this.catch_args);
2660       term = term.Eval (this.domain);
2661       return (! term.IsSymbol || term.val != '@mimtag');
2662     },
2663
2664     GetSurroundingChar: function (pos)
2665     {
2666       if (pos < 0)
2667         {
2668           pos += this.range[0];
2669           if (pos < 0)
2670             return 0;
2671         }
2672       else
2673         {
2674           pos += this.range[1];
2675           if (pos >= this.target.value.length)
2676             return 0;
2677         }
2678       return this.target.value.charCodeAt (pos);
2679     },
2680     
2681     DelSurroundText: function (pos)
2682     {
2683       var text;
2684       if (pos < 0)
2685         {
2686           pos += this.range[0];
2687           if (pos <= 0)
2688             {
2689               pos = 0; text = '';
2690             }
2691           else
2692             text = this.target.value.substring (0, pos);
2693           if (this.range[0] < this.target.value.length)
2694             text += this.target.value.substring (this.range[0]);
2695           this.target.value = text;
2696           this.range[1] -= this.range[0] - pos;
2697           this.range[0] = pos;
2698         }
2699       else
2700         {
2701           pos += this.range[1];
2702           text = this.target.value.substring (0, this.range[1]);
2703           if (pos >= this.target.value.length)
2704             pos = this.target.value.length;
2705           else
2706             text += this.target.value.substring (pos);
2707           this.target.value = text;
2708         }
2709     },
2710
2711     adjust_markers: function (from, to, inserted)
2712     {
2713       var diff = inserted - (to - from);
2714
2715       for (var name in this.marker_positions)
2716         {
2717           var pos = this.marker_positions[name];
2718           if (pos > from)
2719             {
2720               if (pos >= to)
2721                 this.marker_positions[name] += diff;
2722               else if (pos > from)
2723                 this.marker_positions[name] = from;
2724             }
2725         }
2726     },
2727
2728     preedit_replace: function (from, to, text, candidates)
2729     {
2730       var newlen = text.length;
2731       this.preedit = (this.preedit.substring (0, from)
2732                       + text + this.preedit.substring (to));
2733       this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2734       this.adjust_markers (from, to, newlen);
2735       this.candidate_table.adjust (from, to, newlen);
2736       if (candidates)
2737         this.candidate_table.put (from, from + newlen, candidates)
2738       if (this.cursor_pos >= to)
2739         set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
2740       else if (this.cursor_pos > from)
2741         set_cursor.call (this, 'adjust', from)
2742     },
2743
2744     ins: function (text, candidates)
2745     {
2746       this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2747     },
2748
2749     rep: function (old_text, new_text, candidates)
2750     {
2751       this.preedit_replace (this.cursor_pos - old_text.length,
2752                             this.cursor_pos, new_text, candidates);
2753     },
2754
2755     del: function (pos)
2756     {
2757       var deleted = pos - this.cursor_pos;
2758       if (pos < this.cursor_pos)
2759         {
2760           if (pos < 0)
2761             {
2762               this.DelSurroundText (pos);
2763               deleted = - this.cursor_pos;
2764               pos = 0;
2765             }
2766           if (pos < this.cursor_pos)
2767             this.preedit_replace (pos, this.cursor_pos, '', null);
2768         }
2769       else
2770         {
2771           if (pos > this.preedit.length)
2772             {
2773               this.DelSurroundText (pos - this.preedit.length);
2774               deleted = this.preedit.length - this.cursor_pos;
2775               pos = this.preedit.length;
2776             }
2777           if (pos > this.cursor_pos)
2778             this.preedit_replace (this.cursor_pos, pos, '', null);
2779         }
2780       return deleted;
2781     },
2782
2783     show: function ()
2784     {
2785       this.candidate_show = true;
2786       this.changed |= MIM.ChangedStatus.CandidateShow;
2787     },
2788
2789     hide: function ()
2790     {
2791       this.candidate_show = false;
2792       this.changed |= MIM.ChangedStatus.CandidateShow;
2793     },
2794
2795     move: function (pos)
2796     {
2797       if (pos < 0)
2798         pos = 0;
2799       else if (pos > this.preedit.length)
2800         pos = this.preedit.length;
2801       if (pos != this.cursor_pos)
2802         {
2803           set_cursor.call (this, 'move', pos);
2804           this.changed |= MIM.ChangedStatus.Preedit;
2805         }
2806     },
2807
2808     pushback: function (n)
2809     {
2810       if (n instanceof MIM.KeySeq)
2811         {
2812           if (this.key_head > 0)
2813             this.key_head--;
2814           if (this.key_head < this.keys.val.length)
2815             this.keys.val.splice (this.key_head,
2816                                   this.keys.val.length - this.key_head);
2817           for (var i = 0; i < n.val.length; i++)
2818             this.keys.val.push (n.val[i]);
2819           return;
2820         }
2821       if (n > 0)
2822         {
2823           this.key_head -= n;
2824           if (this.key_head < 0)
2825             this.key_head = 0;
2826         }
2827       else if (n == 0)
2828         this.key_head = 0;
2829       else
2830       {
2831         this.key_head = - n;
2832         if (this.key_head > this.keys.val.length)
2833           this.key_head = this.keys.val.length;
2834       }
2835     },
2836
2837     pop: function ()
2838     {
2839       if (this.key_head < this.keys.val.length)
2840         this.keys.val.splice (this.key_head, 1);
2841     },
2842
2843     commit: function ()
2844     {
2845       if (this.preedit.length > 0)
2846       {
2847         this.candidate_table.clear ();
2848         this.produced += this.preedit;
2849         this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2850         this.preedit_saved = '';
2851         this.state_pos = 0;
2852         this.commit_key_head = this.key_head;
2853       }
2854     },
2855
2856     shift: function (state)
2857     {
2858       if (state == null)
2859         {
2860           if (this.prev_state == null)
2861             return;
2862           state = this.prev_state;
2863         }
2864
2865       if (state == this.initial_state)
2866         {
2867           if (this.state)
2868             {
2869               this.commit ();
2870               this.keys.val.splice (0, this.key_head);
2871               this.key_head = this.state_key_head = this.commit_key_head = 0;
2872               this.prev_state = null;
2873             }
2874         }
2875       else
2876         {
2877           if (state != this.state)
2878             this.prev_state = this.state;
2879         }
2880       if (state != this.state && state.enter_actions)
2881         this.take_actions (state.enter_actions);
2882       if (! this.state || this.state.title != state.title)
2883         this.changed |= MIM.ChangedStatus.StateTitle;
2884       this.state = state;
2885       this.keymap = state.keymap;
2886       save_state.call (this);
2887     },
2888
2889     Filter: function (key)
2890     {
2891       if (! this.active)
2892         {
2893           Xex.Log ("active = false");
2894           this.key_unhandled = true;
2895           this.unhandled_key = key;
2896           return false;
2897         }
2898       if (key.key == '_reload')
2899         return true;
2900       this.changed = MIM.ChangedStatus.None;
2901       this.produced = '';
2902       this.key_unhandled = false;
2903       this.keys.val.push (key);
2904       var count = 0;
2905       while (this.key_head < this.keys.val.length)
2906         {
2907           if (! handle_key.call (this))
2908             {
2909               if (this.key_head < this.keys.val.length)
2910                 {
2911                   this.unhandled_key = this.keys.val[this.key_head];
2912                   this.keys.val.splice (this.key_head, this.key_head + 1);
2913                 }
2914               if (this.state_key_head > 0)
2915                 this.state_key_head--;
2916               if (this.commit_key_head > 0)
2917                 this.commit_key_head--;
2918               this.key_unhandled = true;
2919               break;
2920             }
2921           if (++count == 10)
2922             {
2923               this.reset ();
2924               this.key_unhandled = true;
2925               break;
2926             }
2927         }
2928       if (this.keymap == this.initial_state.keymap)
2929         this.commit ();
2930
2931       if (this.commit_key_head > 0)
2932         {
2933           this.keys.val.splice (0, this.commit_key_head);
2934           this.key_head -= this.commit_key_head;
2935           this.state_key_head -= this.commit_key_head;
2936           this.commit_key_head = 0;
2937         }
2938       if (this.key_unhandled)
2939         {
2940           this.keys.val.length = 0;
2941           //this.keys.val.splice (0, this.keys.val.length);
2942           this.key_head = this.state_key_head = this.commit_key_head = 0;
2943         }
2944       if (this.changed & MIM.ChangedStatus.Candidate)
2945         {
2946           if (this.candidate_show && this.candidates)
2947             MIM.show (this);
2948           else
2949             MIM.hide (this);
2950         }
2951       return (! this.key_unhandled
2952               && this.produced.length == 0);
2953     }
2954   }
2955
2956   MIM.IC.prototype = proto;
2957
2958   var node = Xex.Load (null, "imlist.xml");
2959   for (node = node.firstChild; node; node = node.nextSibling)
2960     if (node.nodeName == 'input-method')
2961       {
2962         var lang = null, name = null, extra_id = null, file = null;
2963
2964         for (var n = node.firstChild; n; n = n.nextSibling)
2965           {
2966             if (n.nodeName == 'language')
2967               lang = n.firstChild.nodeValue;
2968             else if (n.nodeName == 'name')
2969               name = n.firstChild.nodeValue;
2970             else if (n.nodeName == 'extra-id')
2971               extra_id = n.firstChild.nodeValue;
2972             else if (n.nodeName == 'filename')
2973               file = n.firstChild.nodeValue;
2974           }
2975         if (name && name != 'nil')
2976           {
2977             if (! MIM.imlist[lang])
2978               MIM.imlist[lang] = {};
2979             MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2980           }
2981         else if (extra_id && extra_id != 'nil')
2982           {
2983             if (! MIM.imextra[lang])
2984               MIM.imextra[lang] = {};
2985             MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2986           }
2987       }
2988   if (MIM.imextra.t && MIM.imextra.t.global)
2989     MIM.im_global = MIM.imextra.t.global;
2990   else
2991     {
2992       MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2993       MIM.im_global.load_status = MIM.LoadStatus.Error;
2994     }
2995   node = undefined;
2996 }) ();
2997
2998 (function () {
2999   var keys = new Array ();
3000   keys[0x09] = 'tab';
3001   keys[0x08] = 'backspace';
3002   keys[0x0D] = 'return';
3003   keys[0x1B] = 'escape';
3004   keys[0x20] = 'space';
3005   keys[0x21] = 'pageup';
3006   keys[0x22] = 'pagedown';
3007   keys[0x23] = 'end';
3008   keys[0x24] = 'home';
3009   keys[0x25] = 'left';
3010   keys[0x26] = 'up';
3011   keys[0x27] = 'right';
3012   keys[0x28] = 'down';
3013   keys[0x2D] = 'insert';
3014   keys[0x2E] = 'delete';
3015   for (var i = 1; i <= 12; i++)
3016     keys[111 + i] = "f" + i;
3017   keys[0x90] = "numlock";
3018   keys[0xF0] = "capslock";
3019
3020   var keyids = {};
3021   keyids['U+0008'] = 'Backspace';
3022   keyids['U+0009'] = 'Tab';
3023   keyids['U+0018'] = 'Cancel';
3024   keyids['U+001B'] = 'Escape';
3025   keyids['U+0020'] = 'Space';
3026   keyids['U+007F'] = 'Delete';
3027
3028   var modifiers = {}
3029   modifiers.Shift = 1;
3030   modifiers.Control = 1;
3031   modifiers.Alt = 1;
3032   modifiers.AltGraph = 1;
3033   modifiers.Meta = 1
3034
3035   MIM.decode_key_event = function (event)
3036   {
3037     var key = event.keyIdentifier;
3038
3039     if (key)                    // keydown event of Chrome
3040       {
3041         if (modifiers[key])
3042           return false;
3043         var mod = '';
3044         if (event.ctrlKey) mod += 'C-';
3045         if (event.metaKey) mod += 'M-';
3046         if (event.altKey) mod += 'A-';
3047         var keysym = keyids[key];
3048         if (keysym)
3049           key = keysym;
3050         else if (key.match(/^U\+([0-9A-Z]+)$/))
3051           {
3052             if (mod.length == 0)
3053               return false;
3054             key = String.fromCharCode (parseInt (RegExp.$1, 16));
3055           }
3056         //else
3057         //key = key.toLowerCase ();
3058         if (event.shiftKey) mod += 'S-';
3059         return new MIM.Key (mod + key);
3060       }
3061     else
3062       {
3063         key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
3064                : event.charCode ? event.charCode
3065                : false);
3066         if (! key)
3067           return false;
3068         if (event.type == 'keydown')
3069           {
3070             key = keys[key];
3071             if (! key)
3072               return false;
3073             if (event.shiftKey) key = "S-" + key ;
3074           }
3075         else
3076           key = String.fromCharCode (key);
3077       }
3078     if (event.altKey) key = "A-" + key ;
3079     if (event.ctrlKey) key = "C-" + key ;
3080     return new MIM.Key (key);
3081   }
3082 }) ();
3083
3084 MIM.add_event_listener
3085   = (window.addEventListener
3086      ? function (target, type, listener) {
3087        target.addEventListener (type, listener, false);
3088      }
3089      : window.attachEvent
3090      ? function (target, type, listener) {
3091        target.attachEvent ('on' + type,
3092                            function() {
3093                              listener.call (target, window.event);
3094                            });
3095      }
3096      : function (target, type, listener) {
3097        target['on' + type]
3098          = function (e) { listener.call (target, e || window.event); };
3099      });
3100
3101 MIM.debug_print = function (event, ic)
3102 {
3103   if (! MIM.debug)
3104     return;
3105   if (! MIM.debug_nodes)
3106     {
3107       MIM.debug_nodes = new Array ();
3108       MIM.debug_nodes['status0'] = document.getElementById ('status0');
3109       MIM.debug_nodes['status1'] = document.getElementById ('status1');
3110       MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3111       MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3112       MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3113       MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3114       MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3115       MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3116     }
3117   var target = event.target;
3118   var code = event.keyCode;
3119   var ch = event.type == 'keypress' ? event.charCode : 0;
3120   var key = MIM.decode_key_event (event);
3121   var index;
3122
3123   MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3124   index = (event.type == 'keydown' ? '0' : '1');
3125   if (ic)
3126     MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3127   else
3128     MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3129   MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3130   MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3131   if (index == 0)
3132     {
3133       MIM.debug_nodes.keypress.innerHTML = '';
3134       MIM.debug_nodes.status1.innerHTML = '';
3135       MIM.debug_nodes.keymap1.innerHTML = '';
3136       MIM.debug_nodes.preedit1.innerHTML = ''
3137     }
3138 };
3139
3140 MIM.get_range = function (target, ic)
3141 {
3142   var from, to;
3143   if (target.selectionStart != null) // for Mozilla
3144     {
3145       from = target.selectionStart;
3146       to = target.selectionEnd;
3147     }
3148   else                          // for IE
3149     {
3150       var r = document.selection.createRange ();
3151       var rr = r.duplicate ();
3152
3153       rr.moveToElementText (target);
3154       rr.setEndPoint ('EndToEnd', range);
3155       from = rr.text.length - r.text.length;
3156       to = rr.text.length;
3157     }
3158   if (ic.range[0] == from && ic.range[1] == to
3159       && (to == from || target.value.substring (from, to) == ic.preedit))
3160     return true;
3161   ic.range[0] = from;
3162   ic.range[1] = to;
3163   return false;
3164 }
3165
3166 MIM.set_caret = function (target, ic)
3167 {
3168   if (target.setSelectionRange) // Mozilla
3169     {
3170       var scrollTop = target.scrollTop;
3171       target.setSelectionRange (ic.range[0], ic.range[1]);
3172       target.scrollTop = scrollTop;
3173     }
3174   else                          // IE
3175     {
3176       var range = target.createTextRange ();
3177       range.moveStart ('character', ic.range[0]);
3178       range.moveEnd ('character', ic.range[1]);
3179       range.select ();
3180     }
3181 };
3182
3183 MIM.update = function (target, ic)
3184 {
3185   var text = target.value;
3186   target.value = (text.substring (0, ic.range[0])
3187                   + ic.produced
3188                   + ic.preedit
3189                   + text.substring (ic.range[1]));
3190   ic.range[0] += ic.produced.length;
3191   ic.range[1] = ic.range[0] + ic.preedit.length;
3192   MIM.set_caret (target, ic);
3193 };
3194
3195 (function () {
3196   var style_props = {
3197     width: 'width',
3198     height: 'height',
3199     padingLeft: 'padding-left',
3200     paddingRight: 'padding-right',
3201     paddingTop: 'padding-top',
3202     paddintBottom: 'padding-bottom', 
3203     borderLeftStyle: 'border-left-style',
3204     borderRightStyle: 'border-right-style',
3205     borderTopStyle: 'border-top-style',
3206     borderBottomStyle: 'border-bottom-style',
3207     borderLeftWidth: 'border-left-width',
3208     borderRightWidth: 'border-right-width',
3209     borderTopWidth: 'border-top-width',
3210     borderBottomWidth: 'border-bottom-width',
3211     fontFamily: 'font-family',
3212     fontSize: 'font-size',
3213     lineHeight: 'line-height',
3214     letterSpacing: 'letter-spacing',
3215     wordSpacing: 'word-spacing' };
3216
3217   function copy_style (from, to)
3218   {
3219     var from_style = getComputedStyle(from,'');
3220     for(var name in style_props)
3221       to.style[name] = from_style.getPropertyValue (style_props[name]);
3222     to.style.left = from.offsetLeft + 'px'; 
3223     to.style.top = from.offsetTop + 'px';
3224     to.style.width = from.offsetWidth;
3225     to.style.height = from.offsetHeight;
3226   }
3227
3228   MIM.show = function (ic)
3229   {
3230     if (! ic.candidates)
3231       return;
3232     var target = ic.target;
3233     if (! ic.div_node)
3234       {
3235         ic.target_top = 0;
3236         ic.target_left = 0;
3237         for (var elm = ic.target.offsetParent; elm; elm = elm.offsetParent)
3238           {
3239             ic.target_top += elm.offsetTop;
3240             ic.target_left += elm.offsetLeft;
3241           }
3242         ic.div_node = document.createElement ('div');
3243         copy_style (target, ic.div_node);
3244         ic.div_node.style.visibility="hidden";
3245         ic.div_node.style.position = "absolute";
3246         document.getElementsByTagName ('body')[0].appendChild (ic.div_node);
3247         ic.div_node_first = document.createElement ('span');
3248         ic.div_node_last = document.createElement('span');
3249         ic.div_node_last.innerHTML = '.';
3250         ic.div_node.appendChild (ic.div_node_first);
3251         ic.div_node.appendChild (ic.div_node_last);
3252         ic.can_node = document.createElement ('table');
3253         ic.can_node.style.position = 'absolute';
3254         ic.can_node.style.display = 'none';
3255         ic.can_node.style.backgroundColor = "white";
3256         ic.can_node.style.border = "1px solid black";
3257         document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
3258       }
3259
3260     if (ic.changed & MIM.ChangedStatus.CandidateList)
3261       {
3262         while (ic.can_node.childNodes.length > 0)
3263           ic.can_node.removeChild (ic.can_node.firstChild);
3264         var tr = document.createElement ('tr');
3265         var group = ic.candidates.CurrentGroup ();
3266         var td = document.createElement ('td');
3267         td.innerHTML = group[0][1] + '/' + group[0][0];
3268         td.style.color = 'white';
3269         td.style.backgroundColor = 'black';
3270         tr.appendChild (td);
3271         for (var i = 1; i < group.length; i++)
3272           {
3273             var td = document.createElement ('td');
3274             td.noWrap = true;
3275             td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
3276             if (i == group[0][2] + 1)
3277               td.style.backgroundColor = 'lightblue';
3278             tr.appendChild (td);
3279           }
3280         ic.can_node.appendChild (tr);
3281         ic.div_node_first.innerHTML = target.value.substr (0, ic.range[0]);
3282         var x = ic.target_left + ic.div_node.lastChild.offsetLeft;
3283         var y = (ic.target_top + ic.div_node.lastChild.offsetTop
3284                  + ic.div_node.lastChild.offsetHeight - target.scrollTop + 10);
3285         ic.can_node.style.left = x + 'px';
3286         ic.can_node.style.top = y + 'px';
3287       }
3288     else
3289       {
3290         var td = ic.can_node.firstElement ().firstElement ().nextElement ();
3291         var col = ic.candidates.CurrentCol ();
3292         for (var i = 0; td; td = td.nextElement ())
3293           td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
3294       }
3295     ic.can_node.style.display = 'block';
3296   }
3297
3298   MIM.hide = function (ic)
3299   {
3300     if (ic.can_node)
3301       ic.can_node.style.display = 'none';
3302   }
3303 }) ();
3304
3305 MIM.focus_in = function (event)
3306 {
3307   var target = event.target;
3308   var ic = target.mim_ic;
3309   if (ic.wait_update == true)
3310     {
3311       Xex.Log ("Focus in " + target.tagName + ' IGNORED');
3312       event.preventDefault ();
3313       return false;
3314     }
3315   Xex.Log ("Focus in " + target.tagName);
3316   ic.Filter (MIM.Key.FocusIn);
3317   MIM.update (target, ic);
3318 }
3319
3320 MIM.focus_out = function (event)
3321 {
3322   var target = event.target;
3323   var ic = target.mim_ic;
3324   function reset_update () { ic.wait_update = false; };
3325   if (ic.wait_update == true)
3326     {
3327       Xex.Log ("Focus out " + target.tagName + ' IGNORED');
3328       event.preventDefault ();
3329       return false;
3330     }
3331   Xex.Log ("Focus out " + target.tagName);
3332   ic.Filter (MIM.Key.FocusOut);
3333   ic.wait_update = true;
3334   MIM.update (target, ic, true);
3335   setTimeout (reset_update, 1000);
3336 };
3337
3338 MIM.keydown = function (event)
3339 {
3340   var target = event.target;
3341   if (target.id == 'log')
3342     return;
3343   if (! (target.type == "text" || target.type == "textarea"))
3344     return;
3345
3346   var ic = target.mim_ic;
3347   if (! ic || ic.im != MIM.current)
3348     {
3349       target.mim_ic = null;
3350       Xex.Log ('creating IC');
3351       ic = new MIM.IC (MIM.current, target);
3352       if (ic.im.load_status != MIM.LoadStatus.Loaded)
3353         return;
3354       target.mim_ic = ic;
3355       MIM.add_event_listener (target, 'focus', MIM.focus_in);
3356       MIM.add_event_listener (target, 'blur', MIM.focus_out);
3357       MIM.get_range (target, ic)
3358     }
3359   else
3360     {
3361       if (! MIM.get_range (target, ic))
3362         ic.reset ();
3363     }
3364   MIM.debug_print (event, ic);
3365   ic.key = MIM.decode_key_event (event);
3366   if (ic.key)
3367     {
3368       try {
3369         var result = ic.Filter (ic.key);
3370       } catch (e) {
3371         Xex.Log ('Error' + e);
3372         throw (e);
3373       }
3374       MIM.update (target, ic);
3375       if (! ic.key_unhandled)
3376         event.preventDefault ();
3377     }
3378 };
3379
3380 MIM.keypress = function (event)
3381 {
3382   var target = event.target;
3383   if (target.id == 'log')
3384     return;
3385   if (! (target.type == "text" || target.type == "textarea"))
3386     return;
3387
3388   var ic = target.mim_ic;
3389   var i;
3390
3391   try {
3392     if (ic.im.load_status != MIM.LoadStatus.Loaded)
3393       return;
3394     if (! ic.key)
3395       ic.key = MIM.decode_key_event (event);
3396     if (! ic.key)
3397       {
3398         ic.reset ();
3399         return;
3400       }
3401     
3402     try {
3403       var result = ic.Filter (ic.key);
3404     } catch (e) {
3405       Xex.Log ('Error:' + e);
3406       throw (e);
3407     }
3408     MIM.update (target, ic);
3409     if (! ic.key_unhandled)
3410       event.preventDefault ();
3411   } catch (e) {
3412     Xex.Log ("error:" + e);
3413     event.preventDefault ();
3414   } finally {
3415     MIM.debug_print (event, ic);
3416   }
3417
3418   return;
3419 };
3420
3421 (function () {
3422   var lang_category = {
3423     European: {
3424       cs: { name: 'Czech' },
3425       da: { name: 'Danish' },
3426       el: { name: 'Greek' },
3427       en: { name: 'English' },
3428       eo: { name: 'Esperanto' },
3429       fr: { name: 'French' },
3430       grc: { name: 'ClassicGreek' },
3431       hr: { name: 'Croatian' },
3432       hy: { name: 'Armenian' },
3433       ka: { name: 'Georgian' },
3434       kk: { name: 'Kazakh' },
3435       ru: { name: 'Russian' },
3436       sk: { name: 'Slovak' },
3437       sr: { name: 'Serbian' },
3438       sv: { name: 'Swedish' },
3439       yi: { name: 'Yiddish' } },
3440     MiddleEast: {
3441       ar: { name: 'Arabic' },
3442       dv: { name: 'Divehi' },
3443       fa: { name: 'Persian' },
3444       he: { name: 'Hebrew' },
3445       kk: { name: 'Kazakh' },
3446       ps: { name: 'Pushto' },
3447       ug: { name: 'Uighur' },
3448       yi: { name: 'Yiddish' } },
3449     SouthAsia: {
3450       as: { name: 'Assamese' },
3451       bn: { name: 'Bengali' },
3452       bo: { name: 'Tibetan' },
3453       gu: { name: 'Gujarati' },
3454       hi: { name: 'Hindi' },
3455       kn: { name: 'Kannada' },
3456       ks: { name: 'Kashmiri' },
3457       ml: { name: 'Malayalam' },
3458       mr: { name: 'Marathi' },
3459       ne: { name: 'Nepali' },
3460       or: { name: 'Oriya' },
3461       pa: { name: 'Panjabi' },
3462       sa: { name: 'Sanskirit' },
3463       sd: { name: 'Sindhi' },
3464       si: { name: 'Sinhalese' },
3465       ta: { name: 'Tamil' },
3466       te: { name: 'Telugu' },
3467       ur: { name: 'Urdu' } },
3468     SouthEastAsia: {
3469       cmc: { name: 'Cham' },
3470       km: { name: 'Khmer'},
3471       lo: { name: 'Lao' },
3472       my: { name: 'Burmese' },
3473       tai: { name: 'Tai Viet' },
3474       th: { name: 'Thai' },
3475       vi: { name: 'Vietanamese' } },
3476     EastAsia: {
3477       ii: { name: 'Yii' },
3478       ja: { name: 'Japanese' },
3479       ko: { name: 'Korean' },
3480       zh: { name: 'Chinese' } },
3481     Other: {
3482       am: { name:  'Amharic' },
3483       ath: { name: 'Carrier' },
3484       bla: { name: 'Blackfoot' },
3485       cr: { name: 'Cree' },
3486       eo: { name: 'Esperanto' },
3487       iu: { name: 'Inuktitut' },
3488       nsk: { name: 'Naskapi' },
3489       oj: { name: 'Ojibwe' },
3490       t: { name: 'Generic' } }
3491   };
3492
3493   function categorize_im ()
3494   {
3495     var cat, lang, list, name;
3496     for (lang in MIM.imlist)
3497       {
3498         list = null;
3499         for (cat in lang_category)
3500           if (lang_category[cat][lang])
3501             {
3502               list = lang_category[cat][lang].list;
3503               if (! list)
3504                 list = lang_category[cat][lang].list = {};
3505               for (name in MIM.imlist[lang])
3506                 list[name] = MIM.imlist[lang][name];
3507             }
3508         if (! list)
3509           for (name in MIM.imlist[lang])
3510             Xex.Log ('no category ' + lang + '-' + name);
3511       }
3512   }
3513
3514   var destroy_timer;
3515   var last_target;
3516
3517   function destroy ()
3518   {
3519     clearTimeout (destroy_timer);
3520     destroy_timer = null;
3521     var target = document.getElementById ('mim-menu');
3522     if (target)
3523       {
3524         for (; last_target && last_target.menu_level;
3525              last_target = last_target.parentLi)
3526           last_target.style.backgroundColor = 'white';
3527         var nodes = target.getElementsByTagName ('ul');
3528         for (var i = 0; i < nodes.length; i++)
3529           nodes[i].style.visibility = 'hidden';
3530         document.getElementsByTagName ('body')[0].removeChild (target);
3531       }
3532   }    
3533
3534   function destroy_menu () {
3535     if (! destroy_timer)
3536       destroy_timer = setTimeout (destroy, 1000);
3537   }
3538
3539   function show_submenu (event)
3540   {
3541     if (destroy_timer)
3542       {
3543         clearTimeout (destroy_timer);
3544         destroy_timer = null;
3545       }
3546     var target = event.target;
3547     if (! target.menu_level)
3548       return;
3549     if (last_target && target.parentLi != last_target)
3550       {
3551         last_target.style.backgroundColor = 'white';
3552         if (target.menu_level < last_target.menu_level)
3553           {
3554             last_target = last_target.parentLi;
3555             last_target.style.backgroundColor = 'white';
3556           }
3557         var uls = last_target.getElementsByTagName ('ul');
3558         for (var i = 0; i < uls.length; i++)
3559           uls[i].style.visibility = 'hidden';
3560       }
3561     last_target = target;
3562     target.style.backgroundColor = 'yellow';
3563     if (target.menu_level < 3)
3564       {
3565         target.lastChild.style.visibility = 'visible';
3566         target.lastChild.style.left = target.clientWidth + 'px';
3567       }
3568     event.preventDefault ();    
3569   }
3570
3571   function select_im (event)
3572   {
3573     var target = event.target;
3574     if (target.im)
3575       {
3576         MIM.current = target.im;
3577         destroy ();
3578       }
3579     event.preventDefault ();
3580   }
3581
3582   function create_ul (visibility)
3583   {
3584     var ul = document.createElement ('ul');
3585     ul.style.position = 'absolute';
3586     ul.style.margin = '0px';
3587     ul.style.padding = '0px';
3588     ul.style.border = '1px solid gray';
3589     ul.style.borderBottom = 'none';
3590     ul.style.top = '-1px';
3591     ul.style.backgroundColor = 'white';
3592     ul.style.visibility = visibility;
3593     return ul;
3594   }
3595
3596   function create_li (level, text)
3597   {
3598     var li = document.createElement ('li');
3599     li.style.position = 'relative';
3600     li.style.margin = '0px';
3601     li.style.padding = '1px';
3602     li.style.borderBottom = '1px solid gray';
3603     li.style.top = '0px';
3604     li.style.listStyle = 'none';
3605     li.menu_level = level;
3606     var nobr = document.createElement ('nobr');
3607     nobr.innerHTML = text;
3608     li.appendChild (nobr);
3609     return li;
3610   }
3611
3612   var menu;
3613
3614   function create_menu (event)
3615   {
3616     var target = event.target;
3617
3618     if (! ((target.type == "text" || target.type == "textarea")
3619            && event.which == 1 && event.ctrlKey))
3620       return;
3621     if (! menu)
3622       {
3623         categorize_im ();
3624         menu = create_ul ('visible');
3625         menu.style.fontFamily = 'sans-serif';
3626         menu.style.fontWeight = 'bold';
3627         menu.id = 'mim-menu';
3628         menu.onclick = select_im;
3629         menu.onmouseover = show_submenu;
3630         menu.onmouseout = destroy_menu;
3631         for (var catname in lang_category)
3632           {
3633             var cat = lang_category[catname];
3634             var li = create_li (1, catname);
3635             var sub = create_ul ('hidden');
3636             for (var langname in cat)
3637               {
3638                 var lang = cat[langname];
3639                 if (! lang.list)
3640                   continue;
3641                 var sub_li = create_li (2, lang.name);
3642                 sub_li.parentLi = li;
3643                 var subsub = create_ul ('hidden');
3644                 for (var name in lang.list)
3645                   {
3646                     var im = lang.list[name];
3647                     var subsub_li = create_li (3, im.name);
3648                     subsub_li.parentLi = sub_li;
3649                     subsub_li.im = im;
3650                     subsub.appendChild (subsub_li);
3651                   }
3652                 sub_li.appendChild (subsub);
3653                 sub.appendChild (sub_li);
3654               }
3655             li.appendChild (sub);
3656             menu.appendChild (li);
3657           }
3658         lang_category = null;
3659       }
3660     menu.style.left = (event.clientX - 10) + "px";
3661     menu.style.top = (event.clientY - 10) + "px";
3662     document.getElementsByTagName ('body')[0].appendChild (menu);
3663   };
3664
3665   MIM.init = function ()
3666   {
3667     MIM.add_event_listener (window, 'keydown', MIM.keydown);
3668     MIM.add_event_listener (window, 'keypress', MIM.keypress);
3669     MIM.add_event_listener (window, 'mousedown', create_menu);
3670     if (window.location == 'http://localhost/mim/index.html')
3671       MIM.server = 'http://localhost/mim';
3672     MIM.current = MIM.imlist['zh']['py-gb'];
3673   };
3674 }) ();
3675
3676 MIM.test = function ()
3677 {
3678   var im = MIM.imlist['t']['latn-post'];
3679   var ic = new MIM.IC (im, null);
3680
3681   ic.Filter (new MIM.Key ('a'));
3682   ic.Filter (new MIM.Key ("'"));
3683
3684   if (true)
3685     document.getElementById ('text').value = ic.produced + ic.preedit;
3686   else {
3687     try {
3688       document.getElementById ('text').value
3689         = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3690     } catch (e) {
3691       if (e instanceof Xex.ErrTerm)
3692         alert (e);
3693       throw e;
3694     }
3695   }
3696 }
3697
3698
3699 MIM.init_debug = function ()
3700 {
3701   MIM.debug = true;
3702   Xex.LogNode = document.getElementById ('log');
3703   Xex.Log (null);
3704   MIM.init ();
3705 };