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