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