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