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