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