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