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