*** 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) : null;
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 ? 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 Fnot (domain, vari, args)
875   {
876     return (args[0].IsTrue () ? Xex.Zero : Xex.One);
877   }
878
879   function maybe_set_intvar (vari, n)
880   {
881     var term = new Xex.IntTerm (n);
882     if (vari)
883       vari.SetValue (term);
884     return term;
885   }
886
887   function Fadd (domain, vari, args)
888   {
889     var n = vari ? vari.val.Intval () : 0;
890     var len = args.length;
891
892     for (var i = 0; i < len; i++)
893       n += args[i].Intval ();
894     return maybe_set_intvar (vari, n);
895   }
896
897   function Fmul (domain, vari, args)
898   {
899     var n = vari ? vari.val.Intval () : 1;
900     for (var i = 0; i < args.length; i++)
901       n *= arg.Intval ();
902     return maybe_set_intvar (vari, n);
903   }
904
905   function Fsub (domain, vari, args)
906   {
907     var n, i;
908
909     if (! vari)
910       {
911         n = args[0].Intval ();
912         i = 1;
913       }
914     else
915       {
916         n = vari.val.Intval ();
917         i = 0;
918       }
919     while (i < args.length)
920       n -= args[i++].Intval ();
921     return maybe_set_intvar (vari, n);
922   }
923
924   function Fdiv (domain, vari, args)
925   {
926     var n, i;
927
928     if (! vari == null)
929       {
930         n = args[0].Intval ();
931         i = 1;
932       }
933     else
934       {
935         n = vari.val.Intval ();
936         i = 0;
937       }
938     while (i < args.length)
939       n /= args[i++].Intval ();
940     return maybe_set_intvar (vari, n);
941   }
942
943   function Fmod (domain, vari, args)
944   {
945     return maybe_set_intvar (vari, args[0].Intval () % args[1].Intval ());
946   }
947
948   function Flogior (domain, vari, args)
949   {
950     var n = vari == null ? 0 : vari.val;
951     for (var i = 0; i < args.length; i++)
952       n |= args[i].val;
953     return maybe_set_intvar (vari, n);
954   }
955
956   function Flogand (domain, vari, args)
957   {
958     var n, i;
959     if (vari == null)
960       {
961         Xex.Log ("logand arg args[0]" + args[0]);
962         n = args[0].Intval ()
963         i = 1;
964       }
965     else
966       {
967         Xex.Log ("logand arg var " + vari);
968         n = vari.val.Intval ();
969         i = 0;
970       }
971     while (n > 0 && i < args.length)
972       {
973         Xex.Log ("logand arg " + args[i]);
974         n &= args[i++].val;
975       }
976     return maybe_set_intvar (vari, n);
977   }
978
979   function Flsh (domain, vari, args)
980   {
981     return maybe_set_intvar (vari, args[0].Intval () << args[1].Intval ());
982   }
983
984   function Frsh (domain, vari, args)
985   {
986     return maybe_set_intvar (vari, args[0].Intval () >> args[1].Intval ());
987   }
988
989   function Fand (domain, vari, args)
990   {
991     var len = args.length;
992     for (var i = 0; i < len; i++)
993     {
994       var result = args[i].Eval (domain);
995       if (domain.Thrown ())
996         return result;
997       if (! result.IsTrue ())
998         return Xex.Zero;
999     }
1000     return Xex.One;
1001   }
1002
1003   function For (domain, vari, args)
1004   {
1005     var len = args.length;
1006     for (var i = 0; i < len; i++)
1007     {
1008       var result = args[i].Eval (domain);
1009       if (domain.Thrown ())
1010         return result;
1011       if (result.IsTrue ())
1012         return Xex.One;
1013     }
1014     return Xex.Zero;
1015   }
1016
1017   function Feq (domain, vari, args)
1018   {
1019     for (var i = 1; i < args.length; i++)
1020       if (! args[i - 1].Equals (args[i]))
1021         return Xex.Zero;
1022     return Xex.One;
1023   }
1024
1025   function Fnoteq (domain, vari, args)
1026   {
1027     return (Feq (domain, vari, args) == Xex.One ? Xex.Zero : Xex.One);
1028   }
1029
1030   function Flt (domain, vari, args)
1031   {
1032     var n = args[0].Intval ();
1033
1034     for (var i = 1; i < args.length; i++)
1035       {
1036         var n1 = args[i].Intval ();
1037         if (n >= n1)
1038           return Xex.Zero;
1039         n = n1;
1040       }
1041     return Xex.One;
1042   }
1043
1044   function Fle (domain, vari, args)
1045   {
1046     var n = args[0].Intval ();
1047     for (var i = 1; i < args.length; i++)
1048       {
1049         var n1 = args[i].Intval ();
1050         if (n > n1)
1051           return Xex.Zero;
1052         n = n1;
1053       }
1054     return Xex.One;
1055   }
1056
1057   function Fgt (domain, vari, args)
1058   {
1059     var n = args[0].Intval ();
1060     for (var i = 1; i < args.length; i++)
1061       {
1062         var n1 = args[i].Intval ();
1063         if (n <= n1)
1064           return Xex.Zero;
1065         n = n1;
1066       }
1067     return Xex.One;
1068   }
1069
1070   function Fge (domain, vari, args)
1071   {
1072     var n = args[0].Intval ();
1073     for (var i = 1; i < args.Length; i++)
1074       {
1075         var n1 = args[i].Intval ();
1076         if (n < n1)
1077           return Xex.Zero;
1078         n = n1;
1079       }
1080     return Xex.One;
1081   }
1082
1083   function Fprogn (domain, vari, args)
1084   {
1085     var result = Xex.One;
1086     var len = args.length;
1087
1088     for (var i = 0; i < len; i++)
1089       {
1090         result = args[i].Eval (domain);
1091         if (domain.Thrown ())
1092           return result;
1093       }
1094     return result;
1095   }
1096
1097   function Fif (domain, vari, args)
1098   {
1099     var result = args[0].Eval (domain);
1100
1101     if (domain.Thrown ())
1102       return result;
1103     if (result.IsTrue ())
1104       return args[1].Eval (domain);
1105     if (args.length == 2)
1106       return Zero;
1107     return args[2].Eval (domain);
1108   }
1109
1110   function Fcond (domain, vari, args)
1111   {
1112     for (var i = 0; i < args.length; i++)
1113       {
1114         var list = args[i];
1115         var result = list.val[0].Eval (domain);
1116         if (result.IsTrue ())
1117           {
1118             for (var j = 1; j < list.val.length; j++)
1119               {
1120                 domain.depth++;
1121                 result = list.val[j].Eval (domain);
1122                 domain.depth--;
1123                 if (domain.Thrown ())
1124                   return result;
1125                 }
1126             return result;
1127           }
1128       }
1129     return Xex.Zero;
1130   }
1131
1132   function eval_terms (domain, terms, idx)
1133   {
1134     var result = Xex.Zero;
1135     domain.caught = false;
1136     for (var i = idx; i < terms.length; i++)
1137       {
1138         result = terms[i].Eval (domain);
1139         if (domain.Thrown ())
1140           return result;
1141       }
1142     return result;
1143   }
1144
1145   function Fcatch (domain, vari, args)
1146   {
1147     var caught = false;
1148     var result;
1149
1150     if (args[0].IsError)
1151       {
1152         try {
1153           result = eval_terms (domain, args, 1);
1154         } catch (e) {
1155           if (e instanceof Xex.ErrTerm)
1156             {
1157               if (! args[0].Matches (e))
1158                 throw e;
1159               if (vari)
1160                 vari.SetValue (e);
1161               return Xex.One;
1162             }
1163         }
1164       }
1165     else if (args[0].IsSymbol)
1166       {
1167         try {
1168           domain.Catch (args[0].val);
1169           result = eval_terms (domain, args, 1);
1170           if (domain.caught)
1171             {
1172               if (vari != null)
1173                 vari.SetValue (result);
1174               return Xex.One;
1175             }
1176           return Xex.Zero;
1177         } finally {
1178           domain.Uncatch ();
1179         }
1180       }
1181     throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1182                            "Not a symbol nor an error: " + args[0]);
1183   }
1184
1185   function Fthrow (domain, vari, args)
1186   {
1187     if (args[0].IsSymbl)
1188       {
1189         domain.ThrowSymbol (args[0]);
1190         return (args[args.length - 1]);
1191       }
1192     if (args[0].IsError)
1193       {
1194         throw args[0];
1195       }
1196     throw new Xex.ErrTerm (Xex.Error.WrongArgument,
1197                            "Not a symbol nor an error:" + args[0]);
1198   }
1199
1200   Xex.BasicDomain = basic;
1201
1202   basic.DefSubr (Fset, "set", true, 1, 1);
1203   basic.DefAlias ("=", "set");
1204   basic.DefSubr (Fnot, "not", false, 1, 1);
1205   basic.DefAlias ("!", "not");
1206   basic.DefSubr (Fadd, "add", true, 1, -1);
1207   basic.DefSubr (Fmul, "mul", true, 1, -1);
1208   basic.DefAlias ("*", "mul");
1209   basic.DefSubr (Fsub, "sub", true, 1, -1);
1210   basic.DefAlias ("-", "sub");
1211   basic.DefSubr (Fdiv, "div", true, 1, -1);
1212   basic.DefAlias ("/", "div");
1213   basic.DefSubr (Fmod, "mod", true, 1, 2);
1214   basic.DefAlias ("%", "mod");
1215   basic.DefSubr (Flogior, "logior", true, 1, -1);
1216   basic.DefAlias ('|', "logior");
1217   basic.DefSubr (Flogand, "logand", true, 1, -1);
1218   basic.DefAlias ("&", "logand");
1219   basic.DefSubr (Flsh, "lsh", true, 1, 2);
1220   basic.DefAlias ("<<", "lsh");
1221   basic.DefSubr (Frsh, "rsh", true, 1, 2);
1222   basic.DefAlias (">>", "rsh");
1223   basic.DefSubr (Feq, "eq", false, 2, -1);
1224   basic.DefAlias ("==", "eq");
1225   basic.DefSubr (Fnoteq, "noteq", false, 2, 2);
1226   basic.DefAlias ("!=", "noteq");
1227   basic.DefSubr (Flt, "lt", false, 2, -1);
1228   basic.DefAlias ("<", "lt");
1229   basic.DefSubr (Fle, "le", false, 2, -1);
1230   basic.DefAlias ("<=", "le");
1231   basic.DefSubr (Fgt, "gt", false, 2, -1);
1232   basic.DefAlias (">", "gt");
1233   basic.DefSubr (Fge, "ge", false, 2, -1);
1234   basic.DefAlias (">=", "ge");
1235   basic.DefSubr (Fthrow, "throw", false, 1, 2);
1236
1237   //basic.DefSubr (Fappend, "append", true, 0, -1);
1238   //basic.DefSubr (Fconcat, "concat", true, 0, -1);
1239   //basic.DefSubr (Fnth, "nth", false, 2, 2);
1240   //basic.DefSubr (Fcopy, "copy", false, 1, 1);
1241   //basic.DefSubr (Fins, "ins", true, 2, 2);
1242   //basic.DefSubr (Fdel, "del", true, 2, 2);
1243   //basic.DefSubr (Feval, "eval", false, 1, 1);
1244   //basic.DefSubr (Fbreak, "break", false, 0, 1);
1245   //basic.DefSubr (Freturn, "return", false, 0, 1);
1246   //basic.DefSubr (Fthrow, "throw", false, 1, 2);
1247
1248   basic.DefSpecial (Fand, "and", false, 1, -1);
1249   basic.DefAlias ("&&", "and");
1250   basic.DefSpecial (For, "or", false, 1, -1);
1251   basic.DefAlias ("||", "or");
1252   basic.DefSpecial (Fprogn, "progn", false, 1, -1);
1253   basic.DefAlias ("expr", "progn");
1254   basic.DefSpecial (Fif, "if", false, 2, 3);
1255   //basic.DefSpecial (Fwhen, "when", false, 1, -1);
1256   //basic.DefSpecial (Floop, "loop", false, 1, -1);
1257   //basic.DefSpecial (Fwhile, "while", false, 1, -1);
1258   basic.DefSpecial (Fcond, "cond", false, 1, -1);
1259   //basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
1260   //basic.DefSpecial (Fquote, "quote", false, 1, 1);
1261   //basic.DefSpecial (Ftype, "type", false, 1, 1);
1262   basic.DefSpecial (Fcatch, "catch", true, 2, -1);
1263
1264   basic.DefType (Xex.Funcall.prototype);
1265   basic.DefType (Xex.Varref.prototype);
1266   basic.DefType (Xex.ErrTerm.prototype);
1267   basic.DefType (Xex.IntTerm.prototype);
1268   basic.DefType (Xex.StrTerm.prototype);
1269   basic.DefType (Xex.SymTerm.prototype);
1270   basic.DefType (Xex.LstTerm.prototype);
1271
1272 }) ();
1273
1274 Xex.Zero = new Xex.IntTerm (0);
1275 Xex.One = new Xex.IntTerm (1);
1276 Xex.nil = new Xex.SymTerm ('nil');
1277
1278 Xex.Load = function (server, file)
1279 {
1280   var obj = new XMLHttpRequest ();
1281   var url = server ? server + '/' + file : file;
1282   obj.open ('GET', url, false);
1283   obj.overrideMimeType ('text/xml');
1284   obj.send ('');
1285   return obj.responseXML.firstChild;
1286 }
1287
1288 var MIM = {
1289   // URL of the input method server.
1290   server: "http://www.m17n.org/common/mim-js",
1291   // Boolean flag to tell if MIM is active or not.
1292   enabled: true,
1293   // Boolean flag to tell if MIM is running in debug mode or not.
1294   debug: false,
1295   // List of main input methods.
1296   imlist: {},
1297   // List of extra input methods;
1298   imextra: {},
1299   // Global input method data
1300   im_global: null,
1301   // Currently selected input method.
1302   current: false,
1303
1304   // enum
1305   LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
1306   ChangedStatus: {
1307     None:       0x00,
1308     StateTitle: 0x01,
1309     PreeditText:0x02,
1310     CursorPos:  0x04,
1311     CandidateList:0x08,
1312     CandidateIndex:0x10,
1313     CandidateShow:0x20,
1314     Preedit:    0x06,           // PreeditText | CursorPos
1315     Candidate:  0x38 // CandidateList | CandidateIndex | CandidateShow
1316   },
1317   KeyModifier: {
1318     SL: 0x00400000,
1319     SR: 0x00800000,
1320     S:  0x00C00000,
1321     CL: 0x01000000,
1322     CR: 0x02000000,
1323     C:  0x03000000,
1324     AL: 0x04000000,
1325     AR: 0x08000000,
1326     A:  0x0C000000,
1327     ML: 0x04000000,
1328     MR: 0x08000000,
1329     M:  0x0C000000,
1330     G:  0x10000000,
1331     s:  0x20000000,
1332     H:  0x40000000,
1333     High:       0x70000000,
1334     All:        0x7FC00000
1335   },
1336   Error: {
1337     ParseError: "parse-error"
1338   }
1339 };
1340   
1341 (function () {
1342   var keysyms = new Array ();
1343   keysyms["bs"] = "backspace";
1344   keysyms["lf"] = "linefeed";
1345   keysyms["cr"] = keysyms["enter"] = "return";
1346   keysyms["esc"] = "escape";
1347   keysyms["spc"] = "space";
1348   keysyms["del"] = "delete";
1349
1350   function decode_keysym (str) {
1351     if (str.length == 1)
1352       return str;
1353     var parts = str.split ("-");
1354     var len = parts.length, i;
1355     var has_modifier = len > 1;
1356
1357     for (i = 0; i < len - 1; i++)
1358       if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
1359         return false;
1360     var key = parts[len - 1];
1361     if (key.length > 1)
1362       {
1363         key = keysyms[key.toLowerCase ()];
1364         if (key)
1365           {
1366             if (len > 1)
1367               {
1368                 str = parts[0];
1369                 for (i = 1; i < len - 1; i++)
1370                   str += '-' + parts[i];
1371                 str += '-' + key;
1372               }
1373             else
1374               str = key;
1375           }
1376       }
1377     if (has_modifier)
1378       {
1379         parts = new Array ();
1380         parts.push (str);
1381         return parts;
1382       }
1383     return str;
1384   }
1385
1386   MIM.Key = function (val)
1387   {
1388     this.key;
1389     this.has_modifier = false;
1390     if (val instanceof Xex.Term)
1391       this.key = val.val;
1392     else if (typeof val == 'string' || val instanceof String)
1393       {
1394         this.key = decode_keysym (val);
1395         if (! this.key)
1396           throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1397         if (this.key instanceof Array)
1398           {
1399             this.key = this.key[0];
1400             this.has_modifier = true;
1401           }
1402       }
1403     else if (typeof val == 'number' || val instanceof Number)
1404       this.key = String.fromCharCode (val);
1405     else
1406       throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
1407   }
1408
1409   MIM.Key.prototype.toString = function () { return this.key; };
1410
1411   MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
1412   MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
1413   MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
1414 }) ();
1415
1416 (function () {
1417   MIM.KeySeq = function (seq)
1418   {
1419     this.val = new Array ();
1420     this.has_modifier = false;
1421
1422     if (seq)
1423       {
1424         if (seq.IsList)
1425           {
1426             var len = seq.val.length;
1427             for (var i = 0; i < len; i++)
1428               {
1429                 var v = seq.val[i], key;
1430                 if (v.type == 'symbol' || v.type == 'string')
1431                   key = new MIM.Key (v);
1432                 else if (v.type == 'integer')
1433                   key = new MIM.Key (v.val);
1434                 else
1435                   throw new Xex.ErrTerm (MIM.Error.ParseError,
1436                                          "Invalid key: " + v);
1437                 this.val.push (key);
1438                 if (key.has_modifier)
1439                   this.has_modifier = true;
1440               }
1441           }
1442         else if (seq.IsStr)
1443           {
1444             var len = seq.val.length;
1445             for (var i = 0; i < len; i++)
1446               this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
1447           }
1448         else
1449           throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
1450       }
1451   }
1452
1453   var proto = new Xex.Term ('keyseq');
1454   proto.Clone = function () { return this; }
1455   proto.Parser = function (domain, node)
1456   {
1457     var seq = new Array ();
1458     for (node = node.firstChild; node; node = node.nextSibling)
1459       if (node.nodeType == 1)
1460         {
1461           var term = Xex.Term.Parse (domain, node);
1462           return new MIM.KeySeq (term);
1463         }
1464     throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
1465   }
1466   proto.toString = function ()
1467   {
1468     var len = this.val.length;
1469     if (len == 0)
1470       return '<keyseq/>';
1471     var first = true;
1472     var str = '<keyseq>';
1473     for (var i = 0; i < len; i++)
1474       {
1475         if (first)
1476           first = false;
1477         else if (this.has_modifier)
1478           str += ' ';
1479         str += this.val[i].toString ();
1480       }
1481     return str + '</keyseq>';
1482   }
1483
1484   MIM.KeySeq.prototype = proto;
1485 }) ();
1486
1487 (function () {
1488   MIM.Marker = function () { }
1489   MIM.Marker.prototype = new Xex.Term ('marker');
1490   MIM.Marker.prototype.CharAt = function (ic)
1491   {
1492     var p = this.Position (ic);
1493     if (p < 0 || p >= ic.preedit.length)
1494       return 0;
1495     return ic.preedit.charCodeAt (p);
1496   }
1497
1498   MIM.FloatingMarker = function (name) { this.val = name; };
1499   var proto = new MIM.Marker ();
1500   MIM.FloatingMarker.prototype = proto;
1501   proto.Position = function (ic) { return ic.marker_positions[this.val]; };
1502   proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
1503
1504   MIM.PredefinedMarker = function (name) { this.val = name; }
1505   MIM.PredefinedMarker.prototype = new MIM.Marker ();
1506   MIM.PredefinedMarker.prototype.Position = function (ic)
1507   {
1508     if (typeof this.pos == 'number')
1509       return this.pos;
1510     return this.pos (ic);
1511   }
1512
1513   var predefined = { }
1514
1515   function def_predefined (name, position)
1516   {
1517     predefined[name] = new MIM.PredefinedMarker (name);
1518     predefined[name].pos = position;
1519   }
1520
1521   def_predefined ('@<', 0);
1522   def_predefined ('@>', function (ic) { return ic.preedit.length; });
1523   def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
1524   def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
1525   def_predefined ('@[', function (ic) {
1526     if (ic.cursor_pos > 0)
1527       {
1528         var pos = ic.cursor_pos;
1529         return ic.preedit.FindProp ('candidates', pos - 1).from;
1530       }
1531     return 0;
1532   });
1533   def_predefined ('@]', function (ic) {
1534     if (ic.cursor_pos < ic.preedit.length - 1)
1535       {
1536         var pos = ic.cursor_pos;
1537         return ic.preedit.FindProp ('candidates', pos).to;
1538       }
1539     return ic.preedit.length;
1540   });
1541   for (var i = 0; i < 10; i++)
1542     def_predefined ("@" + i, i);
1543   predefined['@first'] = predefined['@<'];
1544   predefined['@last'] = predefined['@>'];
1545   predefined['@previous'] = predefined['@-'];
1546   predefined['@next'] = predefined['@+'];
1547   predefined['@previous-candidate-change'] = predefined['@['];
1548   predefined['@next-candidate-change'] = predefined['@]'];
1549
1550   MIM.SurroundMarker = function (name)
1551   {
1552     this.val = name;
1553     this.distance = parseInt (name.slice (1));
1554     if (isNaN (this.distance))
1555       throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
1556   }
1557   MIM.SurroundMarker.prototype = new MIM.Marker ();
1558   MIM.SurroundMarker.prototype.Position = function (ic)
1559   {
1560     return ic.cursor_pos + this.distance;
1561   }
1562   MIM.SurroundMarker.prototype.CharAt = function (ic)
1563   {
1564     if (this.val == '@-0')
1565       return -1;
1566     var p = this.Position (ic);
1567     if (p < 0)
1568       return ic.GetSurroundingChar (p);
1569     else if (p >= ic.preedit.length)
1570       return ic.GetSurroundingChar (p - ic.preedit.length);
1571     return ic.preedit.charCodeAt (p);
1572   }
1573
1574   MIM.Marker.prototype.Parser = function (domain, node)
1575   {
1576     var name = node.firstChild.nodeValue;
1577     if (name.charAt (0) == '@')
1578       {
1579         var n = predefined[name];
1580         if (n)
1581           return n;
1582         if (name.charAt (1) == '-' || name.charAt (1) == '+')
1583           return new MIM.SurroundMarker (name);
1584         throw new Xex.ErrTerm (MIM.Error.ParseError,
1585                                "Invalid marker: " + name);
1586       }
1587     return new MIM.FloatingMarker (name);;
1588   }
1589 }) ();
1590
1591 MIM.Selector = function (name)
1592 {
1593   this.val = name;
1594 }
1595 MIM.Selector.prototype = new Xex.Term ('selector');
1596
1597 (function () {
1598   var selectors = {};
1599   selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
1600   selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
1601   selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
1602   selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
1603   selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
1604   selectors["@["] = selectors["@previous-candidate-change"]
1605     = new MIM.Selector ('@[');
1606   selectors["@]"] = selectors["@next-candidate-change"]
1607     = new MIM.Selector ('@]');
1608
1609   MIM.Selector.prototype.Parser = function (domain, node)
1610   {
1611     var name = node.firstChild.nodeValue;
1612     var s = selectors[name];
1613     if (! s)
1614       throw new Xex.ErrTerm (MIM.Error.ParseError,
1615                              "Invalid selector: " + name);
1616     return s;
1617   }
1618 }) ();
1619
1620 MIM.Rule = function (keyseq, actions)
1621 {
1622   this.keyseq = keyseq;
1623   this.actions = actions;
1624 }
1625 MIM.Rule.prototype = new Xex.Term ('rule');
1626 MIM.Rule.prototype.Parser = function (domain, node)
1627 {
1628   var n;
1629   for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1630   if (! n)
1631     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1632   var keyseq = Xex.Term.Parse (domain, n);
1633   if (keyseq.type != 'keyseq')
1634     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1635   var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1636   return new MIM.Rule (keyseq, actions);
1637 }
1638 MIM.Rule.prototype.toString = function ()
1639 {
1640   return '<rule/>';
1641 }
1642
1643 MIM.Map = function (name)
1644 {
1645   this.name = name;
1646   this.rules = new Array ();
1647 };
1648
1649 (function () {
1650   var proto = new Xex.Term ('map');
1651
1652   proto.Parser = function (domain, node)
1653   {
1654     var name = node.attributes['mname'].nodeValue;
1655     if (! name)
1656       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1657     var map = new MIM.Map (name);
1658     for (var n = node.firstChild; n; n = n.nextSibling)
1659       if (n.nodeType == 1)
1660         map.rules.push (Xex.Term.Parse (domain, n));
1661     return map;
1662   }
1663
1664   proto.toString = function ()
1665   {
1666     var str = '<map mname="' + this.name + '">';
1667     var len = this.rules.length;
1668     for (i = 0; i < len; i++)
1669       str += this.rules[i];
1670     return str + '</map>';
1671   }
1672
1673   MIM.Map.prototype = proto;
1674 }) ();
1675
1676 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1677
1678 MIM.Action = function (domain, terms)
1679 {
1680   var args = new Array ();
1681   args.push (Xex.CatchTag_.mimtag);
1682   for (var i = 0; i < terms.length; i++)
1683     args.push (terms[i]);
1684   this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1685 }
1686
1687 MIM.Action.prototype.Run = function (domain)
1688 {
1689   var result = this.action.Eval (domain);
1690   if (result.type == 'error')
1691     {
1692       domain.context.Error = result.toString ();
1693       return false;
1694     }
1695   return (result != Xex.CatchTag._mimtag);
1696 }
1697
1698 MIM.Keymap = function ()
1699 {
1700   this.name = 'TOP';
1701   this.submaps = null;
1702 };
1703
1704 (function () {
1705   var proto = {};
1706
1707   function add_rule (keymap, rule, branch_actions)
1708   {
1709     var keyseq = rule.keyseq;
1710     var len = keyseq.val.length;
1711     var name = '';
1712
1713     for (var i = 0; i < len; i++)
1714       {
1715         var key = keyseq.val[i];
1716         var sub = false;
1717
1718         name += key.key;
1719         if (! keymap.submaps)
1720           keymap.submaps = {};
1721         else
1722           sub = keymap.submaps[key.key];
1723         if (! sub)
1724           keymap.submaps[key.key] = sub = new MIM.Keymap ();
1725         keymap = sub;
1726         keymap.name = name;
1727       }
1728     keymap.map_actions = rule.actions;
1729     keymap.branch_actions = branch_actions;
1730   }
1731
1732   proto.Add = function (map, branch_actions)
1733   {
1734     var rules = map.rules;
1735     var len = rules.length;
1736
1737     for (var i = 0; i < len; i++)
1738       add_rule (this, rules[i], branch_actions);
1739   }
1740   proto.Lookup = function (keys, index)
1741   {
1742     var sub;
1743
1744     if (index < keys.val.length && this.submaps
1745         && (sub = this.submaps[keys.val[index].key]))
1746       {
1747         index++;
1748         return sub.Lookup (keys, index);
1749       }
1750     return { map: this, index: index };
1751   }
1752
1753   MIM.Keymap.prototype = proto;
1754 }) ();
1755
1756 MIM.State = function (name)
1757 {
1758   this.name = name;
1759   this.keymap = new MIM.Keymap ();
1760 };
1761
1762 (function () {
1763   var proto = new Xex.Term ('state');
1764
1765   proto.Parser = function (domain, node)
1766   {
1767     var map_list = domain.map_list;
1768     var name = node.attributes['sname'].nodeValue;
1769     if (! name)
1770       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1771     var state = new MIM.State (name);
1772     for (node = node.firstElement (); node; node = node.nextElement ())
1773       {
1774         if (node.nodeName == 'title')
1775           state.title = node.firstChild.nodeValue;
1776         else
1777           {
1778             var n = node.firstElement ();
1779             if (node.nodeName == 'branch')
1780               state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1781                                 Xex.Term.Parse (domain, n, null));
1782             else if (node.nodeName == 'state-hook')
1783               state.enter_actions = Xex.Term.Parse (domain, n, null);
1784             else if (node.nodeName == 'catch-all-branch')
1785               state.fallback_actions = Xex.Term.Parse (domain, n, null);
1786           }
1787       }
1788     return state;
1789   }
1790
1791   proto.toString = function ()
1792   {
1793     return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1794   }
1795
1796   MIM.State.prototype = proto;
1797 }) ();
1798
1799 (function () {
1800   function Block (index, term)
1801   {
1802     this.Index = index;
1803     if (term.IsStr)
1804       this.Data = term.val;
1805     else if (term.IsList)
1806       {
1807         this.Data = new Array ();
1808         for (var i = 0; i < term.val.length; i++)
1809           this.Data.push (term.val[i].val);
1810       }
1811   }
1812
1813   Block.prototype.Count = function () { return this.Data.length; }
1814   Block.prototype.get = function (i)
1815   {
1816     return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1817   }
1818
1819   MIM.Candidates = function (candidates, column)
1820   {
1821     this.column = column;
1822     this.row = 0;
1823     this.index = 0;
1824     this.total = 0;
1825     this.blocks = new Array ();
1826
1827     for (var i = 0; i < candidates.length; i++)
1828       {
1829         var block = new Block (this.total, candidates[i]);
1830         this.blocks.push (block);
1831         this.total += block.Count ();
1832       }
1833   }
1834
1835   function get_col ()
1836   {
1837     return (this.column > 0 ? this.index % this.column
1838             : this.index - this.blocks[this.row].Index);
1839   }
1840
1841   function prev_group ()
1842   {
1843     var col = get_col.call (this);
1844     var nitems;
1845     if (this.column > 0)
1846       {
1847         this.index -= this.column;
1848         if (this.index >= 0)
1849           nitems = this.column;
1850         else
1851           {
1852             var lastcol = (this.total - 1) % this.column;
1853             this.index = (col < lastcol ? this.total - lastcol + col
1854                           : this.total - 1);
1855             this.row = this.blocks.length - 1;
1856             nitems = lastcol + 1;
1857           }
1858         while (this.blocks[this.row].Index > this.index)
1859           this.row--;
1860       }
1861     else
1862       {
1863         this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1864         nitems = this.blocks[this.row].Count ();
1865         this.index = (this.blocks[this.row].Index
1866                       + (col < nitems ? col : nitems - 1));
1867       }
1868     return nitems;
1869   }
1870
1871   function next_group ()
1872   {
1873     var col = get_col.call (this);
1874     var nitems;
1875     if (this.column > 0)
1876       {
1877         this.index += this.column - col;
1878         if (this.index < this.total)
1879           {
1880             if (this.index + col >= this.total)
1881               {
1882                 nitems = this.total - this.index;
1883                 this.index = this.total - 1;
1884               }
1885             else
1886               {
1887                 nitems = this.column;
1888                 this.index += col;
1889               }
1890           }
1891         else
1892           {
1893             this.index = col;
1894             this.row = 0;
1895           }
1896         while (this.blocks[this.row].Index > this.index)
1897           this.row++;
1898       }
1899     else
1900       {
1901         this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1902         nitems = this.blocks[this.row].Count ();
1903         this.index = (this.blocks[this.row].Index
1904                       + (col < nitems ? col : nitems - 1));
1905       }
1906     return nitems;
1907   }
1908
1909   function prev ()
1910   {
1911     if (this.index == 0)
1912       {
1913         this.index = this.total - 1;
1914         this.row = this.blocks.length - 1;
1915       }
1916     else
1917       {
1918         this.index--;
1919         if (this.blocks[this.row].Index > this.index)
1920           this.row--;
1921       }
1922     }
1923
1924   function next ()
1925   {
1926     this.index++;
1927     if (this.index == this.total)
1928       {
1929         this.index = 0;
1930         this.row = 0;
1931       }
1932     else
1933       {
1934         var b = this.blocks[this.row];
1935         if (this.index == b.Index + b.Count ())
1936           this.row++;
1937       }
1938   }
1939
1940   function first ()
1941   {
1942     this.index -= get_col.call (this);
1943     while (this.blocks[this.row].Index > this.index)
1944       this.row--;
1945   }
1946
1947   function last ()
1948   {
1949     var b = this.blocks[this.row];
1950     if (this.column > 0)
1951       {
1952         if (this.index + 1 < this.total)
1953           {
1954             this.index += this.column - get_col.call (this) + 1;
1955             while (b.Index + b.Count () <= this.index)
1956               b = this.blocks[++this.row];
1957           }
1958       }
1959     else
1960       this.index = b.Index + b.Count () - 1;
1961   }
1962
1963   MIM.Candidates.prototype.Current = function ()
1964   {
1965     var b = this.blocks[this.row];
1966     return b.get (this.index - b.Index);
1967   }
1968
1969   MIM.Candidates.prototype.Select = function (selector)
1970   {
1971     if (selector.type == 'selector')
1972       {
1973         switch (selector.val)
1974           {
1975           case '@<': first.call (this); break;
1976           case '@>': last.call (this); break;
1977           case '@-': prev.call (this); break;
1978           case '@+': next.call (this); break;
1979           case '@[': prev_group.call (this); break;
1980           case '@]': next_group.cal (this); break;
1981           default: break;
1982           }
1983         return this.Current ();
1984       }
1985     var col, start, end
1986     if (this.column > 0)
1987       {
1988         col = this.index % this.column;
1989         start = this.index - col;
1990         end = start + this.column;
1991       }
1992     else
1993       {
1994         start = this.blocks[this.row].Index;
1995         col = this.index - start;
1996         end = start + this.blocks[this.row].Count;
1997       }
1998     if (end > this.total)
1999       end = this.total;
2000     this.index += selector.val - col;
2001     if (this.index >= end)
2002       this.index = end - 1;
2003     if (this.column > 0)
2004       {
2005         if (selector.val > col)
2006           while (this.blocks[this.row].Index + this.blocks[this.row].Count
2007                  < this.index)
2008             this.row++;
2009         else
2010           while (this.blocks[this.row].Index > this.index)
2011             this.row--;
2012       }
2013     return this.Current ();
2014   }
2015 }) ();
2016
2017 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2018 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2019 MIM.im_domain.DefType (MIM.Marker.prototype);
2020 MIM.im_domain.DefType (MIM.Selector.prototype);
2021 MIM.im_domain.DefType (MIM.Rule.prototype);
2022 MIM.im_domain.DefType (MIM.Map.prototype);
2023 MIM.im_domain.DefType (MIM.State.prototype);
2024
2025 (function () {
2026   var im_domain = MIM.im_domain;
2027
2028   function Finsert (domain, vari, args)
2029   {
2030     var text;
2031     if (args[0].type == 'integer')
2032       text = String.fromCharCode (args[0].val);
2033     else
2034       text = args[0].val;
2035     domain.context.ins (text, null);
2036     return args[0];
2037   }
2038
2039   function Finsert_candidates (domain, vari, args)
2040   {
2041     var ic = domain.context;
2042     var gsize = domain.variables['candidates_group_size'];
2043     var candidates = new MIM.Candidates (args, gsize ? gsize.Intval () : 0);
2044     ic.ins (candidates.Current (), candidates);
2045     return args[0];
2046   }
2047
2048   function Fdelete (domain, vari, args)
2049   {
2050     var ic = domain.context;
2051     var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2052     return new Xex.IntTerm (ic.del (pos));
2053   }
2054
2055   function Fselect (domain, vari, args)
2056   {
2057     var ic = domain.context;
2058     var can = ic.candidates;
2059
2060     if (can)
2061       {
2062         var old_text = can.Current ();
2063         var new_text = can.Select (args[0]);
2064         ic.rep (old_text, new_text, can);
2065       }
2066     else
2067       Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2068     return args[0];
2069   }
2070
2071   function Fshow (domain, vari, args)
2072   {
2073     domain.context.candidate_show = true;
2074     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2075     return Xex.nil;
2076   }
2077
2078   function Fhide (domain, vari, args)
2079   {
2080     domain.context.candidate_show = false;
2081     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2082     return Xex.nil;
2083   }
2084
2085   function Fchar_at (domain, vari, args)
2086   {
2087     return new Xex.IntTerm (args[0].CharAt (domain.context));
2088   }
2089
2090   function Fmove (domain, vari, args)
2091   {
2092     var ic = domain.context;
2093     var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2094     ic.move (pos);
2095     return new Xex.IntTerm (pos);
2096   }
2097
2098   function Fmark (domain, vari, args)
2099   {
2100     args[0].Mark (domain.context);
2101     return args[0];
2102   }
2103
2104   function Fpushback (domain, vari, args)
2105   {
2106     var a = (args[0].IsInt ? args[0].Intval ()
2107              : args[0].IsStr ? new KeySeq (args[0])
2108              : args[0]);
2109     domain.context.pushback (a);
2110     return args[0];
2111   }
2112
2113   function Fpop (domain, vari, args)
2114   {
2115     var ic = domain.context;
2116     if (ic.key_head < ic.keys.val.length)
2117       ic.keys.val.splice (ic.keys_head, 1);
2118     return Xex.nil;
2119   }
2120
2121   function Fundo  (domain, vari, args)
2122   {
2123     var ic = domain.context;
2124     var n = args.length == 0 ? -2 : args[0].val;
2125     if (n < 0)
2126       ic.keys.val.splice (ic.keys.val.length + n, -n);
2127     else
2128       ic.keys.val.splice (n, ic.keys.val.length);
2129     ic.reset ();
2130     return Xex.nil;
2131   }
2132
2133   function Fcommit (domain, vari, args)
2134   {
2135     domain.context.commit ();
2136     return Xex.nil;
2137   }
2138
2139   function Funhandle (domain, vari, args)
2140     {
2141       domain.context.commit ();
2142       return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2143     }
2144
2145   function Fshift (domain, vari, args)
2146   {
2147     var ic = domain.context;
2148     var state_name = args[0].val;
2149     var state = ic.im.state_list[state_name];
2150     if (! state)
2151       throw ("Unknown state: " + state_name);
2152       ic.shift (state);
2153     return args[0];
2154   }
2155
2156   function Fshiftback (domain, vari, args)
2157   {
2158     domain.context.shift (null);
2159     return Xex.nil;
2160   }
2161
2162   function Fkey_count (domain, vari, args)
2163   {
2164     return new Xex.IntTerm (domain.context.key_head);
2165   }
2166
2167   function Fsurrounding_flag (domain, vari, args)
2168   {
2169     return new Xex.IntTerm (-1);
2170   }
2171
2172   im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2173   im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2174   im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2175   im_domain.DefSubr (Fselect, "select", false, 1, 1);
2176   im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2177   im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2178   im_domain.DefSubr (Fmove, "move", false, 1, 1);
2179   im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2180   im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2181   im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2182   im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2183   im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2184   im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2185   im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2186   im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2187   im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2188   im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2189   im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2190 }) ();
2191
2192
2193 (function () {
2194   function get_global_var (vname)
2195   {
2196     if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2197       MIM.im_global.Load ()
2198     return MIM.im_global.domain.variables[vname];
2199   }
2200
2201   function include (node)
2202   {
2203     node = node.firstElement ();
2204     if (node.nodeName != 'tags')
2205       return null;
2206     
2207     var lang = null, name = null, extra = null;
2208     for (node = node.firstElement (); node; node = node.nextElement ())
2209       {
2210         if (node.nodeName == 'language')
2211           lang = node.firstChild.nodeValue;
2212         else if (node.nodeName == 'name')
2213           name = node.firstChild.nodeValue;
2214         else if (node.nodeName == 'extra-id')
2215           extra = node.firstChild.nodeValue;
2216       }
2217     if (! lang || ! MIM.imlist[lang])
2218       return null;
2219     if (! extra)
2220       {
2221         if (! name || ! (im = MIM.imlist[lang][name]))
2222           return null;
2223       }
2224     else
2225       {
2226         if (! (im = MIM.imextra[lang][extra]))
2227           return null;
2228       }
2229     if (im.load_status != MIM.LoadStatus.Loaded
2230         && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2231       return null;
2232     return im;
2233   }
2234
2235   var parsers = { };
2236
2237   parsers['description'] = function (node)
2238   {
2239     this.description = node.firstChild.nodeValue;
2240   }
2241   parsers['variable-list'] = function (node)
2242   {
2243     for (node = node.firstElement (); node; node = node.nextElement ())
2244       {
2245         var vname = node.attributes['vname'].nodeValue;
2246         if (this != MIM.im_global)
2247           {
2248             var vari = get_global_var (vname);
2249             if (vari != null)
2250               this.domain.Defvar (vname);
2251           }
2252         vname = Xex.Term.Parse (this.domain, node)
2253       }
2254   }
2255   parsers['command-list'] = function (node)
2256   {
2257   }
2258   parsers['macro-list'] = function (node)
2259   {
2260     for (var n = node.firstElement (); n; n = n.nextElement ())
2261       if (n.nodeName == 'xi:include')
2262         {
2263           var im = include (n);
2264           if (! im)
2265             alert ('inclusion fail');
2266           else
2267             for (var macro in im.domain.functions)
2268               {
2269                 var func = im.domain.functions[macro];
2270                 if (func instanceof Xex.Macro)
2271                   im.domain.CopyFunc (this.domain, macro);
2272               }
2273           n = n.previousSibling;
2274           node.removeChild (n.nextSibling);
2275         }
2276     Xex.Term.Parse (this.domain, node.firstElement (), null);
2277   }
2278   parsers['title'] = function (node)
2279   {
2280     this.title = node.firstChild.nodeValue;
2281   }
2282   parsers['map-list'] = function (node)
2283   {
2284     for (node = node.firstElement (); node; node = node.nextElement ())
2285       {
2286         if (node.nodeName == 'xi:include')
2287           {
2288             var im = include (node);
2289             if (! im)
2290               {
2291                 alert ('inclusion fail');
2292                 continue;
2293               }
2294             for (var mname in im.map_list)
2295               {
2296                 this.map_list[mname] = im.map_list[mname];
2297                 Xex.Log ('include map ' + mname);
2298               }
2299           }
2300         else
2301           {
2302             var map = Xex.Term.Parse (this.domain, node);
2303             this.map_list[map.name] = map;
2304           }
2305       }
2306   }
2307   parsers['state-list'] = function (node)
2308   {
2309     this.domain.map_list = this.map_list;
2310     for (node = node.firstElement (); node; node = node.nextElement ())
2311       {
2312         if (node.nodeName == 'xi:include')
2313           {
2314             var im = include (node);
2315             if (! im)
2316               alert ('inclusion fail');
2317             for (var sname in im.state_list)
2318               {
2319                 state = im.state_list[sname];
2320                 if (! this.initial_state)
2321                   this.initial_state = state;
2322                 this.state_list[sname] = state;
2323                 Xex.Log ('include state ' + sname);
2324               }
2325           }
2326         else if (node.nodeName == 'state')
2327           {
2328             var state = Xex.Term.Parse (this.domain, node);
2329             if (! state.title)
2330               state.title = this.title;
2331             if (! this.initial_state)
2332               this.initial_state = state;
2333             this.state_list[state.name] = state;
2334           }
2335       }
2336     delete this.domain.map_list;
2337   }
2338
2339   MIM.IM = function (lang, name, extra_id, file)
2340   {
2341     this.lang = lang;
2342     this.name = name;
2343     this.extra_id = extra_id;
2344     this.file = file;
2345     this.load_status = MIM.LoadStatus.NotLoaded;
2346     this.domain = new Xex.Domain (this.lang + '-'
2347                                   + (this.name != 'nil'
2348                                      ? this.name : this.extra_id),
2349                                   MIM.im_domain, null);
2350   }
2351
2352   var proto = {
2353     Load: function ()
2354     {
2355       var node = Xex.Load (null, this.file);
2356       if (! node)
2357         {
2358           this.load_status = MIM.LoadStatus.Error;
2359           return false;
2360         }
2361       this.map_list = {};
2362       this.initial_state = null;
2363       this.state_list = {};
2364       for (node = node.firstElement (); node; node = node.nextElement ())
2365         {
2366           var name = node.nodeName;
2367           var parser = parsers[name];
2368           if (parser)
2369             parser.call (this, node);
2370         }
2371       this.load_status = MIM.LoadStatus.Loaded;
2372       return true;
2373     }
2374   }
2375
2376   MIM.IM.prototype = proto;
2377
2378   MIM.IC = function (im, target)
2379   {
2380     if (im.load_status == MIM.LoadStatus.NotLoaded)
2381       im.Load ();
2382     if (im.load_status != MIM.LoadStatus.Loaded)
2383       alert ('im:' + im.name + ' error:' + im.load_status);
2384     this.im = im;
2385     this.target = target;
2386     this.domain = new Xex.Domain ('context', im.domain, this);
2387     this.active = true;
2388     this.range = new Array ();
2389     this.range[0] = this.range[1] = 0;
2390     this.state = null;
2391     this.initial_state = this.im.initial_state;
2392     this.keys = new MIM.KeySeq ();
2393     this.marker_positions = new Array ();
2394     this.candidate_table = new MIM.CandidateTable ();
2395     this.reset ();
2396   }
2397
2398   MIM.CandidateTable = function ()
2399   {
2400     this.table = new Array ();
2401   }
2402
2403   MIM.CandidateTable.prototype.get = function (pos)
2404   {
2405     for (var i = 0; i < this.table.length; i++)
2406       {
2407         var elt = this.table[i];
2408         if (elt.from < pos && pos <= elt.to)
2409           return elt.val;
2410       }
2411   }
2412
2413   MIM.CandidateTable.prototype.put = function (from, to, candidates)
2414   {
2415     for (var i = 0; i < this.table.length; i++)
2416       {
2417         var elt = this.table[i];
2418         if (elt.from < to && elt.to > from)
2419           {
2420             elt.from = from;
2421             elt.to = to;
2422             elt.val = candidates;
2423             return;
2424           }
2425       }
2426     this.table.push ({ from: from, to: to, val: candidates });
2427   }
2428
2429   MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2430   {
2431     var diff = inserted - (to - from);
2432     if (diff == 0)
2433       return;
2434     for (var i = 0; i < this.table.length; i++)
2435       {
2436         var elt = this.table[i];
2437         if (elt.from >= to)
2438           {
2439             elt.from += diff;
2440             elt.to += diff;
2441           }
2442       }
2443   }
2444
2445   MIM.CandidateTable.prototype.clear = function ()
2446   {
2447     this.table.length = 0;
2448   }
2449
2450   function detach_candidates (ic)
2451   {
2452     ic.candidate_table.clear ();
2453     ic.candidates = null;
2454     ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2455                    | MIM.ChangedStatus.CandidateList
2456                    | MIM.ChangedStatus.CandidateIndex
2457                    | MIM.ChangedStatus.CandidateShow);
2458   }
2459
2460   function set_cursor (prefix, pos)
2461   {
2462     this.cursor_pos = pos;
2463     this.candidates = this.candidate_table.get (pos);
2464   }
2465
2466   function save_state ()
2467   {
2468     this.state_var_values = this.domain.SaveValues ();
2469     this.state_preedit = this.preedit;
2470     this.state_key_head = this.key_head;
2471     this.state_pos = this.cursor_pos;
2472   }
2473
2474   function restore_state ()
2475   {
2476     this.domain.RestoreValues (this.state_var_values);
2477     this.preedit = this.state_preedit;
2478     set_cursor.call (this, "restore", this.state_pos);
2479   }
2480
2481   function handle_key ()
2482   {
2483     var out = this.keymap.Lookup (this.keys, this.key_head);
2484     var sub = out.map;
2485
2486     Xex.Log ('handling ' + this.keys.val[this.key_head]
2487              + ' in ' + this.state.name + ':' + this.keymap.name);
2488     this.key_head = out.index;
2489     if (sub != this.keymap)
2490       {
2491
2492         restore_state.call (this);
2493         this.keymap = sub;
2494         Xex.Log ('submap found');
2495         if (this.keymap.map_actions)
2496           {
2497             Xex.Log ('taking map actions:');
2498             if (! this.take_actions (this.keymap.map_actions))
2499               return false;
2500           }
2501         else if (this.keymap.submaps)
2502           {
2503             Xex.Log ('no map actions');
2504             for (var i = this.state_key_head; i < this.key_head; i++)
2505               {
2506                 Xex.Log ('inserting key:' + this.keys.val[i].key);
2507                 this.ins (this.keys.val[i].key, null);
2508               }
2509           }
2510         if (! this.keymap.submaps)
2511           {
2512             Xex.Log ('terminal:');
2513             if (this.keymap.branch_actions != null)
2514               {
2515                 Xex.Log ('branch actions:');
2516                 if (! this.take_actions (this.keymap.branch_actions))
2517                   return false;
2518               }
2519             if (this.keymap != this.state.keymap)
2520               this.shift (this.state);
2521           }
2522       }
2523     else
2524       {
2525         Xex.Log ('no submap');
2526         var current_state = this.state;
2527         var map = this.keymap;
2528
2529         if (map.branch_actions)
2530           {
2531             Xex.Log ('branch actions');
2532             if (! this.take_actions (map.branch_actions))
2533               return false;
2534           }
2535
2536         if (map == this.keymap)
2537           {
2538             Xex.Log ('no state change');
2539             if (map == this.initial_state.keymap
2540                 && this.key_head < this.keys.val.length)
2541               {
2542                 Xex.Log ('unhandled');
2543                 return false;
2544               }
2545             if (this.keymap != current_state.keymap)
2546               this.shift (current_state);
2547             else if (this.keymap.actions == null)
2548               this.shift (this.initial_state);
2549           }
2550       }
2551     return true;
2552   }
2553
2554   proto = {
2555     reset: function ()
2556     {
2557       Xex.Log ('reseting ' + this.im.lang + '-' + this.im.name);
2558       this.cursor_pos = 0;
2559       this.candidate_show = false;
2560       this.prev_state = null;
2561       this.title = this.initial_state.title;
2562       this.state_preedit = '';
2563       this.state_key_head = 0;
2564       this.state_var_values = {};
2565       this.state_pos = 0;
2566       this.key_head = 0;
2567       this.key_unhandled = false;
2568       this.unhandled_key = null;
2569       this.changed = MIM.ChangedStatus.None;
2570       this.error_message = '';
2571       this.title = this.initial_state.title;
2572       this.produced = '';
2573       this.preedit = '';
2574       this.preedit_saved = '';
2575       this.candidate_table.clear ();
2576       this.candidates = null;
2577       this.candidate_show = false;
2578       for (var elt in this.marker_positions)
2579         this.marker_positions[elt] = 0;
2580       this.shift (this.initial_state);
2581     },
2582
2583     catch_args: new Array (Xex.CatchTag._mimtag, null),
2584
2585     take_actions: function (actions)
2586     {
2587       var func_progn = this.domain.GetFunc ('progn');
2588       var func_catch = this.domain.GetFunc ('catch');
2589       this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2590       var term = new Xex.Funcall (func_catch, null, this.catch_args);
2591       term = term.Eval (this.domain);
2592       return (! term.IsSymbol || term.val != '@mimtag');
2593     },
2594
2595     GetSurroundingChar: function (pos)
2596     {
2597       if (pos < 0)
2598         {
2599           pos += this.range[0];
2600           if (pos < 0)
2601             return 0;
2602         }
2603       else
2604         {
2605           pos += this.range[1];
2606           if (pos >= this.target.value.length)
2607             return 0;
2608         }
2609       return this.target.value.charCodeAt (pos);
2610     },
2611     
2612     DelSurroundText: function (pos)
2613     {
2614       var text;
2615       if (pos < 0)
2616         {
2617           pos += this.range[0];
2618           if (pos <= 0)
2619             {
2620               pos = 0; text = '';
2621             }
2622           else
2623             text = this.target.value.substring (0, pos);
2624           if (this.range[0] < this.target.value.length)
2625             text += this.target.value.substring (this.range[0]);
2626           this.target.value = text;
2627           this.range[1] -= this.range[0] - pos;
2628           this.range[0] = pos;
2629         }
2630       else
2631         {
2632           pos += this.range[1];
2633           text = this.target.value.substring (0, this.range[1]);
2634           if (pos >= this.target.value.length)
2635             pos = this.target.value.length;
2636           else
2637             text += this.target.value.substring (pos);
2638           this.target.value = text;
2639         }
2640     },
2641
2642     adjust_markers: function (from, to, inserted)
2643     {
2644       var diff = inserted - (to - from);
2645
2646       for (var name in this.marker_positions)
2647         {
2648           var pos = this.marker_positions[name];
2649           if (pos > from)
2650             {
2651               if (pos >= to)
2652                 this.marker_positions[name] += diff;
2653               else if (pos > from)
2654                 this.marker_positions[name] = from;
2655             }
2656         }
2657       if (this.cursor_pos >= to)
2658         set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2659       else if (this.cursor_pos > from)
2660         set_cursor.call (this, 'adjust', from)
2661     },
2662
2663     preedit_replace: function (from, to, text, candidates)
2664     {
2665       this.preedit = (this.preedit.substring (0, from)
2666                       + text + this.preedit.substring (to));
2667       this.adjust_markers (from, to, text.length);
2668       this.candidate_table.adjust (from, to, text.length);
2669       if (candidates)
2670         this.candidate_table.put (from, from + text.length, candidates)
2671     },
2672
2673     ins: function (text, candidates)
2674     {
2675       this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2676       this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2677     },
2678
2679     rep: function (old_text, new_text, candidates)
2680     {
2681       this.preedit_replace (this.cursor_pos - old_text.length,
2682                             this.cursor_pos, new_text, candidates);
2683       this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2684     },
2685
2686     del: function (pos)
2687     {
2688       var deleted = pos - this.cursor_pos;
2689       if (pos < this.cursor_pos)
2690         {
2691           if (pos < 0)
2692             {
2693               this.DelSurroundText (pos);
2694               deleted = - this.cursor_pos;
2695               pos = 0;
2696             }
2697           if (pos < this.cursor_pos)
2698             this.preedit_replace (pos, this.cursor_pos, '', null);
2699         }
2700       else
2701         {
2702           if (pos > this.preedit.length)
2703             {
2704               this.DelSurroundText (pos - this.preedit.length);
2705               deleted = this.preedit.length - this.cursor_pos;
2706               pos = this.preedit.length;
2707             }
2708           if (pos > this.cursor_pos)
2709             this.preedit_replace (this.cursor_pos, pos, '', null);
2710         }
2711       if (deleted != 0)
2712         this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2713       return deleted;
2714     },
2715
2716     show: function ()
2717     {
2718       this.candidate_show = true;
2719       this.changed |= MIM.ChangedStatus.CandidateShow;
2720     },
2721
2722     hide: function ()
2723     {
2724       this.candidate_show = false;
2725       this.changed |= MIM.ChangedStatus.CandidateShow;
2726     },
2727
2728     move: function (pos)
2729     {
2730       if (pos < 0)
2731         pos = 0;
2732       else if (pos > this.preedit.length)
2733         pos = this.preedit.length;
2734       if (pos != this.cursor_pos)
2735         {
2736           set_cursor.call (this, 'move', pos);
2737           this.changed |= MIM.ChangedStatus.Preedit;
2738         }
2739     },
2740
2741     pushback: function (n)
2742     {
2743       if (n instanceof MIM.KeySeq)
2744         {
2745           if (this.key_head > 0)
2746             this.key_head--;
2747           if (this.key_head < this.keys.val.length)
2748             this.keys.val.splice (this.key_head,
2749                                   this.keys.val.length - this.key_head);
2750           for (var i = 0; i < n.val.length; i++)
2751             this.keys.val.push (n.val[i]);
2752           return;
2753         }
2754       if (n > 0)
2755         {
2756           this.key_head -= n;
2757           if (this.key_head < 0)
2758             this.key_head = 0;
2759         }
2760       else if (n == 0)
2761         this.key_head = 0;
2762       else
2763       {
2764         this.key_head = - n;
2765         if (this.key_head > this.keys.val.length)
2766           this.key_head = this.keys.val.length;
2767       }
2768     },
2769
2770     pop: function ()
2771     {
2772       if (this.key_head < this.keys.val.length)
2773         this.keys.val.splice (this.key_head, 1);
2774     },
2775
2776     commit: function ()
2777     {
2778       if (this.preedit.length > 0)
2779       {
2780         this.candidate_table.clear ();
2781         this.produced += this.preedit;
2782         this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2783       }
2784     },
2785
2786     shift: function (state)
2787     {
2788       if (state == null)
2789         {
2790           if (this.prev_state == null)
2791             return;
2792           state = this.prev_state;
2793         }
2794
2795       if (state == this.initial_state)
2796         {
2797           if (this.state)
2798             {
2799               this.commit ();
2800               this.keys.val.splice (0, this.key_head);
2801               this.key_head = 0;
2802               this.prev_state = null;
2803             }
2804         }
2805       else
2806         {
2807           if (state != this.state)
2808             this.prev_state = this.state;
2809         }
2810       if (state != this.state && state.enter_actions)
2811         this.take_actions (state.enter_actions);
2812       if (! this.state || this.state.title != state.title)
2813         this.changed |= MIM.ChangedStatus.StateTitle;
2814       this.state = state;
2815       this.keymap = state.keymap;
2816       this.state_key_head = this.key_head;
2817       save_state.call (this);
2818     },
2819
2820     Filter: function (key)
2821     {
2822       if (! this.active)
2823         {
2824           this.key_unhandled = true;
2825           this.unhandled_key = key;
2826           return false;
2827         }
2828       if (key.key == '_reload')
2829         return true;
2830       this.changed = MIM.ChangedStatus.None;
2831       this.produced = '';
2832       this.key_unhandled = false;
2833       this.keys.val.push (key);
2834       var count = 0;
2835       while (this.key_head < this.keys.val.length)
2836         {
2837           if (! handle_key.call (this))
2838             {
2839               if (this.key_head < this.keys.val.length)
2840                 {
2841                   this.unhandled_key = this.keys.val[this.key_head];
2842                   this.keys.val.splice (this.key_head, this.key_head + 1);
2843                 }
2844               this.key_unhandled = true;
2845               break;
2846             }
2847           if (++count == 10)
2848             {
2849               this.reset ();
2850               this.key_unhandled = true;
2851               break;
2852             }
2853         }
2854       if (this.key_unhandled)
2855         {
2856           this.keys.val.length = 0;
2857           this.key_head = this.state_key_head = this.commit_key_head = 0;
2858         }
2859       return (! this.key_unhandled
2860               && this.produced.length == 0
2861               && this.preedit.length == 0);
2862     }
2863   }
2864
2865   MIM.IC.prototype = proto;
2866
2867   var node = Xex.Load (null, "imlist.xml");
2868   for (node = node.firstChild; node; node = node.nextSibling)
2869     if (node.nodeName == 'input-method')
2870       {
2871         var lang = null, name = null, extra_id = null, file = null;
2872
2873         for (var n = node.firstChild; n; n = n.nextSibling)
2874           {
2875             if (n.nodeName == 'language')
2876               lang = n.firstChild.nodeValue;
2877             else if (n.nodeName == 'name')
2878               name = n.firstChild.nodeValue;
2879             else if (n.nodeName == 'extra-id')
2880               extra_id = n.firstChild.nodeValue;
2881             else if (n.nodeName == 'filename')
2882               file = n.firstChild.nodeValue;
2883           }
2884         if (name && name != 'nil')
2885           {
2886             if (! MIM.imlist[lang])
2887               MIM.imlist[lang] = {};
2888             MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
2889           }
2890         else if (extra_id && extra_id != 'nil')
2891           {
2892             if (! MIM.imextra[lang])
2893               MIM.imextra[lang] = {};
2894             MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
2895           }
2896       }
2897   if (MIM.imextra.t && MIM.imextra.t.global)
2898     MIM.im_global = MIM.imextra.t.global;
2899   else
2900     {
2901       MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
2902       MIM.im_global.load_status = MIM.LoadStatus.Error;
2903     }
2904   node = undefined;
2905 }) ();
2906
2907 (function () {
2908   var keys = new Array ();
2909   keys[0x09] = 'tab';
2910   keys[0x08] = 'backspace';
2911   keys[0x0D] = 'return';
2912   keys[0x1B] = 'escape';
2913   keys[0x20] = 'space';
2914   keys[0x21] = 'pageup';
2915   keys[0x22] = 'pagedown';
2916   keys[0x23] = 'end';
2917   keys[0x24] = 'home';
2918   keys[0x25] = 'left';
2919   keys[0x26] = 'up';
2920   keys[0x27] = 'right';
2921   keys[0x28] = 'down';
2922   keys[0x2D] = 'insert';
2923   keys[0x2E] = 'delete';
2924   for (var i = 1; i <= 12; i++)
2925     keys[111 + i] = "f" + i;
2926   keys[0x90] = "numlock";
2927   keys[0xF0] = "capslock";
2928
2929   var keyids = {};
2930   keyids['U+0008'] = 'backspace';
2931   keyids['U+0009'] = 'tab';
2932   keyids['U+0018'] = 'cancel';
2933   keyids['U+001B'] = 'escape';
2934   keyids['U+0020'] = 'space';
2935   keyids['U+007F'] = 'delete';
2936
2937   var modifiers = {}
2938   modifiers.Shift = 1;
2939   modifiers.Control = 1;
2940   modifiers.Alt = 1;
2941   modifiers.AltGraph = 1;
2942   modifiers.Meta = 1
2943
2944   MIM.decode_key_event = function (event)
2945   {
2946     var key = event.keyIdentifier;
2947
2948     if (key)                    // keydown event of Chrome
2949       {
2950         if (modifiers[key])
2951           return false;
2952         var mod = '';
2953         if (event.ctrlKey) mod += 'C-';
2954         if (event.metaKey) mod += 'M-';
2955         if (event.altKey) mod += 'A-';
2956         var keysym = keyids[key];
2957         if (keysym)
2958           key = keysym;
2959         else if (key.match(/^U\+([0-9A-Z]+)$/))
2960           {
2961             if (mod.length == 0)
2962               return false;
2963             key = String.fromCharCode (parseInt (RegExp.$1, 16));
2964           }
2965         else
2966           key = key.toLowerCase ();
2967         if (event.shiftKey) mod += 'S-';
2968         return new MIM.Key (mod + key);
2969       }
2970     else
2971       {
2972         key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
2973                : event.charCode ? event.charCode
2974                : false);
2975         if (! key)
2976           return false;
2977         if (event.type == 'keydown')
2978           {
2979             key = keys[key];
2980             if (! key)
2981               return false;
2982             if (event.shiftKey) key = "S-" + key ;
2983           }
2984         else
2985           key = String.fromCharCode (key);
2986       }
2987     if (event.altKey) key = "A-" + key ;
2988     if (event.ctrlKey) key = "C-" + key ;
2989     return new MIM.Key (key);
2990   }
2991 }) ();
2992
2993 MIM.add_event_listener
2994   = (window.addEventListener
2995      ? function (target, type, listener) {
2996        target.addEventListener (type, listener, false);
2997      }
2998      : window.attachEvent
2999      ? function (target, type, listener) {
3000        target.attachEvent ('on' + type,
3001                            function() {
3002                              listener.call (target, window.event);
3003                            });
3004      }
3005      : function (target, type, listener) {
3006        target['on' + type]
3007          = function (e) { listener.call (target, e || window.event); };
3008      });
3009
3010 MIM.debug_print = function (event, ic)
3011 {
3012   if (! MIM.debug)
3013     return;
3014   if (! MIM.debug_nodes)
3015     {
3016       MIM.debug_nodes = new Array ();
3017       MIM.debug_nodes['status0'] = document.getElementById ('status0');
3018       MIM.debug_nodes['status1'] = document.getElementById ('status1');
3019       MIM.debug_nodes['keydown'] = document.getElementById ('keydown');
3020       MIM.debug_nodes['keypress'] = document.getElementById ('keypress');
3021       MIM.debug_nodes['keymap0'] = document.getElementById ('keymap0');
3022       MIM.debug_nodes['keymap1'] = document.getElementById ('keymap1');
3023       MIM.debug_nodes['preedit0'] = document.getElementById ('preedit0');
3024       MIM.debug_nodes['preedit1'] = document.getElementById ('preedit1');
3025     }
3026   var target = event.target;
3027   var code = event.keyCode;
3028   var ch = event.type == 'keypress' ? event.charCode : 0;
3029   var key = MIM.decode_key_event (event);
3030   var index;
3031
3032   MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + ":" + key + '/' + event.keyIdentifier;
3033   index = (event.type == 'keydown' ? '0' : '1');
3034   if (ic)
3035     MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status;
3036   else
3037     MIM.debug_nodes['status' + index].innerHTML = 'no IM';
3038   MIM.debug_nodes['keymap' + index].innerHTML = ic.state.name;
3039   MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit;
3040   if (index == 0)
3041     {
3042       MIM.debug_nodes.keypress.innerHTML = '';
3043       MIM.debug_nodes.status1.innerHTML = '';
3044       MIM.debug_nodes.keymap1.innerHTML = '';
3045       MIM.debug_nodes.preedit1.innerHTML = ''
3046     }
3047 };
3048
3049 MIM.get_range = function (target, ic)
3050 {
3051   var from, to;
3052   if (target.selectionStart != null) // for Mozilla
3053     {
3054       from = target.selectionStart;
3055       to = target.selectionEnd;
3056     }
3057   else                          // for IE
3058     {
3059       var r = document.selection.createRange ();
3060       var rr = r.duplicate ();
3061
3062       rr.moveToElementText (target);
3063       rr.setEndPoint ('EndToEnd', range);
3064       from = rr.text.length - r.text.length;
3065       to = rr.text.length;
3066     }
3067   if (ic.range[0] == from && ic.range[1] == to
3068       && (to == from || target.value.substring (from, to) == ic.preedit))
3069     return true;
3070   ic.range[0] = from;
3071   ic.range[1] = to;
3072   return false;
3073 }
3074
3075 MIM.set_caret = function (target, ic)
3076 {
3077   if (target.setSelectionRange) // Mozilla
3078     {
3079       var scrollTop = target.scrollTop;
3080       target.setSelectionRange (ic.range[0], ic.range[1]);
3081       target.scrollTop = scrollTop;
3082     }
3083   else                          // IE
3084     {
3085       var range = target.createTextRange ();
3086       range.moveStart ('character', ic.range[0]);
3087       range.moveEnd ('character', ic.range[1]);
3088       range.select ();
3089     }
3090 };
3091
3092 MIM.ignore_focus = false;
3093
3094 MIM.update = function (target, ic)
3095 {
3096   var text = target.value;
3097   target.value = (text.substring (0, ic.range[0])
3098                   + ic.produced
3099                   + ic.preedit
3100                   + text.substring (ic.range[1]));
3101   ic.range[0] += ic.produced.length;
3102   ic.range[1] = ic.range[0] + ic.preedit.length;
3103   MIM.set_caret (target, ic);
3104 };
3105
3106 MIM.focus_in = function (event)
3107 {
3108   var target = event.target;
3109   var ic = target.mim_ic;
3110   if (ic)
3111     {
3112       if (target.mim_ignore_focus_in)
3113         {
3114           // Ignore this event which happened in setSelectionRange ().
3115           target.mim_ignore_focus_in = false;
3116           event.preventDefault ();
3117         }
3118       else
3119         {
3120           try {
3121             ic.Filter (MIM.Key.FocusIn);
3122             MIM.update (target, ic);
3123             // Ignore further focus-in caused by setSelectionRange ().
3124             target.mim_ignore_focus_in = true;
3125           }
3126           catch (e) { Xex.Log ('Error:' + e); throw (e); }
3127         }
3128     }
3129 }
3130
3131 MIM.focus_out = function (event)
3132 {
3133   var target = event.target;
3134   var ic = target.mim_ic;
3135   if (ic)
3136     {
3137       if (target.mim_ignore_focus_out)
3138         {
3139           // Ignore this event which caused by setSelectionRange ().
3140           target.mim_ignore_focus_out = false;
3141           event.preventDefault ();
3142         }
3143       else
3144         {
3145           try {
3146             if (! MIM.get_range (target, ic))
3147               ic.reset ();
3148             ic.Filter (MIM.Key.FocusOut);
3149             MIM.update (target, ic);
3150             // Ignore further focus-out caused by setSelectionRange ().
3151             target.mim_ignore_focus_out = true;
3152           }
3153           catch (e) { Xex.Log ('Error:' + e); throw (e); }
3154         }
3155     }
3156 };
3157
3158 MIM.keydown = function (event)
3159 {
3160   var target = event.target;
3161   if (target.id == 'log')
3162     return;
3163   if (! (target.type == "text" || target.type == "textarea"))
3164     return;
3165   document.akey = event;
3166
3167   var ic = target.mim_ic;
3168   if (! ic || ic.im != MIM.current)
3169     {
3170       target.mim_ic = null;
3171       Xex.Log ('creating IC');
3172       ic = new MIM.IC (MIM.current, target);
3173       if (ic.im.load_status != MIM.LoadStatus.Loaded)
3174         return;
3175       target.mim_ic = ic;
3176       MIM.add_event_listener (target, 'focus', MIM.focus_in);
3177       MIM.add_event_listener (target, 'blur', MIM.focus_out);
3178       MIM.get_range (target, ic)
3179     }
3180   else
3181     {
3182       if (! MIM.get_range (target, ic))
3183         ic.reset ();
3184     }
3185   MIM.debug_print (event, ic);
3186   ic.key = MIM.decode_key_event (event);
3187   if (ic.key)
3188     {
3189       Xex.Log ("filtering " + ic.key);
3190       try {
3191         var result = ic.Filter (ic.key);
3192       } catch (e) {
3193         Xex.Log ('Error' + e);
3194         throw (e);
3195       }
3196       MIM.update (target, ic);
3197       if (! ic.key_unhandled)
3198         event.preventDefault ();
3199     }
3200 };
3201
3202 MIM.keypress = function (event)
3203 {
3204   var target = event.target;
3205   if (target.id == 'log')
3206     return;
3207   if (! (target.type == "text" || target.type == "textarea"))
3208     return;
3209
3210   var ic = target.mim_ic;
3211   var i;
3212
3213   try {
3214     if (ic.im.load_status != MIM.LoadStatus.Loaded)
3215       return;
3216     if (! ic.key)
3217       ic.key = MIM.decode_key_event (event);
3218     if (! ic.key)
3219       {
3220         ic.reset ();
3221         return;
3222       }
3223     
3224     Xex.Log ("filtering " + ic.key);
3225     try {
3226       var result = ic.Filter (ic.key);
3227     } catch (e) {
3228       Xex.Log ('Error;' + e);
3229       throw (e);
3230     }
3231     MIM.update (target, ic);
3232     if (! ic.key_unhandled)
3233       event.preventDefault ();
3234   } catch (e) {
3235     Xex.Log ("error:" + e);
3236     event.preventDefault ();
3237   } finally {
3238     MIM.debug_print (event, ic);
3239   }
3240   return;
3241 };
3242
3243 (function () {
3244   var lang_category = {
3245     European: {
3246       cs: { name: 'Czech' },
3247       da: { name: 'Danish' },
3248       el: { name: 'Greek' },
3249       en: { name: 'English' },
3250       eo: { name: 'Esperanto' },
3251       fr: { name: 'French' },
3252       grc: { name: 'ClassicGreek' },
3253       hr: { name: 'Croatian' },
3254       hy: { name: 'Armenian' },
3255       ka: { name: 'Georgian' },
3256       kk: { name: 'Kazakh' },
3257       ru: { name: 'Russian' },
3258       sk: { name: 'Slovak' },
3259       sr: { name: 'Serbian' },
3260       sv: { name: 'Swedish' },
3261       vi: { name: 'Vietnamese' },
3262       yi: { name: 'Yiddish' } },
3263     MiddleEast: {
3264       ar: { name: 'Arabic' },
3265       dv: { name: 'Divehi' },
3266       fa: { name: 'Persian' },
3267       he: { name: 'Hebrew' },
3268       kk: { name: 'Kazakh' },
3269       ps: { name: 'Pushto' },
3270       ug: { name: 'Uighur' },
3271       yi: { name: 'Yiddish' } },
3272     SouthAsia: {
3273       as: { name: 'Assamese' },
3274       bn: { name: 'Bengali' },
3275       bo: { name: 'Tibetan' },
3276       gu: { name: 'Gujarati' },
3277       hi: { name: 'Hindi' },
3278       kn: { name: 'Kannada' },
3279       ks: { name: 'Kashmiri' },
3280       ml: { name: 'Malayalam' },
3281       mr: { name: 'Marathi' },
3282       ne: { name: 'Nepali' },
3283       or: { name: 'Oriya' },
3284       pa: { name: 'Panjabi' },
3285       sa: { name: 'Sanskirit' },
3286       sd: { name: 'Sindhi' },
3287       si: { name: 'Sinhalese' },
3288       ta: { name: 'Tamil' },
3289       te: { name: 'Telugu' },
3290       ur: { name: 'Urdu' } },
3291     SouthEastAsia: {
3292       cmc: { name: 'Cham' },
3293       km: { name: 'Khmer'},
3294       lo: { name: 'Lao' },
3295       my: { name: 'Burmese' },
3296       tai: { name: 'Tai Viet' },
3297       th: { name: 'Thai' },
3298       vi: { name: 'Vietanamese' } },
3299     EastAsia: {
3300       ii: { name: 'Yii' },
3301       ja: { name: 'Japanese' },
3302       ko: { name: 'Korean' },
3303       zh: { name: 'Chinese' } },
3304     Other: {
3305       am: { name:  'Amharic' },
3306       ath: { name: 'Carrier' },
3307       bla: { name: 'Blackfoot' },
3308       cr: { name: 'Cree' },
3309       eo: { name: 'Esperanto' },
3310       iu: { name: 'Inuktitut' },
3311       nsk: { name: 'Naskapi' },
3312       oj: { name: 'Ojibwe' },
3313       t: { name: 'Generic' } }
3314   };
3315
3316   function categorize_im ()
3317   {
3318     var cat, lang, list, name;
3319     for (lang in MIM.imlist)
3320       {
3321         list = null;
3322         for (cat in lang_category)
3323           if (lang_category[cat][lang])
3324             {
3325               list = lang_category[cat][lang].list;
3326               if (! list)
3327                 list = lang_category[cat][lang].list = {};
3328               break;
3329             }
3330         if (list)
3331           for (name in MIM.imlist[lang])
3332             list[name] = MIM.imlist[lang][name];
3333         else
3334           for (name in MIM.imlist[lang])
3335             Xex.Log ('no category ' + lang + '-' + name);
3336       }
3337   }
3338
3339   var destroy_timer;
3340   var last_target;
3341
3342   function destroy ()
3343   {
3344     clearTimeout (destroy_timer);
3345     destroy_timer = null;
3346     var target = document.getElementById ('mim-menu');
3347     if (target)
3348       {
3349         for (; last_target && last_target.menu_level;
3350              last_target = last_target.parentLi)
3351           last_target.style.backgroundColor = 'white';
3352         var nodes = target.getElementsByTagName ('ul');
3353         for (var i = 0; i < nodes.length; i++)
3354           nodes[i].style.visibility = 'hidden';
3355         document.getElementsByTagName ('body')[0].removeChild (target);
3356       }
3357   }    
3358
3359   function destroy_menu () {
3360     if (! destroy_timer)
3361       destroy_timer = setTimeout (destroy, 1000);
3362   }
3363
3364   function show_submenu (event)
3365   {
3366     if (destroy_timer)
3367       {
3368         clearTimeout (destroy_timer);
3369         destroy_timer = null;
3370       }
3371     var target = event.target;
3372     if (! target.menu_level)
3373       return;
3374     if (last_target && target.parentLi != last_target)
3375       {
3376         last_target.style.backgroundColor = 'white';
3377         if (target.menu_level < last_target.menu_level)
3378           {
3379             last_target = last_target.parentLi;
3380             last_target.style.backgroundColor = 'white';
3381           }
3382         var uls = last_target.getElementsByTagName ('ul');
3383         for (var i = 0; i < uls.length; i++)
3384           uls[i].style.visibility = 'hidden';
3385       }
3386     last_target = target;
3387     target.style.backgroundColor = 'yellow';
3388     if (target.menu_level < 3)
3389       {
3390         target.lastChild.style.visibility = 'visible';
3391         target.lastChild.style.left = target.clientWidth + 'px';
3392       }
3393     event.preventDefault ();    
3394   }
3395
3396   function select_im (event)
3397   {
3398     var target = event.target;
3399     if (target.im)
3400       {
3401         MIM.current = target.im;
3402         destroy ();
3403       }
3404     event.preventDefault ();
3405   }
3406
3407   function create_ul (visibility)
3408   {
3409     var ul = document.createElement ('ul');
3410     ul.style.position = 'absolute';
3411     ul.style.margin = '0px';
3412     ul.style.padding = '0px';
3413     ul.style.border = '1px solid gray';
3414     ul.style.borderBottom = 'none';
3415     ul.style.top = '-1px';
3416     ul.style.backgroundColor = 'white';
3417     ul.style.visibility = visibility;
3418     return ul;
3419   }
3420
3421   function create_li (level, text)
3422   {
3423     var li = document.createElement ('li');
3424     li.style.position = 'relative';
3425     li.style.margin = '0px';
3426     li.style.padding = '1px';
3427     li.style.borderBottom = '1px solid gray';
3428     li.style.top = '0px';
3429     li.style.listStyle = 'none';
3430     li.menu_level = level;
3431     li.appendChild (document.createTextNode (text));
3432     return li;
3433   }
3434
3435   var menu;
3436
3437   function create_menu (event)
3438   {
3439     var target = event.target;
3440
3441     if (! ((target.type == "text" || target.type == "textarea")
3442            && event.which == 1 && event.ctrlKey))
3443       return;
3444     if (! menu)
3445       {
3446         categorize_im ();
3447         menu = create_ul ('visible');
3448         menu.style.fontFamily = 'sans-serif';
3449         menu.style.fontWeight = 'bold';
3450         menu.id = 'mim-menu';
3451         menu.onclick = select_im;
3452         menu.onmouseover = show_submenu;
3453         menu.onmouseout = destroy_menu;
3454         for (var catname in lang_category)
3455           {
3456             var cat = lang_category[catname];
3457             var li = create_li (1, catname);
3458             var sub = create_ul ('hidden');
3459             for (var langname in cat)
3460               {
3461                 var lang = cat[langname];
3462                 if (! lang.list)
3463                   continue;
3464                 var sub_li = create_li (2, lang.name);
3465                 sub_li.parentLi = li;
3466                 var subsub = create_ul ('hidden');
3467                 for (var name in lang.list)
3468                   {
3469                     var im = lang.list[name];
3470                     var subsub_li = create_li (3, im.name);
3471                     subsub_li.parentLi = sub_li;
3472                     subsub_li.im = im;
3473                     subsub.appendChild (subsub_li);
3474                   }
3475                 sub_li.appendChild (subsub);
3476                 sub.appendChild (sub_li);
3477               }
3478             li.appendChild (sub);
3479             menu.appendChild (li);
3480           }
3481         document.mimmenu = menu;
3482         lang_category = null;
3483       }
3484     menu.style.left = (event.clientX - 10) + "px";
3485     menu.style.top = (event.clientY - 10) + "px";
3486     document.getElementsByTagName ('body')[0].appendChild (menu);
3487   };
3488
3489   MIM.init = function ()
3490   {
3491     MIM.add_event_listener (window, 'keydown', MIM.keydown);
3492     MIM.add_event_listener (window, 'keypress', MIM.keypress);
3493     MIM.add_event_listener (window, 'mousedown', create_menu);
3494     if (window.location == 'http://localhost/mim/index.html')
3495       MIM.server = 'http://localhost/mim';
3496     MIM.current = MIM.imlist['vi']['telex'];
3497   };
3498 }) ();
3499
3500 MIM.test = function ()
3501 {
3502   var im = MIM.imlist['t']['latn-post'];
3503   var ic = new MIM.IC (im, null);
3504
3505   ic.Filter (new MIM.Key ('a'));
3506   ic.Filter (new MIM.Key ("'"));
3507
3508   if (true)
3509     document.getElementById ('text').value = ic.produced + ic.preedit;
3510   else {
3511     try {
3512       document.getElementById ('text').value
3513         = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3514     } catch (e) {
3515       if (e instanceof Xex.ErrTerm)
3516         alert (e);
3517       throw e;
3518     }
3519   }
3520 }
3521
3522
3523 MIM.init_debug = function ()
3524 {
3525   MIM.debug = true;
3526   Xex.LogNode = document.getElementById ('log');
3527   Xex.Log (null);
3528   MIM.init ();
3529 };