*** empty log message ***
[m17n/m17n-lib-js.git] / xex.js
1 // -* coding: utf-8; -*
2
3 var Xex = {
4   LogNode: null,
5   Log: function (arg, indent)
6   {
7     if (! Xex.LogNode)
8       return;
9     if (! arg)
10       Xex.LogNode.value = '';
11     else
12       {
13         var str = '';
14         if (indent != undefined)
15           for (var i = 0; i <= indent; i++)
16             str += '  ';
17         Xex.LogNode.value += "\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-group"] = new MIM.Selector ('@[');
1609   selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
1610
1611   MIM.Selector.prototype.Parser = function (domain, node)
1612   {
1613     var name = node.firstChild.nodeValue;
1614     var s = selectors[name];
1615     if (! s)
1616       throw new Xex.ErrTerm (MIM.Error.ParseError,
1617                              "Invalid selector: " + name);
1618     return s;
1619   }
1620 }) ();
1621
1622 MIM.Rule = function (keyseq, actions)
1623 {
1624   this.keyseq = keyseq;
1625   this.actions = actions;
1626 }
1627 MIM.Rule.prototype = new Xex.Term ('rule');
1628 MIM.Rule.prototype.Parser = function (domain, node)
1629 {
1630   var n;
1631   for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
1632   if (! n)
1633     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1634   var keyseq = Xex.Term.Parse (domain, n);
1635   if (keyseq.type != 'keyseq')
1636     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
1637   var actions = Xex.Term.Parse (domain, n.nextElement (), null);
1638   return new MIM.Rule (keyseq, actions);
1639 }
1640 MIM.Rule.prototype.toString = function ()
1641 {
1642   return '<rule/>';
1643 }
1644
1645 MIM.Map = function (name)
1646 {
1647   this.name = name;
1648   this.rules = new Array ();
1649 };
1650
1651 (function () {
1652   var proto = new Xex.Term ('map');
1653
1654   proto.Parser = function (domain, node)
1655   {
1656     var name = node.attributes['mname'].nodeValue;
1657     if (! name)
1658       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1659     var map = new MIM.Map (name);
1660     for (var n = node.firstChild; n; n = n.nextSibling)
1661       if (n.nodeType == 1)
1662         map.rules.push (Xex.Term.Parse (domain, n));
1663     return map;
1664   }
1665
1666   proto.toString = function ()
1667   {
1668     var str = '<map mname="' + this.name + '">';
1669     var len = this.rules.length;
1670     for (i = 0; i < len; i++)
1671       str += this.rules[i];
1672     return str + '</map>';
1673   }
1674
1675   MIM.Map.prototype = proto;
1676 }) ();
1677
1678 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
1679
1680 MIM.Action = function (domain, terms)
1681 {
1682   var args = new Array ();
1683   args.push (Xex.CatchTag_.mimtag);
1684   for (var i = 0; i < terms.length; i++)
1685     args.push (terms[i]);
1686   this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
1687 }
1688
1689 MIM.Action.prototype.Run = function (domain)
1690 {
1691   var result = this.action.Eval (domain);
1692   if (result.type == 'error')
1693     {
1694       domain.context.Error = result.toString ();
1695       return false;
1696     }
1697   return (result != Xex.CatchTag._mimtag);
1698 }
1699
1700 MIM.Keymap = function ()
1701 {
1702   this.name = 'TOP';
1703   this.submaps = null;
1704 };
1705
1706 (function () {
1707   var proto = {};
1708
1709   function add_rule (keymap, rule, branch_actions)
1710   {
1711     var keyseq = rule.keyseq;
1712     var len = keyseq.val.length;
1713     var name = '';
1714
1715     for (var i = 0; i < len; i++)
1716       {
1717         var key = keyseq.val[i];
1718         var sub = false;
1719
1720         name += key.key;
1721         if (! keymap.submaps)
1722           keymap.submaps = {};
1723         else
1724           sub = keymap.submaps[key.key];
1725         if (! sub)
1726           keymap.submaps[key.key] = sub = new MIM.Keymap ();
1727         keymap = sub;
1728         keymap.name = name;
1729       }
1730     keymap.map_actions = rule.actions;
1731     keymap.branch_actions = branch_actions;
1732   }
1733
1734   proto.Add = function (map, branch_actions)
1735   {
1736     var rules = map.rules;
1737     var len = rules.length;
1738
1739     for (var i = 0; i < len; i++)
1740       add_rule (this, rules[i], branch_actions);
1741   }
1742   proto.Lookup = function (keys, index)
1743   {
1744     var sub;
1745
1746     if (index < keys.val.length && this.submaps
1747         && ! keys.val[index])
1748       {
1749         Xex.Log ('invalid key at ' + index);
1750         throw 'invalid key';
1751       }
1752
1753     if (index < keys.val.length && this.submaps
1754         && (sub = this.submaps[keys.val[index].key]))
1755       {
1756         index++;
1757         return sub.Lookup (keys, index);
1758       }
1759     return { map: this, index: index };
1760   }
1761
1762   MIM.Keymap.prototype = proto;
1763 }) ();
1764
1765 MIM.State = function (name)
1766 {
1767   this.name = name;
1768   this.keymap = new MIM.Keymap ();
1769 };
1770
1771 (function () {
1772   var proto = new Xex.Term ('state');
1773
1774   proto.Parser = function (domain, node)
1775   {
1776     var map_list = domain.map_list;
1777     var name = node.attributes['sname'].nodeValue;
1778     if (! name)
1779       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
1780     var state = new MIM.State (name);
1781     for (node = node.firstElement (); node; node = node.nextElement ())
1782       {
1783         if (node.nodeName == 'title')
1784           state.title = node.firstChild.nodeValue;
1785         else
1786           {
1787             var n = node.firstElement ();
1788             if (node.nodeName == 'branch')
1789               state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
1790                                 Xex.Term.Parse (domain, n, null));
1791             else if (node.nodeName == 'state-hook')
1792               state.enter_actions = Xex.Term.Parse (domain, n, null);
1793             else if (node.nodeName == 'catch-all-branch')
1794               state.fallback_actions = Xex.Term.Parse (domain, n, null);
1795           }
1796       }
1797     return state;
1798   }
1799
1800   proto.toString = function ()
1801   {
1802     return '<state sname="' + this.name + '">' + this.keymap + '</state>';
1803   }
1804
1805   MIM.State.prototype = proto;
1806 }) ();
1807
1808 (function () {
1809   function Block (index, term)
1810   {
1811     this.Index = index;
1812     if (term.IsStr)
1813       this.Data = term.val;
1814     else if (term.IsList)
1815       {
1816         this.Data = new Array ();
1817         for (var i = 0; i < term.val.length; i++)
1818           this.Data.push (term.val[i].val);
1819       }
1820   }
1821
1822   Block.prototype.Count = function () { return this.Data.length; }
1823   Block.prototype.get = function (i)
1824   {
1825     return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
1826   }
1827
1828   MIM.Candidates = function (candidates, column)
1829   {
1830     this.column = column;
1831     this.row = 0;
1832     this.index = 0;
1833     this.total = 0;
1834     this.blocks = new Array ();
1835
1836     for (var i = 0; i < candidates.length; i++)
1837       {
1838         var block = new Block (this.total, candidates[i]);
1839         this.blocks.push (block);
1840         this.total += block.Count ();
1841       }
1842   }
1843
1844   function get_col ()
1845   {
1846     return (this.column > 0 ? this.index % this.column
1847             : this.index - this.blocks[this.row].Index);
1848   }
1849
1850   function prev_group ()
1851   {
1852     var col = get_col.call (this);
1853     var nitems;
1854     if (this.column > 0)
1855       {
1856         this.index -= this.column;
1857         if (this.index >= 0)
1858           nitems = this.column;
1859         else
1860           {
1861             var lastcol = (this.total - 1) % this.column;
1862             this.index = (col < lastcol ? this.total - lastcol + col
1863                           : this.total - 1);
1864             this.row = this.blocks.length - 1;
1865             nitems = lastcol + 1;
1866           }
1867         while (this.blocks[this.row].Index > this.index)
1868           this.row--;
1869       }
1870     else
1871       {
1872         this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
1873         nitems = this.blocks[this.row].Count ();
1874         this.index = (this.blocks[this.row].Index
1875                       + (col < nitems ? col : nitems - 1));
1876       }
1877     return nitems;
1878   }
1879
1880   function next_group ()
1881   {
1882     var col = get_col.call (this);
1883     var nitems;
1884     if (this.column > 0)
1885       {
1886         this.index += this.column - col;
1887         if (this.index < this.total)
1888           {
1889             if (this.index + col >= this.total)
1890               {
1891                 nitems = this.total - this.index;
1892                 this.index = this.total - 1;
1893               }
1894             else
1895               {
1896                 nitems = this.column;
1897                 this.index += col;
1898               }
1899           }
1900         else
1901           {
1902             this.index = col;
1903             this.row = 0;
1904           }
1905         while (this.blocks[this.row].Index > this.index)
1906           this.row++;
1907       }
1908     else
1909       {
1910         this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
1911         nitems = this.blocks[this.row].Count ();
1912         this.index = (this.blocks[this.row].Index
1913                       + (col < nitems ? col : nitems - 1));
1914       }
1915     return nitems;
1916   }
1917
1918   function prev ()
1919   {
1920     if (this.index == 0)
1921       {
1922         this.index = this.total - 1;
1923         this.row = this.blocks.length - 1;
1924       }
1925     else
1926       {
1927         this.index--;
1928         if (this.blocks[this.row].Index > this.index)
1929           this.row--;
1930       }
1931     }
1932
1933   function next ()
1934   {
1935     this.index++;
1936     if (this.index == this.total)
1937       {
1938         this.index = 0;
1939         this.row = 0;
1940       }
1941     else
1942       {
1943         var b = this.blocks[this.row];
1944         if (this.index == b.Index + b.Count ())
1945           this.row++;
1946       }
1947   }
1948
1949   function first ()
1950   {
1951     this.index -= get_col.call (this);
1952     while (this.blocks[this.row].Index > this.index)
1953       this.row--;
1954   }
1955
1956   function last ()
1957   {
1958     var b = this.blocks[this.row];
1959     if (this.column > 0)
1960       {
1961         if (this.index + 1 < this.total)
1962           {
1963             this.index += this.column - get_col.call (this) + 1;
1964             while (b.Index + b.Count () <= this.index)
1965               b = this.blocks[++this.row];
1966           }
1967       }
1968     else
1969       this.index = b.Index + b.Count () - 1;
1970   }
1971
1972   MIM.Candidates.prototype.Current = function ()
1973   {
1974     var b = this.blocks[this.row];
1975     return b.get (this.index - b.Index);
1976   }
1977
1978   MIM.Candidates.prototype.Select = function (selector)
1979   {
1980     if (selector.type == 'selector')
1981       {
1982         switch (selector.val)
1983           {
1984           case '@<': first.call (this); break;
1985           case '@>': last.call (this); break;
1986           case '@-': prev.call (this); break;
1987           case '@+': next.call (this); break;
1988           case '@[': prev_group.call (this); break;
1989           case '@]': next_group.cal (this); break;
1990           default: break;
1991           }
1992         return this.Current ();
1993       }
1994     var col, start, end
1995     if (this.column > 0)
1996       {
1997         col = this.index % this.column;
1998         start = this.index - col;
1999         end = start + this.column;
2000       }
2001     else
2002       {
2003         start = this.blocks[this.row].Index;
2004         col = this.index - start;
2005         end = start + this.blocks[this.row].Count;
2006       }
2007     if (end > this.total)
2008       end = this.total;
2009     this.index += selector.val - col;
2010     if (this.index >= end)
2011       this.index = end - 1;
2012     if (this.column > 0)
2013       {
2014         if (selector.val > col)
2015           while (this.blocks[this.row].Index + this.blocks[this.row].Count
2016                  < this.index)
2017             this.row++;
2018         else
2019           while (this.blocks[this.row].Index > this.index)
2020             this.row--;
2021       }
2022     return this.Current ();
2023   }
2024 }) ();
2025
2026 MIM.im_domain = new Xex.Domain ('input-method', null, null);
2027 MIM.im_domain.DefType (MIM.KeySeq.prototype);
2028 MIM.im_domain.DefType (MIM.Marker.prototype);
2029 MIM.im_domain.DefType (MIM.Selector.prototype);
2030 MIM.im_domain.DefType (MIM.Rule.prototype);
2031 MIM.im_domain.DefType (MIM.Map.prototype);
2032 MIM.im_domain.DefType (MIM.State.prototype);
2033
2034 (function () {
2035   var im_domain = MIM.im_domain;
2036
2037   function Finsert (domain, vari, args)
2038   {
2039     var text;
2040     if (args[0].type == 'integer')
2041       text = String.fromCharCode (args[0].val);
2042     else
2043       text = args[0].val;
2044     domain.context.ins (text, null);
2045     return args[0];
2046   }
2047
2048   function Finsert_candidates (domain, vari, args)
2049   {
2050     var ic = domain.context;
2051     var gsize = domain.variables['candidates_group_size'];
2052     var candidates = new MIM.Candidates (args, gsize ? gsize.Intval () : 0);
2053     ic.ins (candidates.Current (), candidates);
2054     return args[0];
2055   }
2056
2057   function Fdelete (domain, vari, args)
2058   {
2059     var ic = domain.context;
2060     var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
2061     return new Xex.IntTerm (ic.del (pos));
2062   }
2063
2064   function Fselect (domain, vari, args)
2065   {
2066     var ic = domain.context;
2067     var can = ic.candidates;
2068
2069     if (can)
2070       {
2071         var old_text = can.Current ();
2072         var new_text = can.Select (args[0]);
2073         ic.rep (old_text, new_text, can);
2074       }
2075     else
2076       Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
2077     return args[0];
2078   }
2079
2080   function Fshow (domain, vari, args)
2081   {
2082     domain.context.candidate_show = true;
2083     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2084     return Xex.nil;
2085   }
2086
2087   function Fhide (domain, vari, args)
2088   {
2089     domain.context.candidate_show = false;
2090     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
2091     return Xex.nil;
2092   }
2093
2094   function Fchar_at (domain, vari, args)
2095   {
2096     return new Xex.IntTerm (args[0].CharAt (domain.context));
2097   }
2098
2099   function Fmove (domain, vari, args)
2100   {
2101     var ic = domain.context;
2102     var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
2103     ic.move (pos);
2104     return new Xex.IntTerm (pos);
2105   }
2106
2107   function Fmark (domain, vari, args)
2108   {
2109     args[0].Mark (domain.context);
2110     return args[0];
2111   }
2112
2113   function Fpushback (domain, vari, args)
2114   {
2115     var a = (args[0].IsInt ? args[0].Intval ()
2116              : args[0].IsStr ? new KeySeq (args[0])
2117              : args[0]);
2118     domain.context.pushback (a);
2119     return args[0];
2120   }
2121
2122   function Fpop (domain, vari, args)
2123   {
2124     var ic = domain.context;
2125     if (ic.key_head < ic.keys.val.length)
2126       ic.keys.val.splice (ic.keys_head, 1);
2127     return Xex.nil;
2128   }
2129
2130   function Fundo  (domain, vari, args)
2131   {
2132     var ic = domain.context;
2133     var n = args.length == 0 ? -2 : args[0].val;
2134     Xex.Log ('undo with arg ' + args[0]);
2135     if (n < 0)
2136       ic.keys.val.splice (ic.keys.val.length + n, -n);
2137     else
2138       ic.keys.val.splice (n, ic.keys.val.length);
2139     ic.reset ();
2140     return Xex.nil;
2141   }
2142
2143   function Fcommit (domain, vari, args)
2144   {
2145     domain.context.commit ();
2146     return Xex.nil;
2147   }
2148
2149   function Funhandle (domain, vari, args)
2150   {
2151     domain.context.commit ();
2152     return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
2153   }
2154
2155   function Fshift (domain, vari, args)
2156   {
2157     var ic = domain.context;
2158     var state_name = args[0].val;
2159     var state = ic.im.state_list[state_name];
2160     if (! state)
2161       throw ("Unknown state: " + state_name);
2162       ic.shift (state);
2163     return args[0];
2164   }
2165
2166   function Fshiftback (domain, vari, args)
2167   {
2168     domain.context.shift (null);
2169     return Xex.nil;
2170   }
2171
2172   function Fkey_count (domain, vari, args)
2173   {
2174     return new Xex.IntTerm (domain.context.key_head);
2175   }
2176
2177   function Fsurrounding_flag (domain, vari, args)
2178   {
2179     return new Xex.IntTerm (-1);
2180   }
2181
2182   im_domain.DefSubr (Finsert, "insert", false, 1, 1);
2183   im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
2184   im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
2185   im_domain.DefSubr (Fselect, "select", false, 1, 1);
2186   im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
2187   im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
2188   im_domain.DefSubr (Fmove, "move", false, 1, 1);
2189   im_domain.DefSubr (Fmark, "mark", false, 1, 1);
2190   im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
2191   im_domain.DefSubr (Fpop, "pop", false, 0, 0);
2192   im_domain.DefSubr (Fundo, "undo", false, 0, 1);
2193   im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
2194   im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
2195   im_domain.DefSubr (Fshift, "shift", false, 1, 1);
2196   im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
2197   im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
2198   im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
2199   im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
2200 }) ();
2201
2202
2203 (function () {
2204   function get_global_var (vname)
2205   {
2206     if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
2207       MIM.im_global.Load ()
2208     return MIM.im_global.domain.variables[vname];
2209   }
2210
2211   function include (node)
2212   {
2213     node = node.firstElement ();
2214     if (node.nodeName != 'tags')
2215       return null;
2216     
2217     var lang = null, name = null, extra = null;
2218     for (node = node.firstElement (); node; node = node.nextElement ())
2219       {
2220         if (node.nodeName == 'language')
2221           lang = node.firstChild.nodeValue;
2222         else if (node.nodeName == 'name')
2223           name = node.firstChild.nodeValue;
2224         else if (node.nodeName == 'extra-id')
2225           extra = node.firstChild.nodeValue;
2226       }
2227     if (! lang || ! MIM.imlist[lang])
2228       return null;
2229     if (! extra)
2230       {
2231         if (! name || ! (im = MIM.imlist[lang][name]))
2232           return null;
2233       }
2234     else
2235       {
2236         if (! (im = MIM.imextra[lang][extra]))
2237           return null;
2238       }
2239     if (im.load_status != MIM.LoadStatus.Loaded
2240         && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
2241       return null;
2242     return im;
2243   }
2244
2245   var parsers = { };
2246
2247   parsers['description'] = function (node)
2248   {
2249     this.description = node.firstChild.nodeValue;
2250   }
2251   parsers['variable-list'] = function (node)
2252   {
2253     for (node = node.firstElement (); node; node = node.nextElement ())
2254       {
2255         var vname = node.attributes['vname'].nodeValue;
2256         if (this != MIM.im_global)
2257           {
2258             var vari = get_global_var (vname);
2259             if (vari != null)
2260               this.domain.Defvar (vname);
2261           }
2262         vname = Xex.Term.Parse (this.domain, node)
2263       }
2264   }
2265   parsers['command-list'] = function (node)
2266   {
2267   }
2268   parsers['macro-list'] = function (node)
2269   {
2270     for (var n = node.firstElement (); n; n = n.nextElement ())
2271       if (n.nodeName == 'xi:include')
2272         {
2273           var im = include (n);
2274           if (! im)
2275             alert ('inclusion fail');
2276           else
2277             for (var macro in im.domain.functions)
2278               {
2279                 var func = im.domain.functions[macro];
2280                 if (func instanceof Xex.Macro)
2281                   im.domain.CopyFunc (this.domain, macro);
2282               }
2283           n = n.previousSibling;
2284           node.removeChild (n.nextSibling);
2285         }
2286     Xex.Term.Parse (this.domain, node.firstElement (), null);
2287   }
2288   parsers['title'] = function (node)
2289   {
2290     this.title = node.firstChild.nodeValue;
2291   }
2292   parsers['map-list'] = function (node)
2293   {
2294     for (node = node.firstElement (); node; node = node.nextElement ())
2295       {
2296         if (node.nodeName == 'xi:include')
2297           {
2298             var im = include (node);
2299             if (! im)
2300               {
2301                 alert ('inclusion fail');
2302                 continue;
2303               }
2304             for (var mname in im.map_list)
2305               this.map_list[mname] = im.map_list[mname];
2306           }
2307         else
2308           {
2309             var map = Xex.Term.Parse (this.domain, node);
2310             this.map_list[map.name] = map;
2311           }
2312       }
2313   }
2314   parsers['state-list'] = function (node)
2315   {
2316     this.domain.map_list = this.map_list;
2317     for (node = node.firstElement (); node; node = node.nextElement ())
2318       {
2319         if (node.nodeName == 'xi:include')
2320           {
2321             var im = include (node);
2322             if (! im)
2323               alert ('inclusion fail');
2324             for (var sname in im.state_list)
2325               {
2326                 state = im.state_list[sname];
2327                 if (! this.initial_state)
2328                   this.initial_state = state;
2329                 this.state_list[sname] = state;
2330               }
2331           }
2332         else if (node.nodeName == 'state')
2333           {
2334             var state = Xex.Term.Parse (this.domain, node);
2335             if (! state.title)
2336               state.title = this.title;
2337             if (! this.initial_state)
2338               this.initial_state = state;
2339             this.state_list[state.name] = state;
2340           }
2341       }
2342     delete this.domain.map_list;
2343   }
2344
2345   MIM.IM = function (lang, name, extra_id, file)
2346   {
2347     this.lang = lang;
2348     this.name = name;
2349     this.extra_id = extra_id;
2350     this.file = file;
2351     this.load_status = MIM.LoadStatus.NotLoaded;
2352     this.domain = new Xex.Domain (this.lang + '-'
2353                                   + (this.name != 'nil'
2354                                      ? this.name : this.extra_id),
2355                                   MIM.im_domain, null);
2356   }
2357
2358   var proto = {
2359     Load: function ()
2360     {
2361       var node = Xex.Load (null, this.file);
2362       if (! node)
2363         {
2364           this.load_status = MIM.LoadStatus.Error;
2365           return false;
2366         }
2367       this.map_list = {};
2368       this.initial_state = null;
2369       this.state_list = {};
2370       for (node = node.firstElement (); node; node = node.nextElement ())
2371         {
2372           var name = node.nodeName;
2373           var parser = parsers[name];
2374           if (parser)
2375             parser.call (this, node);
2376         }
2377       this.load_status = MIM.LoadStatus.Loaded;
2378       return true;
2379     }
2380   }
2381
2382   MIM.IM.prototype = proto;
2383
2384   MIM.IC = function (im, target)
2385   {
2386     if (im.load_status == MIM.LoadStatus.NotLoaded)
2387       im.Load ();
2388     if (im.load_status != MIM.LoadStatus.Loaded)
2389       alert ('im:' + im.name + ' error:' + im.load_status);
2390     this.im = im;
2391     this.target = target;
2392     this.domain = new Xex.Domain ('context', im.domain, this);
2393     this.active = true;
2394     this.range = new Array ();
2395     this.range[0] = this.range[1] = 0;
2396     this.state = null;
2397     this.initial_state = this.im.initial_state;
2398     this.keys = new MIM.KeySeq ();
2399     this.marker_positions = new Array ();
2400     this.candidate_table = new MIM.CandidateTable ();
2401     this.reset ();
2402   }
2403
2404   MIM.CandidateTable = function ()
2405   {
2406     this.table = new Array ();
2407   }
2408
2409   MIM.CandidateTable.prototype.get = function (pos)
2410   {
2411     for (var i = 0; i < this.table.length; i++)
2412       {
2413         var elt = this.table[i];
2414         if (elt.from < pos && pos <= elt.to)
2415           return elt.val;
2416       }
2417   }
2418
2419   MIM.CandidateTable.prototype.put = function (from, to, candidates)
2420   {
2421     for (var i = 0; i < this.table.length; i++)
2422       {
2423         var elt = this.table[i];
2424         if (elt.from < to && elt.to > from)
2425           {
2426             elt.from = from;
2427             elt.to = to;
2428             elt.val = candidates;
2429             return;
2430           }
2431       }
2432     this.table.push ({ from: from, to: to, val: candidates });
2433   }
2434
2435   MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
2436   {
2437     var diff = inserted - (to - from);
2438     if (diff == 0)
2439       return;
2440     for (var i = 0; i < this.table.length; i++)
2441       {
2442         var elt = this.table[i];
2443         if (elt.from >= to)
2444           {
2445             elt.from += diff;
2446             elt.to += diff;
2447           }
2448       }
2449   }
2450
2451   MIM.CandidateTable.prototype.clear = function ()
2452   {
2453     this.table.length = 0;
2454   }
2455
2456   function detach_candidates (ic)
2457   {
2458     ic.candidate_table.clear ();
2459     ic.candidates = null;
2460     ic.changed |= (MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos
2461                    | MIM.ChangedStatus.CandidateList
2462                    | MIM.ChangedStatus.CandidateIndex
2463                    | MIM.ChangedStatus.CandidateShow);
2464   }
2465
2466   function set_cursor (prefix, pos)
2467   {
2468     this.cursor_pos = pos;
2469     this.candidates = this.candidate_table.get (pos);
2470   }
2471
2472   function save_state ()
2473   {
2474     this.state_var_values = this.domain.SaveValues ();
2475     this.state_preedit = this.preedit;
2476     this.state_key_head = this.key_head;
2477     this.state_pos = this.cursor_pos;
2478   }
2479
2480   function restore_state ()
2481   {
2482     this.domain.RestoreValues (this.state_var_values);
2483     this.preedit = this.state_preedit;
2484     set_cursor.call (this, "restore", this.state_pos);
2485   }
2486
2487   function handle_key ()
2488   {
2489     Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
2490              + '" in ' + this.state.name + ':' + this.keymap.name
2491              + " key/state/commit-head/len:"
2492              + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
2493     var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
2494     var sub = out.map;
2495
2496     if (out.index > this.key_head)
2497       {
2498         this.key_head = out.index;
2499         Xex.Log (' with submap for ' + this.key_head + 'keys');
2500         restore_state.call (this);
2501         this.keymap = sub;
2502         if (sub.map_actions)
2503           {
2504             Xex.Log ('taking map actions:');
2505             if (! this.take_actions (sub.map_actions))
2506               return false;
2507           }
2508         else if (sub.submaps)
2509           {
2510             Xex.Log ('no map actions');
2511             for (var i = this.state_key_head; i < this.key_head; i++)
2512               {
2513                 Xex.Log ('inserting key:' + this.keys.val[i].key);
2514                 this.ins (this.keys.val[i].key, null);
2515               }
2516           }
2517         if (! sub.submaps)
2518           {
2519             Xex.Log ('terminal:');
2520             if (this.keymap.branch_actions != null)
2521               {
2522                 Xex.Log ('branch actions:');
2523                 if (! this.take_actions (this.keymap.branch_actions))
2524                   return false;
2525               }
2526             if (sub != this.state.keymap)
2527               this.shift (this.state);
2528           }
2529       }
2530     else
2531       {
2532         Xex.Log (' without submap');
2533         this.keymap = sub;
2534         var current_state = this.state;
2535         var map = this.keymap;
2536
2537         if (map.branch_actions)
2538           {
2539             Xex.Log ('branch actions');
2540             if (! this.take_actions (map.branch_actions))
2541               return false;
2542           }
2543
2544         if (map == this.keymap)
2545           {
2546             Xex.Log ('no state change');
2547             if (map == this.initial_state.keymap
2548                 && this.key_head < this.keys.val.length)
2549               {
2550                 Xex.Log ('unhandled');
2551                 return false;
2552               }
2553             if (this.keymap != current_state.keymap)
2554               this.shift (current_state);
2555             else if (this.keymap.actions == null)
2556               this.shift (this.initial_state);
2557           }
2558       }
2559     return true;
2560   }
2561
2562   proto = {
2563     reset: function ()
2564     {
2565       this.cursor_pos = 0;
2566       this.candidate_show = false;
2567       this.prev_state = null;
2568       this.title = this.initial_state.title;
2569       this.state_preedit = '';
2570       this.state_key_head = 0;
2571       this.state_var_values = {};
2572       this.state_pos = 0;
2573       this.key_head = 0;
2574       this.commit_key_head = 0;
2575       this.key_unhandled = false;
2576       this.unhandled_key = null;
2577       this.changed = MIM.ChangedStatus.None;
2578       this.error_message = '';
2579       this.title = this.initial_state.title;
2580       this.produced = '';
2581       this.preedit = '';
2582       this.preedit_saved = '';
2583       this.candidate_table.clear ();
2584       this.candidates = null;
2585       this.candidate_show = false;
2586       for (var elt in this.marker_positions)
2587         this.marker_positions[elt] = 0;
2588       this.shift (this.initial_state);
2589     },
2590
2591     catch_args: new Array (Xex.CatchTag._mimtag, null),
2592
2593     take_actions: function (actions)
2594     {
2595       var func_progn = this.domain.GetFunc ('progn');
2596       var func_catch = this.domain.GetFunc ('catch');
2597       this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
2598       var term = new Xex.Funcall (func_catch, null, this.catch_args);
2599       term = term.Eval (this.domain);
2600       return (! term.IsSymbol || term.val != '@mimtag');
2601     },
2602
2603     GetSurroundingChar: function (pos)
2604     {
2605       if (pos < 0)
2606         {
2607           pos += this.range[0];
2608           if (pos < 0)
2609             return 0;
2610         }
2611       else
2612         {
2613           pos += this.range[1];
2614           if (pos >= this.target.value.length)
2615             return 0;
2616         }
2617       return this.target.value.charCodeAt (pos);
2618     },
2619     
2620     DelSurroundText: function (pos)
2621     {
2622       var text;
2623       if (pos < 0)
2624         {
2625           pos += this.range[0];
2626           if (pos <= 0)
2627             {
2628               pos = 0; text = '';
2629             }
2630           else
2631             text = this.target.value.substring (0, pos);
2632           if (this.range[0] < this.target.value.length)
2633             text += this.target.value.substring (this.range[0]);
2634           this.target.value = text;
2635           this.range[1] -= this.range[0] - pos;
2636           this.range[0] = pos;
2637         }
2638       else
2639         {
2640           pos += this.range[1];
2641           text = this.target.value.substring (0, this.range[1]);
2642           if (pos >= this.target.value.length)
2643             pos = this.target.value.length;
2644           else
2645             text += this.target.value.substring (pos);
2646           this.target.value = text;
2647         }
2648     },
2649
2650     adjust_markers: function (from, to, inserted)
2651     {
2652       var diff = inserted - (to - from);
2653
2654       for (var name in this.marker_positions)
2655         {
2656           var pos = this.marker_positions[name];
2657           if (pos > from)
2658             {
2659               if (pos >= to)
2660                 this.marker_positions[name] += diff;
2661               else if (pos > from)
2662                 this.marker_positions[name] = from;
2663             }
2664         }
2665       if (this.cursor_pos >= to)
2666         set_cursor.call (this, 'adjust', this.cursor_pos + diff);
2667       else if (this.cursor_pos > from)
2668         set_cursor.call (this, 'adjust', from)
2669     },
2670
2671     preedit_replace: function (from, to, text, candidates)
2672     {
2673       this.preedit = (this.preedit.substring (0, from)
2674                       + text + this.preedit.substring (to));
2675       this.adjust_markers (from, to, text.length);
2676       this.candidate_table.adjust (from, to, text.length);
2677       if (candidates)
2678         this.candidate_table.put (from, from + text.length, candidates)
2679     },
2680
2681     ins: function (text, candidates)
2682     {
2683       this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
2684       this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2685     },
2686
2687     rep: function (old_text, new_text, candidates)
2688     {
2689       this.preedit_replace (this.cursor_pos - old_text.length,
2690                             this.cursor_pos, new_text, candidates);
2691       this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2692     },
2693
2694     del: function (pos)
2695     {
2696       var deleted = pos - this.cursor_pos;
2697       if (pos < this.cursor_pos)
2698         {
2699           if (pos < 0)
2700             {
2701               this.DelSurroundText (pos);
2702               deleted = - this.cursor_pos;
2703               pos = 0;
2704             }
2705           if (pos < this.cursor_pos)
2706             this.preedit_replace (pos, this.cursor_pos, '', null);
2707         }
2708       else
2709         {
2710           if (pos > this.preedit.length)
2711             {
2712               this.DelSurroundText (pos - this.preedit.length);
2713               deleted = this.preedit.length - this.cursor_pos;
2714               pos = this.preedit.length;
2715             }
2716           if (pos > this.cursor_pos)
2717             this.preedit_replace (this.cursor_pos, pos, '', null);
2718         }
2719       if (deleted != 0)
2720         this.changed = MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
2721       return deleted;
2722     },
2723
2724     show: function ()
2725     {
2726       this.candidate_show = true;
2727       this.changed |= MIM.ChangedStatus.CandidateShow;
2728     },
2729
2730     hide: function ()
2731     {
2732       this.candidate_show = false;
2733       this.changed |= MIM.ChangedStatus.CandidateShow;
2734     },
2735
2736     move: function (pos)
2737     {
2738       if (pos < 0)
2739         pos = 0;
2740       else if (pos > this.preedit.length)
2741         pos = this.preedit.length;
2742       if (pos != this.cursor_pos)
2743         {
2744           set_cursor.call (this, 'move', pos);
2745           this.changed |= MIM.ChangedStatus.Preedit;
2746         }
2747     },
2748
2749     pushback: function (n)
2750     {
2751       if (n instanceof MIM.KeySeq)
2752         {
2753           if (this.key_head > 0)
2754             this.key_head--;
2755           if (this.key_head < this.keys.val.length)
2756             this.keys.val.splice (this.key_head,
2757                                   this.keys.val.length - this.key_head);
2758           for (var i = 0; i < n.val.length; i++)
2759             this.keys.val.push (n.val[i]);
2760           return;
2761         }
2762       if (n > 0)
2763         {
2764           this.key_head -= n;
2765           if (this.key_head < 0)
2766             this.key_head = 0;
2767         }
2768       else if (n == 0)
2769         this.key_head = 0;
2770       else
2771       {
2772         this.key_head = - n;
2773         if (this.key_head > this.keys.val.length)
2774           this.key_head = this.keys.val.length;
2775       }
2776       Xex.Log ('0: key head = ' + this.key_head); 
2777     },
2778
2779     pop: function ()
2780     {
2781       if (this.key_head < this.keys.val.length)
2782         this.keys.val.splice (this.key_head, 1);
2783     },
2784
2785     commit: function ()
2786     {
2787       if (this.preedit.length > 0)
2788       {
2789         this.candidate_table.clear ();
2790         this.produced += this.preedit;
2791         this.preedit_replace.call (this, 0, this.preedit.length, '', null);
2792         this.preedit_saved = '';
2793         this.state_pos = 0;
2794         this.commit_key_head = this.key_head;
2795       }
2796     },
2797
2798     shift: function (state)
2799     {
2800       if (state == null)
2801         {
2802           if (this.prev_state == null)
2803             return;
2804           state = this.prev_state;
2805         }
2806
2807       if (state == this.initial_state)
2808         {
2809           if (this.state)
2810             {
2811               this.commit ();
2812               this.keys.val.splice (0, this.key_head);
2813               this.key_head = this.state_key_head = this.commit_key_head = 0;
2814               this.prev_state = null;
2815             }
2816         }
2817       else
2818         {
2819           if (state != this.state)
2820             this.prev_state = this.state;
2821         }
2822       if (state != this.state && state.enter_actions)
2823         this.take_actions (state.enter_actions);
2824       if (! this.state || this.state.title != state.title)
2825         this.changed |= MIM.ChangedStatus.StateTitle;
2826       this.state = state;
2827       this.keymap = state.keymap;
2828       save_state.call (this);
2829     },
2830
2831     Filter: function (key)
2832     {
2833       if (! this.active)
2834         {
2835           Xex.Log ("active = false");
2836           this.key_unhandled = true;
2837           this.unhandled_key = key;
2838           return false;
2839         }
2840       if (key.key == '_reload')
2841         return true;
2842       this.changed = MIM.ChangedStatus.None;
2843       this.produced = '';
2844       this.key_unhandled = false;
2845       this.keys.val.push (key);
2846       var count = 0;
2847       Xex.Log ("keyhead=" + this.key_head + " len="+this.keys.val.length);
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.update = function (target, ic)
3120 {
3121   var text = target.value;
3122   target.value = (text.substring (0, ic.range[0])
3123                   + ic.produced
3124                   + ic.preedit
3125                   + text.substring (ic.range[1]));
3126   ic.range[0] += ic.produced.length;
3127   ic.range[1] = ic.range[0] + ic.preedit.length;
3128   MIM.set_caret (target, ic);
3129 };
3130
3131 (function () {
3132   var style_props = {
3133     width: 'width',
3134     height: 'height',
3135     padingLeft: 'padding-left',
3136     paddingRight: 'padding-right',
3137     paddingTop: 'padding-top',
3138     paddintBottom: 'padding-bottom', 
3139     borderLeftStyle: 'border-left-style',
3140     borderRightStyle: 'border-right-style',
3141     borderTopStyle: 'border-top-style',
3142     borderBottomStyle: 'border-bottom-style',
3143     borderLeftWidth: 'border-left-width',
3144     borderRightWidth: 'border-right-width',
3145     borderTopWidth: 'border-top-width',
3146     borderBottomWidth: 'border-bottom-width',
3147     fontFamily: 'font-family',
3148     fontSize: 'font-size',
3149     lineHeight: 'line-height',
3150     letterSpacing: 'letter-spacing',
3151     wordSpacing: 'word-spacing' };
3152
3153   function copy_style (from, to)
3154   {
3155     var from_style = getComputedStyle(from,'');
3156         
3157     for(var name in style_props)
3158       to.style[name] = from_style.getPropertyValue (style_props[name]);
3159         
3160     to.style.left = from.offsetLeft + 'px'; 
3161     to.style.top = from.offsetTop + 'px';
3162     to.style.width = from.offsetWidth;
3163     to.style.height = from.offsetHeight;
3164   }
3165
3166   MIM.show = function (target, ic)
3167   {
3168     if (! ic.div_node)
3169       {
3170         ic.target_top = 0;
3171         ic.target_left = 0;
3172         for (var elm = ic.target.offsetParent; elm; elm = elm.offsetParent)
3173           {
3174             ic.target_top += elm.offsetTop;
3175             ic.target_left += elm.offsetLeft;
3176           }
3177         ic.div_node = document.createElement ('div');
3178         ic.div_node.style.visibility="hidden";
3179         ic.div_node.style.position = "absolute";
3180         document.getElementsByTagName ('body')[0].appendChild (ic.div_node);
3181         ic.div_node_first = document.createElement ('span');
3182         ic.div_node_last = document.createElement('span');
3183         ic.div_node_last.innerHTML = '.';
3184         ic.can_node = document.createElement ('div');
3185         ic.can_node.appendChild (document.createTextNode ('1. abc'));
3186         ic.can_node.appendChild (document.createTextNode ('2. def'));
3187         ic.can_node.style.position = 'absolute';
3188         ic.can_node.style.display = 'none';
3189         document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
3190       }
3191
3192     copy_style (target, ic.div_node);
3193     ic.div_node.innerHTML = '';
3194     ic.div_node_first.innerHTML = target.value.substr (0, ic.range[0]);
3195     ic.div_node.appendChild (ic.div_node_first);
3196     ic.div_node.appendChild (ic.div_node_last);
3197     Xex.Log (ic.div_node.firstChild.innerHTML);
3198     var x = ic.target_left + ic.div_node.lastChild.offsetLeft;
3199     var y = ic.target_top + ic.div_node.lastChild.offsetTop - target.scrollTop;
3200     ic.can_node.style.left = x + 'px';
3201     ic.can_node.style.top = y + 'px';
3202     ic.can_node.style.display = 'block';
3203     Xex.Log ("x:" + x + "px y:" + y + "px");
3204   }
3205 }) ();
3206
3207 MIM.focus_in = function (event)
3208 {
3209   var target = event.target;
3210   var ic = target.mim_ic;
3211   if (ic.wait_update == true)
3212     {
3213       Xex.Log ("Focus in " + target.tagName + ' IGNORED');
3214       event.preventDefault ();
3215       return false;
3216     }
3217   Xex.Log ("Focus in " + target.tagName);
3218   ic.Filter (MIM.Key.FocusIn);
3219   MIM.update (target, ic);
3220 }
3221
3222 MIM.focus_out = function (event)
3223 {
3224   var target = event.target;
3225   var ic = target.mim_ic;
3226   function reset_update () { ic.wait_update = false; };
3227   if (ic.wait_update == true)
3228     {
3229       Xex.Log ("Focus out " + target.tagName + ' IGNORED');
3230       event.preventDefault ();
3231       return false;
3232     }
3233   Xex.Log ("Focus out " + target.tagName);
3234   ic.Filter (MIM.Key.FocusOut);
3235   ic.wait_update = true;
3236   MIM.update (target, ic, true);
3237   setTimeout (reset_update, 1000);
3238 };
3239
3240 MIM.keydown = function (event)
3241 {
3242   var target = event.target;
3243   if (target.id == 'log')
3244     return;
3245   if (! (target.type == "text" || target.type == "textarea"))
3246     return;
3247
3248   var ic = target.mim_ic;
3249   if (! ic || ic.im != MIM.current)
3250     {
3251       target.mim_ic = null;
3252       Xex.Log ('creating IC');
3253       ic = new MIM.IC (MIM.current, target);
3254       if (ic.im.load_status != MIM.LoadStatus.Loaded)
3255         return;
3256       target.mim_ic = ic;
3257       MIM.add_event_listener (target, 'focus', MIM.focus_in);
3258       MIM.add_event_listener (target, 'blur', MIM.focus_out);
3259       MIM.get_range (target, ic)
3260     }
3261   else
3262     {
3263       if (! MIM.get_range (target, ic))
3264         ic.reset ();
3265     }
3266   MIM.show (target, ic);
3267   MIM.debug_print (event, ic);
3268   ic.key = MIM.decode_key_event (event);
3269   if (ic.key)
3270     {
3271       Xex.Log ("filtering " + ic.key);
3272       try {
3273         var result = ic.Filter (ic.key);
3274       } catch (e) {
3275         Xex.Log ('Error' + e);
3276         throw (e);
3277       }
3278       MIM.update (target, ic);
3279       if (! ic.key_unhandled)
3280         event.preventDefault ();
3281     }
3282 };
3283
3284 MIM.keypress = function (event)
3285 {
3286   var target = event.target;
3287   if (target.id == 'log')
3288     return;
3289   if (! (target.type == "text" || target.type == "textarea"))
3290     return;
3291
3292   var ic = target.mim_ic;
3293   var i;
3294
3295   try {
3296     if (ic.im.load_status != MIM.LoadStatus.Loaded)
3297       return;
3298     if (! ic.key)
3299       ic.key = MIM.decode_key_event (event);
3300     if (! ic.key)
3301       {
3302         ic.reset ();
3303         return;
3304       }
3305     
3306     Xex.Log ("filtering " + ic.key);
3307     try {
3308       var result = ic.Filter (ic.key);
3309       Xex.Log ("result = " + result);
3310     } catch (e) {
3311       Xex.Log ('Error:' + e);
3312       throw (e);
3313     }
3314     MIM.update (target, ic);
3315     if (! ic.key_unhandled)
3316       event.preventDefault ();
3317   } catch (e) {
3318     Xex.Log ("error:" + e);
3319     event.preventDefault ();
3320   } finally {
3321     MIM.debug_print (event, ic);
3322   }
3323
3324   return;
3325 };
3326
3327 (function () {
3328   var lang_category = {
3329     European: {
3330       cs: { name: 'Czech' },
3331       da: { name: 'Danish' },
3332       el: { name: 'Greek' },
3333       en: { name: 'English' },
3334       eo: { name: 'Esperanto' },
3335       fr: { name: 'French' },
3336       grc: { name: 'ClassicGreek' },
3337       hr: { name: 'Croatian' },
3338       hy: { name: 'Armenian' },
3339       ka: { name: 'Georgian' },
3340       kk: { name: 'Kazakh' },
3341       ru: { name: 'Russian' },
3342       sk: { name: 'Slovak' },
3343       sr: { name: 'Serbian' },
3344       sv: { name: 'Swedish' },
3345       yi: { name: 'Yiddish' } },
3346     MiddleEast: {
3347       ar: { name: 'Arabic' },
3348       dv: { name: 'Divehi' },
3349       fa: { name: 'Persian' },
3350       he: { name: 'Hebrew' },
3351       kk: { name: 'Kazakh' },
3352       ps: { name: 'Pushto' },
3353       ug: { name: 'Uighur' },
3354       yi: { name: 'Yiddish' } },
3355     SouthAsia: {
3356       as: { name: 'Assamese' },
3357       bn: { name: 'Bengali' },
3358       bo: { name: 'Tibetan' },
3359       gu: { name: 'Gujarati' },
3360       hi: { name: 'Hindi' },
3361       kn: { name: 'Kannada' },
3362       ks: { name: 'Kashmiri' },
3363       ml: { name: 'Malayalam' },
3364       mr: { name: 'Marathi' },
3365       ne: { name: 'Nepali' },
3366       or: { name: 'Oriya' },
3367       pa: { name: 'Panjabi' },
3368       sa: { name: 'Sanskirit' },
3369       sd: { name: 'Sindhi' },
3370       si: { name: 'Sinhalese' },
3371       ta: { name: 'Tamil' },
3372       te: { name: 'Telugu' },
3373       ur: { name: 'Urdu' } },
3374     SouthEastAsia: {
3375       cmc: { name: 'Cham' },
3376       km: { name: 'Khmer'},
3377       lo: { name: 'Lao' },
3378       my: { name: 'Burmese' },
3379       tai: { name: 'Tai Viet' },
3380       th: { name: 'Thai' },
3381       vi: { name: 'Vietanamese' } },
3382     EastAsia: {
3383       ii: { name: 'Yii' },
3384       ja: { name: 'Japanese' },
3385       ko: { name: 'Korean' },
3386       zh: { name: 'Chinese' } },
3387     Other: {
3388       am: { name:  'Amharic' },
3389       ath: { name: 'Carrier' },
3390       bla: { name: 'Blackfoot' },
3391       cr: { name: 'Cree' },
3392       eo: { name: 'Esperanto' },
3393       iu: { name: 'Inuktitut' },
3394       nsk: { name: 'Naskapi' },
3395       oj: { name: 'Ojibwe' },
3396       t: { name: 'Generic' } }
3397   };
3398
3399   function categorize_im ()
3400   {
3401     var cat, lang, list, name;
3402     for (lang in MIM.imlist)
3403       {
3404         list = null;
3405         for (cat in lang_category)
3406           if (lang_category[cat][lang])
3407             {
3408               list = lang_category[cat][lang].list;
3409               if (! list)
3410                 list = lang_category[cat][lang].list = {};
3411               for (name in MIM.imlist[lang])
3412                 list[name] = MIM.imlist[lang][name];
3413             }
3414         if (! list)
3415           for (name in MIM.imlist[lang])
3416             Xex.Log ('no category ' + lang + '-' + name);
3417       }
3418   }
3419
3420   var destroy_timer;
3421   var last_target;
3422
3423   function destroy ()
3424   {
3425     clearTimeout (destroy_timer);
3426     destroy_timer = null;
3427     var target = document.getElementById ('mim-menu');
3428     if (target)
3429       {
3430         for (; last_target && last_target.menu_level;
3431              last_target = last_target.parentLi)
3432           last_target.style.backgroundColor = 'white';
3433         var nodes = target.getElementsByTagName ('ul');
3434         for (var i = 0; i < nodes.length; i++)
3435           nodes[i].style.visibility = 'hidden';
3436         document.getElementsByTagName ('body')[0].removeChild (target);
3437       }
3438   }    
3439
3440   function destroy_menu () {
3441     if (! destroy_timer)
3442       destroy_timer = setTimeout (destroy, 1000);
3443   }
3444
3445   function show_submenu (event)
3446   {
3447     if (destroy_timer)
3448       {
3449         clearTimeout (destroy_timer);
3450         destroy_timer = null;
3451       }
3452     var target = event.target;
3453     if (! target.menu_level)
3454       return;
3455     if (last_target && target.parentLi != last_target)
3456       {
3457         last_target.style.backgroundColor = 'white';
3458         if (target.menu_level < last_target.menu_level)
3459           {
3460             last_target = last_target.parentLi;
3461             last_target.style.backgroundColor = 'white';
3462           }
3463         var uls = last_target.getElementsByTagName ('ul');
3464         for (var i = 0; i < uls.length; i++)
3465           uls[i].style.visibility = 'hidden';
3466       }
3467     last_target = target;
3468     target.style.backgroundColor = 'yellow';
3469     if (target.menu_level < 3)
3470       {
3471         target.lastChild.style.visibility = 'visible';
3472         target.lastChild.style.left = target.clientWidth + 'px';
3473       }
3474     event.preventDefault ();    
3475   }
3476
3477   function select_im (event)
3478   {
3479     var target = event.target;
3480     if (target.im)
3481       {
3482         MIM.current = target.im;
3483         destroy ();
3484       }
3485     event.preventDefault ();
3486   }
3487
3488   function create_ul (visibility)
3489   {
3490     var ul = document.createElement ('ul');
3491     ul.style.position = 'absolute';
3492     ul.style.margin = '0px';
3493     ul.style.padding = '0px';
3494     ul.style.border = '1px solid gray';
3495     ul.style.borderBottom = 'none';
3496     ul.style.top = '-1px';
3497     ul.style.backgroundColor = 'white';
3498     ul.style.visibility = visibility;
3499     return ul;
3500   }
3501
3502   function create_li (level, text)
3503   {
3504     var li = document.createElement ('li');
3505     li.style.position = 'relative';
3506     li.style.margin = '0px';
3507     li.style.padding = '1px';
3508     li.style.borderBottom = '1px solid gray';
3509     li.style.top = '0px';
3510     li.style.listStyle = 'none';
3511     li.menu_level = level;
3512     var nobr = document.createElement ('nobr');
3513     nobr.innerHTML = text;
3514     li.appendChild (nobr);
3515     return li;
3516   }
3517
3518   var menu;
3519
3520   function create_menu (event)
3521   {
3522     var target = event.target;
3523
3524     if (! ((target.type == "text" || target.type == "textarea")
3525            && event.which == 1 && event.ctrlKey))
3526       return;
3527     if (! menu)
3528       {
3529         categorize_im ();
3530         menu = create_ul ('visible');
3531         menu.style.fontFamily = 'sans-serif';
3532         menu.style.fontWeight = 'bold';
3533         menu.id = 'mim-menu';
3534         menu.onclick = select_im;
3535         menu.onmouseover = show_submenu;
3536         menu.onmouseout = destroy_menu;
3537         for (var catname in lang_category)
3538           {
3539             var cat = lang_category[catname];
3540             var li = create_li (1, catname);
3541             var sub = create_ul ('hidden');
3542             for (var langname in cat)
3543               {
3544                 var lang = cat[langname];
3545                 if (! lang.list)
3546                   continue;
3547                 var sub_li = create_li (2, lang.name);
3548                 sub_li.parentLi = li;
3549                 var subsub = create_ul ('hidden');
3550                 for (var name in lang.list)
3551                   {
3552                     var im = lang.list[name];
3553                     var subsub_li = create_li (3, im.name);
3554                     subsub_li.parentLi = sub_li;
3555                     subsub_li.im = im;
3556                     subsub.appendChild (subsub_li);
3557                   }
3558                 sub_li.appendChild (subsub);
3559                 sub.appendChild (sub_li);
3560               }
3561             li.appendChild (sub);
3562             menu.appendChild (li);
3563           }
3564         lang_category = null;
3565       }
3566     menu.style.left = (event.clientX - 10) + "px";
3567     menu.style.top = (event.clientY - 10) + "px";
3568     document.getElementsByTagName ('body')[0].appendChild (menu);
3569   };
3570
3571   MIM.init = function ()
3572   {
3573     MIM.add_event_listener (window, 'keydown', MIM.keydown);
3574     MIM.add_event_listener (window, 'keypress', MIM.keypress);
3575     MIM.add_event_listener (window, 'mousedown', create_menu);
3576     if (window.location == 'http://localhost/mim/index.html')
3577       MIM.server = 'http://localhost/mim';
3578     MIM.current = MIM.imlist['zh']['tonepy'];
3579   };
3580 }) ();
3581
3582 MIM.test = function ()
3583 {
3584   var im = MIM.imlist['t']['latn-post'];
3585   var ic = new MIM.IC (im, null);
3586
3587   ic.Filter (new MIM.Key ('a'));
3588   ic.Filter (new MIM.Key ("'"));
3589
3590   if (true)
3591     document.getElementById ('text').value = ic.produced + ic.preedit;
3592   else {
3593     try {
3594       document.getElementById ('text').value
3595         = Xex.Term.Parse (domain, body).Eval (domain).toString ();
3596     } catch (e) {
3597       if (e instanceof Xex.ErrTerm)
3598         alert (e);
3599       throw e;
3600     }
3601   }
3602 }
3603
3604
3605 MIM.init_debug = function ()
3606 {
3607   MIM.debug = true;
3608   Xex.LogNode = document.getElementById ('log');
3609   Xex.Log (null);
3610   MIM.init ();
3611 };