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