26c978f520b4c24feb7bdb3f3699cfc6fb2bbcca
[m17n/m17n-lib-js.git] / mim.js
1 var MIM = {
2   // URL of the input method server.
3   server: "http://www.m17n.org/common/mim-js",
4   // Boolean flag to tell if MIM is active or not.
5   enabled: true,
6   // Boolean flag to tell if MIM is running in debug mode or not.
7   debug: false,
8   // List of main input methods.
9   imlist: {},
10   // List of extra input methods;
11   imextra: {},
12   // Global input method data
13   im_global: null,
14   // Currently selected input method.
15   current: false,
16
17   // enum
18   LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
19   ChangedStatus: {
20     None:       0x00,
21     StateTitle: 0x01,
22     PreeditText:0x02,
23     CursorPos:  0x04,
24     CandidateList:0x08,
25     CandidateIndex:0x10,
26     CandidateShow:0x20,
27     Preedit:    0x06,           // PreeditText | CursorPos
28     Candidate:  0x38 // CandidateList | CandidateIndex | CandidateShow
29   },
30   KeyModifier: {
31     SL: 0x00400000,
32     SR: 0x00800000,
33     S:  0x00C00000,
34     CL: 0x01000000,
35     CR: 0x02000000,
36     C:  0x03000000,
37     AL: 0x04000000,
38     AR: 0x08000000,
39     A:  0x0C000000,
40     ML: 0x04000000,
41     MR: 0x08000000,
42     M:  0x0C000000,
43     G:  0x10000000,
44     s:  0x20000000,
45     H:  0x40000000,
46     High:       0x70000000,
47     All:        0x7FC00000
48   },
49   Error: {
50     ParseError: "parse-error"
51   }
52 };
53   
54 if (window.location == 'http://localhost/mim/index.html')
55   MIM.server = 'http://localhost/mim';
56
57 (function () {
58   var keysyms = new Array ();
59   keysyms["bs"] = "backspace";
60   keysyms["lf"] = "linefeed";
61   keysyms["cr"] = keysyms["enter"] = "return";
62   keysyms["esc"] = "escape";
63   keysyms["spc"] = "space";
64   keysyms["del"] = "delete";
65
66   function decode_keysym (str) {
67     if (str.length == 1)
68       return str;
69     var parts = str.split ("-");
70     var len = parts.length, i;
71     var has_modifier = len > 1;
72
73     for (i = 0; i < len - 1; i++)
74       if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
75         return false;
76     var key = parts[len - 1];
77     if (key.length > 1)
78       {
79         key = keysyms[key.toLowerCase ()];
80         if (key)
81           {
82             if (len > 1)
83               {
84                 str = parts[0];
85                 for (i = 1; i < len - 1; i++)
86                   str += '-' + parts[i];
87                 str += '-' + key;
88               }
89             else
90               str = key;
91           }
92       }
93     if (has_modifier)
94       {
95         parts = new Array ();
96         parts.push (str);
97         return parts;
98       }
99     return str;
100   }
101
102   MIM.Key = function (val)
103   {
104     this.key;
105     if (val instanceof Xex.Term)
106       this.key = val.val;
107     else if (typeof val == 'string' || val instanceof String)
108       {
109         this.key = decode_keysym (val);
110         if (! this.key)
111           throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
112         if (this.key instanceof Array)
113           {
114             this.key = this.key[0];
115             this.has_modifier = true;
116           }
117       }
118     else if (typeof val == 'number' || val instanceof Number)
119       this.key = String.fromCharCode (val);
120     else
121       throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
122   }
123
124   MIM.Key.prototype.toString = function () { return this.key; };
125
126   MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
127   MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
128   MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
129 }) ();
130
131 (function () {
132   MIM.KeySeq = function (seq)
133   {
134     this.val = new Array ();
135
136     if (seq)
137       {
138         if (seq.IsList)
139           {
140             var len = seq.val.length;
141             for (var i = 0; i < len; i++)
142               {
143                 var v = seq.val[i], key;
144                 if (v.type == 'symbol' || v.type == 'string')
145                   key = new MIM.Key (v);
146                 else if (v.type == 'integer')
147                   key = new MIM.Key (v.val);
148                 else
149                   throw new Xex.ErrTerm (MIM.Error.ParseError,
150                                          "Invalid key: " + v);
151                 this.val.push (key);
152                 if (key.has_modifier)
153                   this.has_modifier = true;
154               }
155           }
156         else if (seq.IsStr)
157           {
158             var len = seq.val.length;
159             for (var i = 0; i < len; i++)
160               this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
161           }
162         else
163           throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
164       }
165   }
166
167   var proto = new Xex.Term ('keyseq');
168   proto.Clone = function () { return this; }
169   proto.Parser = function (domain, node)
170   {
171     var seq = new Array ();
172     for (node = node.firstChild; node; node = node.nextSibling)
173       if (node.nodeType == 1)
174         {
175           var term = Xex.Term.Parse (domain, node);
176           return new MIM.KeySeq (term);
177         }
178     throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
179   }
180   proto.toString = function ()
181   {
182     var len = this.val.length;
183     if (len == 0)
184       return '<keyseq/>';
185     var first = true;
186     var str = '<keyseq>';
187     for (var i = 0; i < len; i++)
188       {
189         if (first)
190           first = false;
191         else if (this.has_modifier)
192           str += ' ';
193         str += this.val[i].toString ();
194       }
195     return str + '</keyseq>';
196   }
197
198   MIM.KeySeq.prototype = proto;
199 }) ();
200
201 (function () {
202   MIM.Marker = function () { }
203   MIM.Marker.prototype = new Xex.Term ('marker');
204   MIM.Marker.prototype.CharAt = function (ic)
205   {
206     var p = this.Position (ic);
207     if (p < 0 || p >= ic.preedit.length)
208       return 0;
209     return ic.preedit.charCodeAt (p);
210   }
211
212   MIM.FloatingMarker = function (name) { this.val = name; };
213   var proto = new MIM.Marker ();
214   MIM.FloatingMarker.prototype = proto;
215   proto.Position = function (ic) { return ic.marker_positions[this.val]; };
216   proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
217
218   MIM.PredefinedMarker = function (name) { this.val = name; }
219   MIM.PredefinedMarker.prototype = new MIM.Marker ();
220   MIM.PredefinedMarker.prototype.Position = function (ic)
221   {
222     if (typeof this.pos == 'number')
223       return this.pos;
224     return this.pos (ic);
225   }
226
227   var predefined = { }
228
229   function def_predefined (name, position)
230   {
231     predefined[name] = new MIM.PredefinedMarker (name);
232     predefined[name].pos = position;
233   }
234
235   def_predefined ('@<', 0);
236   def_predefined ('@>', function (ic) { return ic.preedit.length; });
237   def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
238   def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
239   def_predefined ('@[', function (ic) {
240     if (ic.cursor_pos > 0)
241       {
242         var pos = ic.cursor_pos;
243         return ic.preedit.FindProp ('candidates', pos - 1).from;
244       }
245     return 0;
246   });
247   def_predefined ('@]', function (ic) {
248     if (ic.cursor_pos < ic.preedit.length - 1)
249       {
250         var pos = ic.cursor_pos;
251         return ic.preedit.FindProp ('candidates', pos).to;
252       }
253     return ic.preedit.length;
254   });
255   for (var i = 0; i < 10; i++)
256     def_predefined ("@" + i, i);
257   predefined['@first'] = predefined['@<'];
258   predefined['@last'] = predefined['@>'];
259   predefined['@previous'] = predefined['@-'];
260   predefined['@next'] = predefined['@+'];
261   predefined['@previous-candidate-change'] = predefined['@['];
262   predefined['@next-candidate-change'] = predefined['@]'];
263
264   MIM.SurroundMarker = function (name)
265   {
266     this.val = name;
267     this.distance = parseInt (name.slice (1));
268     if (isNaN (this.distance))
269       throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
270   }
271   MIM.SurroundMarker.prototype = new MIM.Marker ();
272   MIM.SurroundMarker.prototype.Position = function (ic)
273   {
274     return ic.cursor_pos + this.distance;
275   }
276   MIM.SurroundMarker.prototype.CharAt = function (ic)
277   {
278     if (this.val == '@-0')
279       return -1;
280     var p = this.Position (ic);
281     if (p < 0)
282       return ic.GetSurroundingChar (p);
283     else if (p >= ic.preedit.length)
284       return ic.GetSurroundingChar (p - ic.preedit.length);
285     return ic.preedit.charCodeAt (p);
286   }
287
288   MIM.Marker.prototype.Parser = function (domain, node)
289   {
290     var name = node.firstChild.nodeValue;
291     if (name.charAt (0) == '@')
292       {
293         var n = predefined[name];
294         if (n)
295           return n;
296         if (name.charAt (1) == '-' || name.charAt (1) == '+')
297           return new MIM.SurroundMarker (name);
298         throw new Xex.ErrTerm (MIM.Error.ParseError,
299                                "Invalid marker: " + name);
300       }
301     return new MIM.FloatingMarker (name);;
302   }
303 }) ();
304
305 MIM.Selector = function (name)
306 {
307   this.val = name;
308 }
309 MIM.Selector.prototype = new Xex.Term ('selector');
310
311 (function () {
312   var selectors = {};
313   selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
314   selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
315   selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
316   selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
317   selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
318   selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
319   selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
320
321   MIM.Selector.prototype.Parser = function (domain, node)
322   {
323     var name = node.firstChild.nodeValue;
324     var s = selectors[name];
325     if (! s)
326       throw new Xex.ErrTerm (MIM.Error.ParseError,
327                              "Invalid selector: " + name);
328     return s;
329   }
330 }) ();
331
332 MIM.Rule = function (keyseq, actions)
333 {
334   this.keyseq = keyseq;
335   if (actions)
336     this.actions = actions;
337 }
338 MIM.Rule.prototype = new Xex.Term ('rule');
339 MIM.Rule.prototype.Parser = function (domain, node)
340 {
341   var n;
342   for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
343   if (! n)
344     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
345   var keyseq = Xex.Term.Parse (domain, n);
346   if (keyseq.type != 'keyseq')
347     throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
348   var actions = null;
349   n = n.nextElement ();
350   if (n)
351     actions = Xex.Term.Parse (domain, n, null);
352   return new MIM.Rule (keyseq, actions);
353 }
354 MIM.Rule.prototype.toString = function ()
355 {
356   return '<rule/>';
357 }
358
359 MIM.Map = function (name)
360 {
361   this.name = name;
362   this.rules = new Array ();
363 };
364
365 (function () {
366   var proto = new Xex.Term ('map');
367
368   proto.Parser = function (domain, node)
369   {
370     var name = node.attributes['mname'].nodeValue;
371     if (! name)
372       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
373     var map = new MIM.Map (name);
374     for (var n = node.firstChild; n; n = n.nextSibling)
375       if (n.nodeType == 1)
376         map.rules.push (Xex.Term.Parse (domain, n));
377     return map;
378   }
379
380   proto.toString = function ()
381   {
382     var str = '<map mname="' + this.name + '">';
383     var len = this.rules.length;
384     for (i = 0; i < len; i++)
385       str += this.rules[i];
386     return str + '</map>';
387   }
388
389   MIM.Map.prototype = proto;
390 }) ();
391
392 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
393
394 MIM.Action = function (domain, terms)
395 {
396   var args = new Array ();
397   args.push (Xex.CatchTag_.mimtag);
398   for (var i = 0; i < terms.length; i++)
399     args.push (terms[i]);
400   this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
401 }
402
403 MIM.Action.prototype.Run = function (domain)
404 {
405   var result = this.action.Eval (domain);
406   if (result.type == 'error')
407     {
408       domain.context.Error = result.toString ();
409       return false;
410     }
411   return (result != Xex.CatchTag._mimtag);
412 }
413
414 MIM.Keymap = function ()
415 {
416   this.name = 'TOP';
417   this.submaps = null;
418 };
419
420 (function () {
421   var proto = {};
422
423   function add_rule (keymap, rule, branch_actions)
424   {
425     var keyseq = rule.keyseq;
426     var len = keyseq.val.length;
427     var name = '';
428
429     for (var i = 0; i < len; i++)
430       {
431         var key = keyseq.val[i];
432         var sub = false;
433
434         name += key.key;
435         if (! keymap.submaps)
436           keymap.submaps = {};
437         else
438           sub = keymap.submaps[key.key];
439         if (! sub)
440           keymap.submaps[key.key] = sub = new MIM.Keymap ();
441         keymap = sub;
442         keymap.name = name;
443       }
444     keymap.map_actions = rule.actions;
445     if (branch_actions)
446       keymap.branch_actions = branch_actions;
447   }
448
449   proto.Add = function (map, branch_actions)
450   {
451     var rules = map.rules;
452     var len = rules.length;
453
454     for (var i = 0; i < len; i++)
455       add_rule (this, rules[i], branch_actions);
456   }
457   proto.Lookup = function (keys, index)
458   {
459     var sub;
460
461     if (index < keys.val.length && this.submaps
462         && ! keys.val[index])
463       {
464         Xex.Log ('invalid key at ' + index);
465         throw 'invalid key';
466       }
467
468     if (index < keys.val.length && this.submaps
469         && (sub = this.submaps[keys.val[index].key]))
470       {
471         index++;
472         return sub.Lookup (keys, index);
473       }
474     return { map: this, index: index };
475   }
476
477   MIM.Keymap.prototype = proto;
478 }) ();
479
480 MIM.State = function (name)
481 {
482   this.name = name;
483   this.keymap = new MIM.Keymap ();
484 };
485
486 (function () {
487   var proto = new Xex.Term ('state');
488
489   proto.Parser = function (domain, node)
490   {
491     var map_list = domain.map_list;
492     var name = node.attributes['sname'].nodeValue;
493     if (! name)
494       throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
495     var state = new MIM.State (name);
496     for (node = node.firstElement (); node; node = node.nextElement ())
497       {
498         if (node.nodeName == 'title')
499           state.title = node.firstChild.nodeValue;
500         else
501           {
502             var n = node.firstElement ();
503             var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
504             if (node.nodeName == 'branch')
505               state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
506                                 branch_actions);
507             else if (node.nodeName == 'state-hook')
508               state.enter_actions = branch_actions;
509             else if (node.nodeName == 'catch-all-branch')
510               state.fallback_actions = branch_actions;
511           }
512       }
513     return state;
514   }
515
516   proto.toString = function ()
517   {
518     return '<state sname="' + this.name + '">' + this.keymap + '</state>';
519   }
520
521   MIM.State.prototype = proto;
522 }) ();
523
524 (function () {
525   function Block (index, term)
526   {
527     this.Index = index;
528     if (term.IsStr)
529       this.Data = term.val;
530     else if (term.IsList)
531       {
532         this.Data = new Array ();
533         for (var i = 0; i < term.val.length; i++)
534           this.Data.push (term.val[i].val);
535       }
536   }
537
538   Block.prototype.Count = function () { return this.Data.length; }
539   Block.prototype.get = function (i)
540   {
541     return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
542   }
543
544   MIM.Candidates = function (ic, candidates, column)
545   {
546     this.ic = ic;
547     this.column = column;
548     this.row = 0;
549     this.index = 0;
550     this.total = 0;
551     this.blocks = new Array ();
552
553     for (var i = 0; i < candidates.length; i++)
554       {
555         var block = new Block (this.total, candidates[i]);
556         this.blocks.push (block);
557         this.total += block.Count ();
558       }
559   }
560
561   function get_col ()
562   {
563     return (this.column > 0 ? this.index % this.column
564             : this.index - this.blocks[this.row].Index);
565   }
566
567   function prev_group ()
568   {
569     var col = get_col.call (this);
570     var nitems;
571     if (this.column > 0)
572       {
573         this.index -= this.column;
574         if (this.index >= 0)
575           nitems = this.column;
576         else
577           {
578             var lastcol = (this.total - 1) % this.column;
579             this.index = (col < lastcol ? this.total - lastcol + col
580                           : this.total - 1);
581             this.row = this.blocks.length - 1;
582             nitems = lastcol + 1;
583           }
584         while (this.blocks[this.row].Index > this.index)
585           this.row--;
586       }
587     else
588       {
589         this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
590         nitems = this.blocks[this.row].Count ();
591         this.index = (this.blocks[this.row].Index
592                       + (col < nitems ? col : nitems - 1));
593       }
594     return nitems;
595   }
596
597   function next_group ()
598   {
599     var col = get_col.call (this);
600     var nitems;
601     if (this.column > 0)
602       {
603         this.index += this.column - col;
604         if (this.index < this.total)
605           {
606             if (this.index + col >= this.total)
607               {
608                 nitems = this.total - this.index;
609                 this.index = this.total - 1;
610               }
611             else
612               {
613                 nitems = this.column;
614                 this.index += col;
615               }
616           }
617         else
618           {
619             this.index = col;
620             this.row = 0;
621           }
622         while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
623                <= this.index)
624           this.row++;
625       }
626     else
627       {
628         this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
629         nitems = this.blocks[this.row].Count ();
630         this.index = (this.blocks[this.row].Index
631                       + (col < nitems ? col : nitems - 1));
632       }
633     return nitems;
634   }
635
636   function prev ()
637   {
638     if (this.index == 0)
639       {
640         this.index = this.total - 1;
641         this.row = this.blocks.length - 1;
642       }
643     else
644       {
645         this.index--;
646         if (this.blocks[this.row].Index > this.index)
647           this.row--;
648       }
649     }
650
651   function next ()
652   {
653     this.index++;
654     if (this.index == this.total)
655       {
656         this.index = 0;
657         this.row = 0;
658       }
659     else
660       {
661         var b = this.blocks[this.row];
662         if (this.index == b.Index + b.Count ())
663           this.row++;
664       }
665   }
666
667   function first ()
668   {
669     this.index -= get_col.call (this);
670     while (this.blocks[this.row].Index > this.index)
671       this.row--;
672   }
673
674   function last ()
675   {
676     var b = this.blocks[this.row];
677     if (this.column > 0)
678       {
679         if (this.index + 1 < this.total)
680           {
681             this.index += this.column - get_col.call (this) + 1;
682             while (b.Index + b.Count () <= this.index)
683               b = this.blocks[++this.row];
684           }
685       }
686     else
687       this.index = b.Index + b.Count () - 1;
688   }
689
690   MIM.Candidates.prototype.Current = function ()
691   {
692     var b = this.blocks[this.row];
693     return b.get (this.index - b.Index);
694   }
695
696   MIM.Candidates.prototype.Select = function (selector)
697   {
698     var idx = this.index;
699     var gidx = this.column > 0 ? idx / this.column : this.row;
700     if (selector.type == 'selector')
701       {
702         switch (selector.val)
703           {
704           case '@<': first.call (this); break;
705           case '@>': last.call (this); break;
706           case '@-': prev.call (this); break;
707           case '@+': next.call (this); break;
708           case '@[': prev_group.call (this); break;
709           case '@]': next_group.call (this); break;
710           default: break;
711           }
712       }
713     else
714       {
715         var col, start, end
716         if (this.column > 0)
717           {
718             col = this.index % this.column;
719             start = this.index - col;
720             end = start + this.column;
721           }
722         else
723           {
724             start = this.blocks[this.row].Index;
725             col = this.index - start;
726             end = start + this.blocks[this.row].Count;
727           }
728         if (end > this.total)
729           end = this.total;
730         this.index += selector.val - col;
731         if (this.index >= end)
732           this.index = end - 1;
733         if (this.column > 0)
734           {
735             if (selector.val > col)
736               while (this.blocks[this.row].Index + this.blocks[this.row].Count
737                      < this.index)
738                 this.row++;
739             else
740               while (this.blocks[this.row].Index > this.index)
741                 this.row--;
742           }
743       }
744     var newgidx = this.column > 0 ? this.index / this.column : this.row;
745     if (this.index != idx)
746       this.ic.changed |= (gidx == newgidx
747                           ? MIM.ChangedStatus.CandidateIndex
748                           : MIM.ChangedStatus.CandidateList);
749     return this.Current ();
750   }
751
752   MIM.Candidates.prototype.CurrentCol = function ()
753   {
754     return get_col.call (this);
755   }
756
757   MIM.Candidates.prototype.CurrentGroup = function ()
758   {
759     var col, start, end, gnum, gidx;
760     if (this.column > 0)
761       {
762         gnum = Math.floor ((this.total - 1) / this.column) + 1;
763         col = this.index % this.column;
764         start = this.index - col;
765         gidx = start / this.column + 1;
766         end = start + this.column;
767         if (end > this.total)
768           end = this.total;
769       }
770     else
771       {
772         gnum = this.blocks.length;
773         gidx = this.row + 1;
774         start = this.blocks[this.row].Index;
775         col = this.index - start;
776         end = start + this.blocks[this.row].Count ();
777       }
778     var group = new Array ();
779     var indices = new Array (gnum, gidx, col);
780     group.push (indices);
781     var row = this.row;
782     var block = this.blocks[row++];
783     while (start < end)
784       {
785         var c = block.get (start - block.Index);
786         group.push (c);
787         start++;
788         if (start == block.Index + block.Count ())
789           block = this.blocks[row++];
790       }
791     return group;
792   }
793 }) ();
794
795 MIM.im_domain = new Xex.Domain ('input-method', null, null);
796 MIM.im_domain.DefType (MIM.KeySeq.prototype);
797 MIM.im_domain.DefType (MIM.Marker.prototype);
798 MIM.im_domain.DefType (MIM.Selector.prototype);
799 MIM.im_domain.DefType (MIM.Rule.prototype);
800 MIM.im_domain.DefType (MIM.Map.prototype);
801 MIM.im_domain.DefType (MIM.State.prototype);
802
803 (function () {
804   var im_domain = MIM.im_domain;
805
806   function Finsert (domain, vari, args)
807   {
808     var text;
809     if (args[0].type == 'integer')
810       text = String.fromCharCode (args[0].val);
811     else
812       text = args[0].val;
813     domain.context.ins (text, null);
814     return args[0];
815   }
816
817   function Finsert_candidates (domain, vari, args)
818   {
819     var ic = domain.context;
820     var gsize = domain.variables['candidates-group-size'];
821     var candidates = new MIM.Candidates (ic, args,
822                                          gsize ? gsize.val.Intval () : 0);
823     ic.ins (candidates.Current (), candidates);
824     return args[0];
825   }
826
827   function Fdelete (domain, vari, args)
828   {
829     var ic = domain.context;
830     var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
831     return new Xex.IntTerm (ic.del (pos));
832   }
833
834   function Fselect (domain, vari, args)
835   {
836     var ic = domain.context;
837     var can = ic.candidates;
838
839     if (can)
840       {
841         var old_text = can.Current ();
842         var new_text = can.Select (args[0]);
843         ic.rep (old_text, new_text, can);
844       }
845     else
846       Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
847     return args[0];
848   }
849
850   function Fshow (domain, vari, args)
851   {
852     domain.context.candidate_show = true;
853     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
854     return Xex.nil;
855   }
856
857   function Fhide (domain, vari, args)
858   {
859     domain.context.candidate_show = false;
860     domain.context.changed |= MIM.ChangedStatus.CandidateShow;
861     return Xex.nil;
862   }
863
864   function Fchar_at (domain, vari, args)
865   {
866     return new Xex.IntTerm (args[0].CharAt (domain.context));
867   }
868
869   function Fmove (domain, vari, args)
870   {
871     var ic = domain.context;
872     var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
873     ic.move (pos);
874     return new Xex.IntTerm (pos);
875   }
876
877   function Fmark (domain, vari, args)
878   {
879     args[0].Mark (domain.context);
880     return args[0];
881   }
882
883   function Fpushback (domain, vari, args)
884   {
885     var a = (args[0].IsInt ? args[0].Intval ()
886              : args[0].IsStr ? new KeySeq (args[0])
887              : args[0]);
888     domain.context.pushback (a);
889     return args[0];
890   }
891
892   function Fpop (domain, vari, args)
893   {
894     var ic = domain.context;
895     if (ic.key_head < ic.keys.val.length)
896       ic.keys.val.splice (ic.keys_head, 1);
897     return Xex.nil;
898   }
899
900   function Fundo  (domain, vari, args)
901   {
902     var ic = domain.context;
903     var n = args.length == 0 ? -2 : args[0].val;
904     Xex.Log ('undo with arg ' + args[0]);
905     if (n < 0)
906       ic.keys.val.splice (ic.keys.val.length + n, -n);
907     else
908       ic.keys.val.splice (n, ic.keys.val.length);
909     ic.reset (false);
910     return Xex.nil;
911   }
912
913   function Fcommit (domain, vari, args)
914   {
915     domain.context.commit ();
916     return Xex.nil;
917   }
918
919   function Funhandle (domain, vari, args)
920   {
921     domain.context.commit ();
922     return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
923   }
924
925   function Fshift (domain, vari, args)
926   {
927     var ic = domain.context;
928     var state_name = args[0].val;
929     var state = ic.im.state_list[state_name];
930     if (! state)
931       throw ("Unknown state: " + state_name);
932       ic.shift (state);
933     return args[0];
934   }
935
936   function Fshiftback (domain, vari, args)
937   {
938     domain.context.shift (null);
939     return Xex.nil;
940   }
941
942   function Fkey_count (domain, vari, args)
943   {
944     return new Xex.IntTerm (domain.context.key_head);
945   }
946
947   function Fsurrounding_flag (domain, vari, args)
948   {
949     return new Xex.IntTerm (-1);
950   }
951
952   im_domain.DefSubr (Finsert, "insert", false, 1, 1);
953   im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
954   im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
955   im_domain.DefSubr (Fselect, "select", false, 1, 1);
956   im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
957   im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
958   im_domain.DefSubr (Fmove, "move", false, 1, 1);
959   im_domain.DefSubr (Fmark, "mark", false, 1, 1);
960   im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
961   im_domain.DefSubr (Fpop, "pop", false, 0, 0);
962   im_domain.DefSubr (Fundo, "undo", false, 0, 1);
963   im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
964   im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
965   im_domain.DefSubr (Fshift, "shift", false, 1, 1);
966   im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
967   im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
968   im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
969   im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
970 }) ();
971
972
973 (function () {
974   function get_global_var (vname)
975   {
976     if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
977       MIM.im_global.Load ()
978     return MIM.im_global.domain.variables[vname];
979   }
980
981   function include (node)
982   {
983     node = node.firstElement ();
984     if (node.nodeName != 'tags')
985       return null;
986     
987     var lang = null, name = null, extra = null;
988     for (node = node.firstElement (); node; node = node.nextElement ())
989       {
990         if (node.nodeName == 'language')
991           lang = node.firstChild.nodeValue;
992         else if (node.nodeName == 'name')
993           name = node.firstChild.nodeValue;
994         else if (node.nodeName == 'extra-id')
995           extra = node.firstChild.nodeValue;
996       }
997     if (! lang || ! MIM.imlist[lang])
998       return null;
999     if (! extra)
1000       {
1001         if (! name || ! (im = MIM.imlist[lang][name]))
1002           return null;
1003       }
1004     else
1005       {
1006         if (! (im = MIM.imextra[lang][extra]))
1007           return null;
1008       }
1009     if (im.load_status != MIM.LoadStatus.Loaded
1010         && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
1011       return null;
1012     return im;
1013   }
1014
1015   var parsers = { };
1016
1017   parsers['description'] = function (node)
1018   {
1019     this.description = node.firstChild.nodeValue;
1020   }
1021   parsers['variable-list'] = function (node)
1022   {
1023     for (node = node.firstElement (); node; node = node.nextElement ())
1024       {
1025         var vname = node.attributes['vname'].nodeValue;
1026         if (this != MIM.im_global)
1027           {
1028             var vari = get_global_var (vname);
1029             if (vari != null)
1030               this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
1031           }
1032         vname = Xex.Term.Parse (this.domain, node)
1033       }
1034   }
1035   parsers['command-list'] = function (node)
1036   {
1037   }
1038   parsers['macro-list'] = function (node)
1039   {
1040     for (var n = node.firstElement (); n; n = n.nextElement ())
1041       if (n.nodeName == 'xi:include')
1042         {
1043           var im = include (n);
1044           if (! im)
1045             alert ('inclusion fail');
1046           else
1047             for (var macro in im.domain.functions)
1048               {
1049                 var func = im.domain.functions[macro];
1050                 if (func instanceof Xex.Macro)
1051                   im.domain.CopyFunc (this.domain, macro);
1052               }
1053           n = n.previousSibling;
1054           node.removeChild (n.nextSibling);
1055         }
1056     Xex.Term.Parse (this.domain, node.firstElement (), null);
1057   }
1058   parsers['title'] = function (node)
1059   {
1060     this.title = node.firstChild.nodeValue;
1061   }
1062   parsers['map-list'] = function (node)
1063   {
1064     for (node = node.firstElement (); node; node = node.nextElement ())
1065       {
1066         if (node.nodeName == 'xi:include')
1067           {
1068             var im = include (node);
1069             if (! im)
1070               {
1071                 alert ('inclusion fail');
1072                 continue;
1073               }
1074             for (var mname in im.map_list)
1075               this.map_list[mname] = im.map_list[mname];
1076           }
1077         else
1078           {
1079             var map = Xex.Term.Parse (this.domain, node);
1080             this.map_list[map.name] = map;
1081           }
1082       }
1083   }
1084   parsers['state-list'] = function (node)
1085   {
1086     this.domain.map_list = this.map_list;
1087     for (node = node.firstElement (); node; node = node.nextElement ())
1088       {
1089         if (node.nodeName == 'xi:include')
1090           {
1091             var im = include (node);
1092             if (! im)
1093               alert ('inclusion fail');
1094             for (var sname in im.state_list)
1095               {
1096                 state = im.state_list[sname];
1097                 if (! this.initial_state)
1098                   this.initial_state = state;
1099                 this.state_list[sname] = state;
1100               }
1101           }
1102         else if (node.nodeName == 'state')
1103           {
1104             var state = Xex.Term.Parse (this.domain, node);
1105             if (! state.title)
1106               state.title = this.title;
1107             if (! this.initial_state)
1108               this.initial_state = state;
1109             this.state_list[state.name] = state;
1110           }
1111       }
1112     delete this.domain.map_list;
1113   }
1114
1115   MIM.IM = function (lang, name, extra_id, file)
1116   {
1117     this.lang = lang;
1118     this.name = name;
1119     this.extra_id = extra_id;
1120     this.file = file;
1121     this.load_status = MIM.LoadStatus.NotLoaded;
1122     this.domain = new Xex.Domain (this.lang + '-'
1123                                   + (this.name != 'nil'
1124                                      ? this.name : this.extra_id),
1125                                   MIM.im_domain, null);
1126   }
1127
1128   var proto = {
1129     Load: function ()
1130     {
1131       var node = Xex.Load (MIM.server, this.file);
1132       if (! node)
1133         {
1134           this.load_status = MIM.LoadStatus.Error;
1135           return false;
1136         }
1137       this.map_list = {};
1138       this.initial_state = null;
1139       this.state_list = {};
1140       for (node = node.firstElement (); node; node = node.nextElement ())
1141         {
1142           var name = node.nodeName;
1143           var parser = parsers[name];
1144           if (parser)
1145             parser.call (this, node);
1146         }
1147       this.load_status = MIM.LoadStatus.Loaded;
1148       return true;
1149     }
1150   }
1151
1152   MIM.IM.prototype = proto;
1153
1154   MIM.IC = function (im, target)
1155   {
1156     if (im.load_status == MIM.LoadStatus.NotLoaded)
1157       im.Load ();
1158     if (im.load_status != MIM.LoadStatus.Loaded)
1159       alert ('im:' + im.name + ' error:' + im.load_status);
1160     this.im = im;
1161     this.target = target;
1162     this.domain = new Xex.Domain ('context', im.domain, this);
1163     this.active = true;
1164     this.range = new Array ();
1165     this.range[0] = this.range[1] = 0;
1166     this.state = null;
1167     this.initial_state = this.im.initial_state;
1168     this.keys = new MIM.KeySeq ();
1169     this.marker_positions = new Array ();
1170     this.candidate_table = new MIM.CandidateTable ();
1171     this.reset (false);
1172   }
1173
1174   MIM.CandidateTable = function ()
1175   {
1176     this.table = new Array ();
1177   }
1178
1179   MIM.CandidateTable.prototype.get = function (pos)
1180   {
1181     for (var i = 0; i < this.table.length; i++)
1182       {
1183         var elt = this.table[i];
1184         if (elt.from < pos && pos <= elt.to)
1185           return elt.val;
1186       }
1187   }
1188
1189   MIM.CandidateTable.prototype.put = function (from, to, candidates)
1190   {
1191     for (var i = 0; i < this.table.length; i++)
1192       {
1193         var elt = this.table[i];
1194         if (elt.from < to && elt.to > from)
1195           {
1196             elt.from = from;
1197             elt.to = to;
1198             elt.val = candidates;
1199             return;
1200           }
1201       }
1202     this.table.push ({ from: from, to: to, val: candidates });
1203   }
1204
1205   MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
1206   {
1207     var diff = inserted - (to - from);
1208     if (diff == 0)
1209       return;
1210     for (var i = 0; i < this.table.length; i++)
1211       {
1212         var elt = this.table[i];
1213         if (elt.from >= to)
1214           {
1215             elt.from += diff;
1216             elt.to += diff;
1217           }
1218       }
1219   }
1220
1221   MIM.CandidateTable.prototype.clear = function ()
1222   {
1223     this.table.length = 0;
1224   }
1225
1226   function set_cursor (prefix, pos)
1227   {
1228     this.cursor_pos = pos;
1229     var candidates = this.candidate_table.get (pos);
1230     if (this.candidates != candidates)
1231       {
1232         this.candidates = candidates;
1233         this.changed |= MIM.ChangedStatus.CandidateList;
1234       }
1235   }
1236
1237   function save_state ()
1238   {
1239     this.state_var_values = this.domain.SaveValues ();
1240     this.state_preedit = this.preedit;
1241     this.state_key_head = this.key_head;
1242     this.state_pos = this.cursor_pos;
1243   }
1244
1245   function restore_state ()
1246   {
1247     this.domain.RestoreValues (this.state_var_values);
1248     this.preedit = this.state_preedit;
1249     set_cursor.call (this, "restore", this.state_pos);
1250   }
1251
1252   function handle_key ()
1253   {
1254     Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
1255              + '" in ' + this.state.name + ':' + this.keymap.name
1256              + " key/state/commit-head/len:"
1257              + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
1258     var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
1259     var sub = out.map;
1260
1261     if (out.index > this.key_head)
1262       {
1263         this.key_head = out.index;
1264         Xex.Log (' with submap', false, true);
1265         restore_state.call (this);
1266         this.keymap = sub;
1267         if (sub.map_actions)
1268           {
1269             Xex.Log ('taking map actions:');
1270             if (! this.take_actions (sub.map_actions))
1271               return false;
1272           }
1273         else if (sub.submaps)
1274           {
1275             Xex.Log ('no map actions');
1276             for (var i = this.state_key_head; i < this.key_head; i++)
1277               {
1278                 Xex.Log ('inserting key:' + this.keys.val[i].key);
1279                 this.ins (this.keys.val[i].key, null);
1280               }
1281           }
1282         if (! sub.submaps)
1283           {
1284             Xex.Log ('terminal:');
1285             if (this.keymap.branch_actions)
1286               {
1287                 Xex.Log ('branch actions:');
1288                 if (! this.take_actions (this.keymap.branch_actions))
1289                   return false;
1290               }
1291             if (sub != this.state.keymap)
1292               this.shift (this.state);
1293           }
1294       }
1295     else
1296       {
1297         Xex.Log (' without submap', false, true);
1298         this.keymap = sub;
1299         var current_state = this.state;
1300         var map = this.keymap;
1301
1302         if (map.branch_actions)
1303           {
1304             Xex.Log ('branch actions:');
1305             if (! this.take_actions (map.branch_actions))
1306               return false;
1307           }
1308
1309         if (map == this.keymap)
1310           {
1311             Xex.Log ('no state change');
1312             if (map == this.initial_state.keymap
1313                 && this.key_head < this.keys.val.length)
1314               {
1315                 Xex.Log ('unhandled');
1316                 return false;
1317               }
1318             if (this.keymap != current_state.keymap)
1319               this.shift (current_state);
1320             else if (this.keymap.actions == null)
1321               this.shift (this.initial_state);
1322           }
1323       }
1324     return true;
1325   }
1326
1327   proto = {
1328     reset: function (clear_keys)
1329     {
1330       this.cursor_pos = 0;
1331       this.prev_state = null;
1332       this.title = this.initial_state.title;
1333       this.state_preedit = '';
1334       this.state_key_head = 0;
1335       this.state_var_values = {};
1336       this.state_pos = 0;
1337       this.key_head = 0;
1338       if (clear_keys)
1339         this.keys.val.length = 0;
1340       this.commit_key_head = 0;
1341       this.key_unhandled = false;
1342       this.unhandled_key = null;
1343       this.changed = MIM.ChangedStatus.None;
1344       this.error_message = '';
1345       this.title = this.initial_state.title;
1346       this.produced = '';
1347       this.preedit = '';
1348       this.preedit_saved = '';
1349       if (this.candidate_show)
1350         MIM.hide (this);
1351       this.candidate_table.clear ();
1352       this.candidates = null;
1353       this.candidate_show = false;
1354       for (var elt in this.marker_positions)
1355         this.marker_positions[elt] = 0;
1356       this.shift (this.initial_state);
1357     },
1358
1359     catch_args: new Array (Xex.CatchTag._mimtag, null),
1360
1361     take_actions: function (actions)
1362     {
1363       if (actions.length == 0)
1364         return true;;
1365       var func_progn = this.domain.GetFunc ('progn');
1366       var func_catch = this.domain.GetFunc ('catch');
1367       this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
1368       var term = new Xex.Funcall (func_catch, null, this.catch_args);
1369       term = term.Eval (this.domain);
1370       return (! term.IsSymbol || term.val != '@mimtag');
1371     },
1372
1373     GetSurroundingChar: function (pos)
1374     {
1375       if (pos < 0)
1376         {
1377           pos += this.range[0];
1378           if (pos < 0)
1379             return -1;
1380         }
1381       else
1382         {
1383           pos += this.range[1];
1384           if (pos >= this.target.value.length)
1385             return -1;
1386         }
1387       return this.target.value.charCodeAt (pos);
1388     },
1389     
1390     DelSurroundText: function (pos)
1391     {
1392       var text;
1393       if (pos < 0)
1394         {
1395           pos += this.range[0];
1396           if (pos <= 0)
1397             {
1398               pos = 0; text = '';
1399             }
1400           else
1401             text = this.target.value.substring (0, pos);
1402           if (this.range[0] < this.target.value.length)
1403             text += this.target.value.substring (this.range[0]);
1404           this.target.value = text;
1405           this.range[1] -= this.range[0] - pos;
1406           this.range[0] = pos;
1407         }
1408       else
1409         {
1410           pos += this.range[1];
1411           text = this.target.value.substring (0, this.range[1]);
1412           if (pos >= this.target.value.length)
1413             pos = this.target.value.length;
1414           else
1415             text += this.target.value.substring (pos);
1416           this.target.value = text;
1417         }
1418     },
1419
1420     adjust_markers: function (from, to, inserted)
1421     {
1422       var diff = inserted - (to - from);
1423
1424       for (var name in this.marker_positions)
1425         {
1426           var pos = this.marker_positions[name];
1427           if (pos > from)
1428             {
1429               if (pos >= to)
1430                 this.marker_positions[name] += diff;
1431               else if (pos > from)
1432                 this.marker_positions[name] = from;
1433             }
1434         }
1435     },
1436
1437     preedit_replace: function (from, to, text, candidates)
1438     {
1439       var newlen = text.length;
1440       this.preedit = (this.preedit.substring (0, from)
1441                       + text + this.preedit.substring (to));
1442       this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
1443       this.adjust_markers (from, to, newlen);
1444       this.candidate_table.adjust (from, to, newlen);
1445       if (candidates)
1446         this.candidate_table.put (from, from + newlen, candidates)
1447       if (this.cursor_pos >= to)
1448         set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
1449       else if (this.cursor_pos > from)
1450         set_cursor.call (this, 'adjust', from)
1451     },
1452
1453     ins: function (text, candidates)
1454     {
1455       this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
1456     },
1457
1458     rep: function (old_text, new_text, candidates)
1459     {
1460       this.preedit_replace (this.cursor_pos - old_text.length,
1461                             this.cursor_pos, new_text, candidates);
1462     },
1463
1464     del: function (pos)
1465     {
1466       var deleted = pos - this.cursor_pos;
1467       if (pos < this.cursor_pos)
1468         {
1469           if (pos < 0)
1470             {
1471               this.DelSurroundText (pos);
1472               deleted = - this.cursor_pos;
1473               pos = 0;
1474             }
1475           if (pos < this.cursor_pos)
1476             this.preedit_replace (pos, this.cursor_pos, '', null);
1477         }
1478       else
1479         {
1480           if (pos > this.preedit.length)
1481             {
1482               this.DelSurroundText (pos - this.preedit.length);
1483               deleted = this.preedit.length - this.cursor_pos;
1484               pos = this.preedit.length;
1485             }
1486           if (pos > this.cursor_pos)
1487             this.preedit_replace (this.cursor_pos, pos, '', null);
1488         }
1489       return deleted;
1490     },
1491
1492     show: function ()
1493     {
1494       this.candidate_show = true;
1495       this.changed |= MIM.ChangedStatus.CandidateShow;
1496     },
1497
1498     hide: function ()
1499     {
1500       this.candidate_show = false;
1501       this.changed |= MIM.ChangedStatus.CandidateShow;
1502     },
1503
1504     move: function (pos)
1505     {
1506       if (pos < 0)
1507         pos = 0;
1508       else if (pos > this.preedit.length)
1509         pos = this.preedit.length;
1510       if (pos != this.cursor_pos)
1511         {
1512           set_cursor.call (this, 'move', pos);
1513           this.changed |= MIM.ChangedStatus.Preedit;
1514         }
1515     },
1516
1517     pushback: function (n)
1518     {
1519       if (n instanceof MIM.KeySeq)
1520         {
1521           if (this.key_head > 0)
1522             this.key_head--;
1523           if (this.key_head < this.keys.val.length)
1524             this.keys.val.splice (this.key_head,
1525                                   this.keys.val.length - this.key_head);
1526           for (var i = 0; i < n.val.length; i++)
1527             this.keys.val.push (n.val[i]);
1528           return;
1529         }
1530       if (n > 0)
1531         {
1532           this.key_head -= n;
1533           if (this.key_head < 0)
1534             this.key_head = 0;
1535         }
1536       else if (n == 0)
1537         this.key_head = 0;
1538       else
1539       {
1540         this.key_head = - n;
1541         if (this.key_head > this.keys.val.length)
1542           this.key_head = this.keys.val.length;
1543       }
1544     },
1545
1546     pop: function ()
1547     {
1548       if (this.key_head < this.keys.val.length)
1549         this.keys.val.splice (this.key_head, 1);
1550     },
1551
1552     commit: function ()
1553     {
1554       if (this.preedit.length > 0)
1555       {
1556         this.candidate_table.clear ();
1557         this.produced += this.preedit;
1558         this.preedit_replace.call (this, 0, this.preedit.length, '', null);
1559         this.preedit_saved = '';
1560         this.state_pos = 0;
1561         this.commit_key_head = this.key_head;
1562       }
1563     },
1564
1565     shift: function (state)
1566     {
1567       if (state == null)
1568         {
1569           if (this.prev_state == null)
1570             return;
1571           state = this.prev_state;
1572         }
1573
1574       if (state == this.initial_state)
1575         {
1576           if (this.state)
1577             {
1578               this.commit ();
1579               this.keys.val.splice (0, this.key_head);
1580               this.key_head = this.state_key_head = this.commit_key_head = 0;
1581               this.prev_state = null;
1582             }
1583         }
1584       else
1585         {
1586           if (state != this.state)
1587             this.prev_state = this.state;
1588         }
1589       if (state != this.state && state.enter_actions)
1590         this.take_actions (state.enter_actions);
1591       if (! this.state || this.state.title != state.title)
1592         this.changed |= MIM.ChangedStatus.StateTitle;
1593       this.state = state;
1594       this.keymap = state.keymap;
1595       save_state.call (this);
1596     },
1597
1598     Filter: function (key)
1599     {
1600       if (! this.active)
1601         {
1602           Xex.Log ("active = false");
1603           this.key_unhandled = true;
1604           this.unhandled_key = key;
1605           return false;
1606         }
1607       if (key.key == '_reload')
1608         return true;
1609       this.changed = MIM.ChangedStatus.None;
1610       this.produced = '';
1611       this.key_unhandled = false;
1612       this.keys.val.push (key);
1613       var count = 0;
1614       while (this.key_head < this.keys.val.length)
1615         {
1616           if (! handle_key.call (this))
1617             {
1618               if (this.key_head < this.keys.val.length)
1619                 {
1620                   this.unhandled_key = this.keys.val[this.key_head];
1621                   this.keys.val.splice (this.key_head, this.key_head + 1);
1622                 }
1623               if (this.state_key_head > 0)
1624                 this.state_key_head--;
1625               if (this.commit_key_head > 0)
1626                 this.commit_key_head--;
1627               this.key_unhandled = true;
1628               break;
1629             }
1630           if (++count == 10)
1631             {
1632               this.reset (true);
1633               this.key_unhandled = true;
1634               break;
1635             }
1636         }
1637       if (this.keymap == this.initial_state.keymap)
1638         this.commit ();
1639
1640       if (this.commit_key_head > 0)
1641         {
1642           this.keys.val.splice (0, this.commit_key_head);
1643           this.key_head -= this.commit_key_head;
1644           this.state_key_head -= this.commit_key_head;
1645           this.commit_key_head = 0;
1646         }
1647       if (this.key_unhandled)
1648         {
1649           this.keys.val.length = 0;
1650           //this.keys.val.splice (0, this.keys.val.length);
1651           this.key_head = this.state_key_head = this.commit_key_head = 0;
1652         }
1653       if (this.changed & MIM.ChangedStatus.Candidate)
1654         {
1655           if (this.candidate_show && this.candidates)
1656             MIM.show (this);
1657           else
1658             MIM.hide (this);
1659         }
1660       return (! this.key_unhandled
1661               && this.produced.length == 0);
1662     }
1663   }
1664
1665   MIM.IC.prototype = proto;
1666
1667   var node = Xex.Load (MIM.server, "imlist.xml");
1668   for (node = node.firstChild; node; node = node.nextSibling)
1669     if (node.nodeName == 'input-method')
1670       {
1671         var lang = null, name = null, extra_id = null, file = null;
1672
1673         for (var n = node.firstChild; n; n = n.nextSibling)
1674           {
1675             if (n.nodeName == 'language')
1676               lang = n.firstChild.nodeValue;
1677             else if (n.nodeName == 'name')
1678               name = n.firstChild.nodeValue;
1679             else if (n.nodeName == 'extra-id')
1680               extra_id = n.firstChild.nodeValue;
1681             else if (n.nodeName == 'filename')
1682               file = n.firstChild.nodeValue;
1683           }
1684         if (name && name != 'nil')
1685           {
1686             if (! MIM.imlist[lang])
1687               MIM.imlist[lang] = {};
1688             MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
1689           }
1690         else if (extra_id && extra_id != 'nil')
1691           {
1692             if (! MIM.imextra[lang])
1693               MIM.imextra[lang] = {};
1694             MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
1695           }
1696       }
1697   if (MIM.imextra.t && MIM.imextra.t.global)
1698     MIM.im_global = MIM.imextra.t.global;
1699   else
1700     {
1701       MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
1702       MIM.im_global.load_status = MIM.LoadStatus.Error;
1703     }
1704   node = undefined;
1705 }) ();
1706
1707 (function () {
1708   var keys = new Array ();
1709   keys[0x09] = 'tab';
1710   keys[0x08] = 'backspace';
1711   keys[0x0D] = 'return';
1712   keys[0x1B] = 'escape';
1713   keys[0x20] = 'space';
1714   keys[0x21] = 'pageup';
1715   keys[0x22] = 'pagedown';
1716   keys[0x23] = 'end';
1717   keys[0x24] = 'home';
1718   keys[0x25] = 'left';
1719   keys[0x26] = 'up';
1720   keys[0x27] = 'right';
1721   keys[0x28] = 'down';
1722   keys[0x2D] = 'insert';
1723   keys[0x2E] = 'delete';
1724   for (var i = 1; i <= 12; i++)
1725     keys[111 + i] = "f" + i;
1726   keys[0x90] = "numlock";
1727   keys[0xF0] = "capslock";
1728
1729   var keyids = {};
1730   keyids['U+0008'] = 'Backspace';
1731   keyids['U+0009'] = 'Tab';
1732   keyids['U+0018'] = 'Cancel';
1733   keyids['U+001B'] = 'Escape';
1734   keyids['U+0020'] = 'Space';
1735   keyids['U+007F'] = 'Delete';
1736
1737   var modifiers = {}
1738   modifiers.Shift = 1;
1739   modifiers.Control = 1;
1740   modifiers.Alt = 1;
1741   modifiers.AltGraph = 1;
1742   modifiers.Meta = 1
1743
1744   MIM.decode_key_event = function (event)
1745   {
1746     var key = event.keyIdentifier;
1747
1748     if (key)                    // keydown event of Chrome
1749       {
1750         if (modifiers[key])
1751           return false;
1752         var mod = '';
1753         if (event.ctrlKey) mod += 'C-';
1754         if (event.metaKey) mod += 'M-';
1755         if (event.altKey) mod += 'A-';
1756         var keysym = keyids[key];
1757         if (keysym)
1758           key = keysym;
1759         else if (key.match(/^U\+([0-9A-Z]+)$/))
1760           {
1761             if (mod.length == 0)
1762               return false;
1763             key = String.fromCharCode (parseInt (RegExp.$1, 16));
1764           }
1765         //else
1766         //key = key.toLowerCase ();
1767         if (event.shiftKey) mod += 'S-';
1768         return new MIM.Key (mod + key);
1769       }
1770     else
1771       {
1772         key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
1773                : event.charCode ? event.charCode
1774                : false);
1775         if (! key)
1776           return false;
1777         if (event.type == 'keydown')
1778           {
1779             key = keys[key];
1780             if (! key)
1781               return false;
1782             if (event.shiftKey) key = "S-" + key ;
1783           }
1784         else
1785           key = String.fromCharCode (key);
1786       }
1787     if (event.altKey) key = "A-" + key ;
1788     if (event.ctrlKey) key = "C-" + key ;
1789     return new MIM.Key (key);
1790   }
1791 }) ();
1792
1793 MIM.add_event_listener
1794   = (window.addEventListener
1795      ? function (target, type, listener) {
1796        target.addEventListener (type, listener, false);
1797      }
1798      : window.attachEvent
1799      ? function (target, type, listener) {
1800        target.attachEvent ('on' + type,
1801                            function() {
1802                              listener.call (target, window.event);
1803                            });
1804      }
1805      : function (target, type, listener) {
1806        target['on' + type]
1807          = function (e) { listener.call (target, e || window.event); };
1808      });
1809
1810 MIM.debug_print = function () { };
1811
1812 MIM.get_range = function (target, ic)
1813 {
1814   var from, to;
1815   if (target.selectionStart != null) // for Mozilla
1816     {
1817       from = target.selectionStart;
1818       to = target.selectionEnd;
1819     }
1820   else                          // for IE
1821     {
1822       var r = document.selection.createRange ();
1823       var rr = r.duplicate ();
1824
1825       rr.moveToElementText (target);
1826       rr.setEndPoint ('EndToEnd', range);
1827       from = rr.text.length - r.text.length;
1828       to = rr.text.length;
1829     }
1830   if (from == to
1831       && from == ic.range[0] + ic.cursor_pos
1832       && (ic.preedit.length == 0
1833           || ic.preedit == target.value.substring (ic.range[0], ic.range[1])))
1834     return true;
1835   ic.reset (true);
1836   ic.range[0] = from;
1837   ic.range[1] = to;
1838   return false;
1839 };
1840
1841 (function () {
1842   var temp;
1843
1844   var style_props = {
1845     width: 'width',
1846     height: 'height',
1847     padingLeft: 'padding-left',
1848     paddingRight: 'padding-right',
1849     paddingTop: 'padding-top',
1850     paddintBottom: 'padding-bottom', 
1851     borderLeftStyle: 'border-left-style',
1852     borderRightStyle: 'border-right-style',
1853     borderTopStyle: 'border-top-style',
1854     borderBottomStyle: 'border-bottom-style',
1855     borderLeftWidth: 'border-left-width',
1856     borderRightWidth: 'border-right-width',
1857     borderTopWidth: 'border-top-width',
1858     borderBottomWidth: 'border-bottom-width',
1859     fontFamily: 'font-family',
1860     fontSize: 'font-size',
1861     lineHeight: 'line-height',
1862     letterSpacing: 'letter-spacing',
1863     wordSpacing: 'word-spacing' };
1864
1865   function copy_style (from, to)
1866   {
1867     var from_style = getComputedStyle(from,'');
1868     for(var name in style_props)
1869       to.style[name] = from_style.getPropertyValue (style_props[name]);
1870     to.style.left = from.offsetLeft + 'px'; 
1871     to.style.top = from.offsetTop + 'px';
1872     to.style.width = from.offsetWidth;
1873     to.style.height = from.offsetHeight;
1874   }
1875
1876   MIM.get_preedit_pos = function (target, ic)
1877   {
1878     if (! temp)
1879       {
1880         temp = document.createElement ('div');
1881         temp.style.visibility = 'hidden';
1882         temp.style.position = 'absolute';
1883         temp.appendChild (document.createElement ('span'));
1884         temp.appendChild (document.createElement ('span'));
1885         document.getElementsByTagName ('body')[0].appendChild (temp);
1886       }
1887     if (temp.ic != ic)
1888       {
1889         temp.ic = ic;
1890         copy_style (target, temp);
1891         ic.abs_top = 0;
1892         ic.abs_left = 0;
1893         for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
1894           {
1895             ic.abs_top += elm.offsetTop;
1896             ic.abs_left += elm.offsetLeft;
1897           }
1898       }
1899     temp.firstChild.innerText = target.value.substring (0, ic.range[0]);
1900     temp.lastChild.innerText
1901       = (ic.range[0] == ic.range[1] ? "|"
1902          : target.value.substring (ic.range[0], ic.range[1]));
1903     ic.abs_y = (ic.abs_top + temp.lastChild.offsetTop
1904                 + temp.lastChild.offsetHeight - target.scrollTop);
1905     ic.abs_x0 = ic.abs_left + temp.lastChild.offsetLeft;
1906     ic.abs_x1 = ic.abs_x0 + temp.lastChild.offsetWidth;
1907   }
1908 }) ();
1909
1910 MIM.update_bar = function (target, ic)
1911 {
1912   if (ic.preedit.length > 0)
1913     {
1914       MIM.get_preedit_pos (target, ic);
1915       if (! ic.bar)
1916         {
1917           ic.bar = document.createElement ('div');
1918           ic.bar.style.position = 'absolute';
1919           ic.bar.style.backgroundColor = "black";
1920           ic.bar.style.minHeight = '1px';
1921           document.getElementsByTagName ('body')[0].appendChild (ic.bar);
1922         }
1923       ic.bar.style.display = 'block'
1924       ic.bar.style.top = (ic.abs_y + 1) + 'px';
1925       ic.bar.style.left = ic.abs_x0 + 'px';
1926       ic.bar.style.minWidth = ic.bar.style.maxWidth = (ic.abs_x1 - ic.abs_x0) + 'px';
1927     }
1928   else if (ic.bar)
1929     ic.bar.style.display = 'none'
1930 };
1931
1932 MIM.update = function (target, ic, for_focus_out)
1933 {
1934   var text = target.value;
1935   target.value = (text.substring (0, ic.range[0])
1936                   + ic.produced
1937                   + ic.preedit
1938                   + text.substring (ic.range[1]));
1939   ic.range[0] += ic.produced.length;
1940   ic.range[1] = ic.range[0] + ic.preedit.length;
1941   MIM.update_bar (target, ic);
1942   if (! for_focus_out)
1943     {
1944       var pos = ic.range[0] + ic.cursor_pos;
1945       if (target.setSelectionRange) // Mozilla
1946         {
1947           var scrollTop = target.scrollTop;
1948           target.setSelectionRange (pos, pos);
1949           target.scrollTop = scrollTop;
1950         }
1951       else                      // IE
1952         {
1953           var range = target.createTextRange ();
1954           range.moveStart ('character', pos);
1955           range.moveEnd ('character', pos);
1956           range.select ();
1957         }
1958     }
1959 };
1960
1961 (function () {
1962   MIM.show = function (ic)
1963   {
1964     if (! ic.candidates)
1965       return;
1966     var target = ic.target;
1967     MIM.get_preedit_pos (target, ic);
1968     if (! ic.can_node)
1969       {
1970         ic.can_node = document.createElement ('table');
1971         ic.can_node.style.position = 'absolute';
1972         ic.can_node.style.display = 'none';
1973         ic.can_node.style.backgroundColor = "white";
1974         ic.can_node.style.border = "1px solid black";
1975         document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
1976       }
1977
1978     if (ic.changed & MIM.ChangedStatus.CandidateList)
1979       {
1980         while (ic.can_node.childNodes.length > 0)
1981           ic.can_node.removeChild (ic.can_node.firstChild);
1982         var tr = document.createElement ('tr');
1983         var group = ic.candidates.CurrentGroup ();
1984         var td = document.createElement ('td');
1985         td.innerHTML = group[0][1] + '/' + group[0][0];
1986         td.style.color = 'white';
1987         td.style.backgroundColor = 'black';
1988         tr.appendChild (td);
1989         for (var i = 1; i < group.length; i++)
1990           {
1991             var td = document.createElement ('td');
1992             td.noWrap = true;
1993             td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
1994             if (i == group[0][2] + 1)
1995               td.style.backgroundColor = 'lightblue';
1996             tr.appendChild (td);
1997           }
1998         ic.can_node.appendChild (tr);
1999         ic.can_node.style.top = (ic.abs_y + 10) + 'px';
2000         ic.can_node.style.left = ic.abs_x0 + 'px';
2001       }
2002     else
2003       {
2004         var td = ic.can_node.firstElement ().firstElement ().nextElement ();
2005         var col = ic.candidates.CurrentCol ();
2006         for (var i = 0; td; td = td.nextElement ())
2007           td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
2008       }
2009     ic.can_node.style.display = 'block';
2010   }
2011
2012   MIM.hide = function (ic)
2013   {
2014     if (ic.can_node)
2015       ic.can_node.style.display = 'none';
2016   }
2017
2018   function real_focus_in (target, ic)
2019   {
2020     if (MIM.get_range (target, ic))
2021       ic.Filter (MIM.Key.FocusIn);
2022     MIM.update (target, ic, false);
2023   }
2024
2025   var focus_in_timer;
2026
2027   MIM.focus_in = function (event)
2028   {
2029     var target = event.target;
2030     Xex.Log ("Focus in " + target.tagName);
2031     focus_in_timer
2032       = setTimeout (function () {real_focus_in (target, target.mim_ic)} , 10);
2033     return true;
2034   }
2035
2036   MIM.click = function (event)
2037   {
2038     var target = event.target;
2039     Xex.Log ("Click in " + target.tagName);
2040     if (focus_in_timer)
2041       {
2042         clearTimeout (focus_in_timer);
2043         focus_in_timer = null;
2044       }
2045     real_focus_in (target, target.mim_ic);
2046   }
2047
2048 }) ();
2049
2050 MIM.focus_out = function (event)
2051 {
2052   var target = event.target;
2053   var ic = target.mim_ic;
2054   Xex.Log ("Focus out " + target.tagName);
2055   MIM.get_range (target, ic);
2056   MIM.debug_print (event, ic);
2057   ic.Filter (MIM.Key.FocusOut);
2058   MIM.update (target, ic, true);
2059   return true;
2060 };
2061
2062 MIM.keydown = function (event)
2063 {
2064   var target = event.target;
2065   if (! (target.type == "text" || target.type == "textarea"))
2066     return;
2067
2068   var ic = target.mim_ic;
2069   if (! ic || ic.im != MIM.current)
2070     {
2071       target.mim_ic = null;
2072       Xex.Log ('creating IC');
2073       ic = new MIM.IC (MIM.current, target);
2074       if (ic.im.load_status != MIM.LoadStatus.Loaded)
2075         return true;
2076       target.mim_ic = ic;
2077       MIM.add_event_listener (target, 'focus', MIM.focus_in);
2078       MIM.add_event_listener (target, 'blur', MIM.focus_out);
2079       MIM.add_event_listener (target, 'click', MIM.click);
2080     }
2081   MIM.get_range (target, ic)
2082   MIM.debug_print (event, ic);
2083   ic.key = MIM.decode_key_event (event);
2084   if (ic.key)
2085     {
2086       try {
2087         var result = ic.Filter (ic.key);
2088       } catch (e) {
2089         Xex.Log ('Error' + e);
2090         throw (e);
2091       }
2092       MIM.update (target, ic, false);
2093       if (! ic.key_unhandled)
2094         event.preventDefault ();
2095     }
2096 };
2097
2098 MIM.keypress = function (event)
2099 {
2100   var target = event.target;
2101   if (! (target.type == "text" || target.type == "textarea"))
2102     return;
2103
2104   var ic = target.mim_ic;
2105   var i;
2106
2107   try {
2108     if (ic.im.load_status != MIM.LoadStatus.Loaded)
2109       return;
2110     if (! ic.key)
2111       ic.key = MIM.decode_key_event (event);
2112     if (! ic.key)
2113       {
2114         ic.reset (true);
2115         return;
2116       }
2117     
2118     try {
2119       var result = ic.Filter (ic.key);
2120     } catch (e) {
2121       Xex.Log ('Error:' + e);
2122       throw (e);
2123     }
2124     MIM.update (target, ic, false);
2125     if (! ic.key_unhandled)
2126       event.preventDefault ();
2127   } catch (e) {
2128     Xex.Log ("error:" + e);
2129     event.preventDefault ();
2130   } finally {
2131     MIM.debug_print (event, ic);
2132   }
2133
2134   return;
2135 };
2136
2137 (function () {
2138   var lang_category = {
2139     European: {
2140       cs: { name: 'Czech' },
2141       da: { name: 'Danish' },
2142       el: { name: 'Greek' },
2143       en: { name: 'English' },
2144       eo: { name: 'Esperanto' },
2145       fr: { name: 'French' },
2146       grc: { name: 'ClassicGreek' },
2147       hr: { name: 'Croatian' },
2148       hy: { name: 'Armenian' },
2149       ka: { name: 'Georgian' },
2150       kk: { name: 'Kazakh' },
2151       ru: { name: 'Russian' },
2152       sk: { name: 'Slovak' },
2153       sr: { name: 'Serbian' },
2154       sv: { name: 'Swedish' },
2155       yi: { name: 'Yiddish' } },
2156     MiddleEast: {
2157       ar: { name: 'Arabic' },
2158       dv: { name: 'Divehi' },
2159       fa: { name: 'Persian' },
2160       he: { name: 'Hebrew' },
2161       kk: { name: 'Kazakh' },
2162       ps: { name: 'Pushto' },
2163       ug: { name: 'Uighur' },
2164       yi: { name: 'Yiddish' } },
2165     SouthAsia: {
2166       as: { name: 'Assamese' },
2167       bn: { name: 'Bengali' },
2168       bo: { name: 'Tibetan' },
2169       gu: { name: 'Gujarati' },
2170       hi: { name: 'Hindi' },
2171       kn: { name: 'Kannada' },
2172       ks: { name: 'Kashmiri' },
2173       ml: { name: 'Malayalam' },
2174       mr: { name: 'Marathi' },
2175       ne: { name: 'Nepali' },
2176       or: { name: 'Oriya' },
2177       pa: { name: 'Panjabi' },
2178       sa: { name: 'Sanskirit' },
2179       sd: { name: 'Sindhi' },
2180       si: { name: 'Sinhalese' },
2181       ta: { name: 'Tamil' },
2182       te: { name: 'Telugu' },
2183       ur: { name: 'Urdu' } },
2184     SouthEastAsia: {
2185       cmc: { name: 'Cham' },
2186       km: { name: 'Khmer'},
2187       lo: { name: 'Lao' },
2188       my: { name: 'Burmese' },
2189       tai: { name: 'Tai Viet' },
2190       th: { name: 'Thai' },
2191       vi: { name: 'Vietanamese' } },
2192     EastAsia: {
2193       ii: { name: 'Yii' },
2194       ja: { name: 'Japanese' },
2195       ko: { name: 'Korean' },
2196       zh: { name: 'Chinese' } },
2197     Other: {
2198       am: { name:  'Amharic' },
2199       ath: { name: 'Carrier' },
2200       bla: { name: 'Blackfoot' },
2201       cr: { name: 'Cree' },
2202       eo: { name: 'Esperanto' },
2203       iu: { name: 'Inuktitut' },
2204       nsk: { name: 'Naskapi' },
2205       oj: { name: 'Ojibwe' },
2206       t: { name: 'Generic' } }
2207   };
2208
2209   function categorize_im ()
2210   {
2211     var cat, lang, list, name;
2212     for (lang in MIM.imlist)
2213       {
2214         list = null;
2215         for (cat in lang_category)
2216           if (lang_category[cat][lang])
2217             {
2218               list = lang_category[cat][lang].list;
2219               if (! list)
2220                 list = lang_category[cat][lang].list = {};
2221               for (name in MIM.imlist[lang])
2222                 list[name] = MIM.imlist[lang][name];
2223             }
2224         if (! list)
2225           for (name in MIM.imlist[lang])
2226             Xex.Log ('no category ' + lang + '-' + name);
2227       }
2228   }
2229
2230   var destroy_timer;
2231   var last_target;
2232
2233   function destroy ()
2234   {
2235     clearTimeout (destroy_timer);
2236     destroy_timer = null;
2237     var target = document.getElementById ('mim-menu');
2238     if (target)
2239       {
2240         for (; last_target && last_target.menu_level;
2241              last_target = last_target.parentLi)
2242           last_target.style.backgroundColor = 'white';
2243         var nodes = target.getElementsByTagName ('ul');
2244         for (var i = 0; i < nodes.length; i++)
2245           nodes[i].style.visibility = 'hidden';
2246         document.getElementsByTagName ('body')[0].removeChild (target);
2247       }
2248   }    
2249
2250   function destroy_menu () {
2251     if (! destroy_timer)
2252       destroy_timer = setTimeout (destroy, 1000);
2253     return true;
2254   }
2255
2256   function show_submenu (event)
2257   {
2258     var target = event.target;
2259     if (! target.menu_level)
2260       {
2261         if (! target.parentNode || ! target.parentNode.menu_level)
2262           return true;
2263         target = target.parentNode;
2264       }
2265     if (destroy_timer)
2266       {
2267         clearTimeout (destroy_timer);
2268         destroy_timer = null;
2269       }
2270     if (last_target && target.parentLi != last_target)
2271       {
2272         last_target.style.backgroundColor = 'white';
2273         if (target.menu_level < last_target.menu_level)
2274           {
2275             last_target = last_target.parentLi;
2276             last_target.style.backgroundColor = 'white';
2277           }
2278         var uls = last_target.getElementsByTagName ('ul');
2279         for (var i = 0; i < uls.length; i++)
2280           uls[i].style.visibility = 'hidden';
2281       }
2282     last_target = target;
2283     target.style.backgroundColor = 'yellow';
2284     if (target.menu_level < 3)
2285       {
2286         target.lastChild.style.visibility = 'visible';
2287         target.lastChild.style.left = target.clientWidth + 'px';
2288       }
2289     event.preventDefault ();    
2290   }
2291
2292   function select_im (event)
2293   {
2294     var target = event.target;
2295     if (! target.im)
2296       {
2297         if (! target.parentNode || ! target.parentNode.menu_level)
2298           {
2299             event.preventDefault ();
2300             return false;
2301           }
2302         target = target.parentNode;
2303       }
2304     if (target.im)
2305       {
2306         MIM.current = target.im;
2307         destroy ();
2308       }
2309     event.preventDefault ();
2310   }
2311
2312   function create_ul (visibility)
2313   {
2314     var ul = document.createElement ('ul');
2315     ul.style.position = 'absolute';
2316     ul.style.margin = '0px';
2317     ul.style.padding = '0px';
2318     ul.style.border = '1px solid gray';
2319     ul.style.borderBottom = 'none';
2320     ul.style.top = '-1px';
2321     ul.style.backgroundColor = 'white';
2322     ul.style.visibility = visibility;
2323     return ul;
2324   }
2325
2326   function create_li (level, text)
2327   {
2328     var li = document.createElement ('li');
2329     li.style.position = 'relative';
2330     li.style.margin = '0px';
2331     li.style.padding = '1px';
2332     li.style.borderBottom = '1px solid gray';
2333     li.style.top = '0px';
2334     li.style.listStyle = 'none';
2335     li.menu_level = level;
2336     var nobr = document.createElement ('nobr');
2337     nobr.innerHTML = text;
2338     li.appendChild (nobr);
2339     return li;
2340   }
2341
2342   var menu;
2343
2344   function create_menu (event)
2345   {
2346     var target = event.target;
2347
2348     if (! ((target.type == "text" || target.type == "textarea")
2349            && event.which == 1 && event.ctrlKey))
2350       return;
2351     if (! menu)
2352       {
2353         categorize_im ();
2354         menu = create_ul ('visible');
2355         menu.style.fontFamily = 'sans-serif';
2356         menu.style.fontWeight = 'bold';
2357         menu.id = 'mim-menu';
2358         menu.onmousedown = select_im;
2359         menu.onmouseover = show_submenu;
2360         menu.onmouseout = destroy_menu;
2361         for (var catname in lang_category)
2362           {
2363             var cat = lang_category[catname];
2364             var li = create_li (1, catname);
2365             var sub = create_ul ('hidden');
2366             for (var langname in cat)
2367               {
2368                 var lang = cat[langname];
2369                 if (! lang.list)
2370                   continue;
2371                 var sub_li = create_li (2, lang.name);
2372                 sub_li.parentLi = li;
2373                 var subsub = create_ul ('hidden');
2374                 for (var name in lang.list)
2375                   {
2376                     var im = lang.list[name];
2377                     var subsub_li = create_li (3, im.name);
2378                     subsub_li.parentLi = sub_li;
2379                     subsub_li.im = im;
2380                     subsub.appendChild (subsub_li);
2381                   }
2382                 sub_li.appendChild (subsub);
2383                 sub.appendChild (sub_li);
2384               }
2385             li.appendChild (sub);
2386             menu.appendChild (li);
2387           }
2388         lang_category = null;
2389       }
2390     menu.style.left = (event.clientX - 10) + "px";
2391     menu.style.top = (event.clientY - 10) + "px";
2392     document.getElementsByTagName ('body')[0].appendChild (menu);
2393   };
2394
2395   MIM.init = function ()
2396   {
2397     MIM.add_event_listener (window, 'keydown', MIM.keydown);
2398     MIM.add_event_listener (window, 'keypress', MIM.keypress);
2399     MIM.add_event_listener (window, 'mousedown', create_menu);
2400     MIM.current = MIM.imlist['t']['latn-post'];
2401   };
2402 }) ();