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.
6 // Boolean flag to tell if MIM is running in debug mode or not.
8 // List of main input methods.
10 // List of extra input methods;
12 // Global input method data
14 // Currently selected input method.
18 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
27 Preedit: 0x06, // PreeditText | CursorPos
28 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
50 ParseError: "parse-error"
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";
63 function decode_keysym (str) {
66 var parts = str.split ("-");
67 var len = parts.length, i;
68 var has_modifier = len > 1;
70 for (i = 0; i < len - 1; i++)
71 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
73 var key = parts[len - 1];
76 key = keysyms[key.toLowerCase ()];
82 for (i = 1; i < len - 1; i++)
83 str += '-' + parts[i];
99 MIM.Key = function (val)
102 if (val instanceof Xex.Term)
104 else if (typeof val == 'string' || val instanceof String)
106 this.key = decode_keysym (val);
108 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
109 if (this.key instanceof Array)
111 this.key = this.key[0];
112 this.has_modifier = true;
115 else if (typeof val == 'number' || val instanceof Number)
116 this.key = String.fromCharCode (val);
118 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
121 MIM.Key.prototype.toString = function () { return this.key; };
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'));
129 MIM.KeySeq = function (seq)
131 this.val = new Array ();
137 var len = seq.val.length;
138 for (var i = 0; i < len; i++)
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);
146 throw new Xex.ErrTerm (MIM.Error.ParseError,
147 "Invalid key: " + v);
149 if (key.has_modifier)
150 this.has_modifier = true;
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)));
160 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
164 var proto = new Xex.Term ('keyseq');
165 proto.Clone = function () { return this; }
166 proto.Parser = function (domain, node)
168 var seq = new Array ();
169 for (node = node.firstChild; node; node = node.nextSibling)
170 if (node.nodeType == 1)
172 var term = Xex.Term.Parse (domain, node);
173 return new MIM.KeySeq (term);
175 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
177 proto.toString = function ()
179 var len = this.val.length;
183 var str = '<keyseq>';
184 for (var i = 0; i < len; i++)
188 else if (this.has_modifier)
190 str += this.val[i].toString ();
192 return str + '</keyseq>';
195 MIM.KeySeq.prototype = proto;
199 MIM.Marker = function () { }
200 MIM.Marker.prototype = new Xex.Term ('marker');
201 MIM.Marker.prototype.CharAt = function (ic)
203 var p = this.Position (ic);
204 if (p < 0 || p >= ic.preedit.length)
206 return ic.preedit.charCodeAt (p);
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; };
215 MIM.PredefinedMarker = function (name) { this.val = name; }
216 MIM.PredefinedMarker.prototype = new MIM.Marker ();
217 MIM.PredefinedMarker.prototype.Position = function (ic)
219 if (typeof this.pos == 'number')
221 return this.pos (ic);
226 function def_predefined (name, position)
228 predefined[name] = new MIM.PredefinedMarker (name);
229 predefined[name].pos = position;
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)
239 var pos = ic.cursor_pos;
240 return ic.preedit.FindProp ('candidates', pos - 1).from;
244 def_predefined ('@]', function (ic) {
245 if (ic.cursor_pos < ic.preedit.length - 1)
247 var pos = ic.cursor_pos;
248 return ic.preedit.FindProp ('candidates', pos).to;
250 return ic.preedit.length;
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['@]'];
261 MIM.SurroundMarker = function (name)
264 this.distance = parseInt (name.slice (1));
265 if (isNaN (this.distance))
266 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
268 MIM.SurroundMarker.prototype = new MIM.Marker ();
269 MIM.SurroundMarker.prototype.Position = function (ic)
271 return ic.cursor_pos + this.distance;
273 MIM.SurroundMarker.prototype.CharAt = function (ic)
275 if (this.val == '@-0')
277 var p = this.Position (ic);
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);
285 MIM.Marker.prototype.Parser = function (domain, node)
287 var name = node.firstChild.nodeValue;
288 if (name.charAt (0) == '@')
290 var n = predefined[name];
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);
298 return new MIM.FloatingMarker (name);;
302 MIM.Selector = function (name)
306 MIM.Selector.prototype = new Xex.Term ('selector');
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 ('@]');
318 MIM.Selector.prototype.Parser = function (domain, node)
320 var name = node.firstChild.nodeValue;
321 var s = selectors[name];
323 throw new Xex.ErrTerm (MIM.Error.ParseError,
324 "Invalid selector: " + name);
329 MIM.Rule = function (keyseq, actions)
331 this.keyseq = keyseq;
333 this.actions = actions;
335 MIM.Rule.prototype = new Xex.Term ('rule');
336 MIM.Rule.prototype.Parser = function (domain, node)
339 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
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);
346 n = n.nextElement ();
348 actions = Xex.Term.Parse (domain, n, null);
349 return new MIM.Rule (keyseq, actions);
351 MIM.Rule.prototype.toString = function ()
356 MIM.Map = function (name)
359 this.rules = new Array ();
363 var proto = new Xex.Term ('map');
365 proto.Parser = function (domain, node)
367 var name = node.attributes['mname'].nodeValue;
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)
373 map.rules.push (Xex.Term.Parse (domain, n));
377 proto.toString = function ()
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>';
386 MIM.Map.prototype = proto;
389 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
391 MIM.Action = function (domain, terms)
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);
400 MIM.Action.prototype.Run = function (domain)
402 var result = this.action.Eval (domain);
403 if (result.type == 'error')
405 domain.context.Error = result.toString ();
408 return (result != Xex.CatchTag._mimtag);
411 MIM.Keymap = function ()
420 function add_rule (keymap, rule, branch_actions)
422 var keyseq = rule.keyseq;
423 var len = keyseq.val.length;
426 for (var i = 0; i < len; i++)
428 var key = keyseq.val[i];
432 if (! keymap.submaps)
435 sub = keymap.submaps[key.key];
437 keymap.submaps[key.key] = sub = new MIM.Keymap ();
441 keymap.map_actions = rule.actions;
443 keymap.branch_actions = branch_actions;
446 proto.Add = function (map, branch_actions)
448 var rules = map.rules;
449 var len = rules.length;
451 for (var i = 0; i < len; i++)
452 add_rule (this, rules[i], branch_actions);
454 proto.Lookup = function (keys, index)
458 if (index < keys.val.length && this.submaps
459 && ! keys.val[index])
461 Xex.Log ('invalid key at ' + index);
465 if (index < keys.val.length && this.submaps
466 && (sub = this.submaps[keys.val[index].key]))
469 return sub.Lookup (keys, index);
471 return { map: this, index: index };
474 MIM.Keymap.prototype = proto;
477 MIM.State = function (name)
480 this.keymap = new MIM.Keymap ();
484 var proto = new Xex.Term ('state');
486 proto.Parser = function (domain, node)
488 var map_list = domain.map_list;
489 var name = node.attributes['sname'].nodeValue;
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 ())
495 if (node.nodeName == 'title')
496 state.title = node.firstChild.nodeValue;
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],
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;
513 proto.toString = function ()
515 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
518 MIM.State.prototype = proto;
522 function Block (index, term)
526 this.Data = term.val;
527 else if (term.IsList)
529 this.Data = new Array ();
530 for (var i = 0; i < term.val.length; i++)
531 this.Data.push (term.val[i].val);
535 Block.prototype.Count = function () { return this.Data.length; }
536 Block.prototype.get = function (i)
538 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
541 MIM.Candidates = function (ic, candidates, column)
544 this.column = column;
548 this.blocks = new Array ();
550 for (var i = 0; i < candidates.length; i++)
552 var block = new Block (this.total, candidates[i]);
553 this.blocks.push (block);
554 this.total += block.Count ();
560 return (this.column > 0 ? this.index % this.column
561 : this.index - this.blocks[this.row].Index);
564 function prev_group ()
566 var col = get_col.call (this);
570 this.index -= this.column;
572 nitems = this.column;
575 var lastcol = (this.total - 1) % this.column;
576 this.index = (col < lastcol ? this.total - lastcol + col
578 this.row = this.blocks.length - 1;
579 nitems = lastcol + 1;
581 while (this.blocks[this.row].Index > this.index)
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));
594 function next_group ()
596 var col = get_col.call (this);
600 this.index += this.column - col;
601 if (this.index < this.total)
603 if (this.index + col >= this.total)
605 nitems = this.total - this.index;
606 this.index = this.total - 1;
610 nitems = this.column;
619 while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
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));
637 this.index = this.total - 1;
638 this.row = this.blocks.length - 1;
643 if (this.blocks[this.row].Index > this.index)
651 if (this.index == this.total)
658 var b = this.blocks[this.row];
659 if (this.index == b.Index + b.Count ())
666 this.index -= get_col.call (this);
667 while (this.blocks[this.row].Index > this.index)
673 var b = this.blocks[this.row];
676 if (this.index + 1 < this.total)
678 this.index += this.column - get_col.call (this) + 1;
679 while (b.Index + b.Count () <= this.index)
680 b = this.blocks[++this.row];
684 this.index = b.Index + b.Count () - 1;
687 MIM.Candidates.prototype.Current = function ()
689 var b = this.blocks[this.row];
690 return b.get (this.index - b.Index);
693 MIM.Candidates.prototype.Select = function (selector)
695 var idx = this.index;
696 var gidx = this.column > 0 ? idx / this.column : this.row;
697 if (selector.type == 'selector')
699 switch (selector.val)
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;
715 col = this.index % this.column;
716 start = this.index - col;
717 end = start + this.column;
721 start = this.blocks[this.row].Index;
722 col = this.index - start;
723 end = start + this.blocks[this.row].Count;
725 if (end > this.total)
727 this.index += selector.val - col;
728 if (this.index >= end)
729 this.index = end - 1;
732 if (selector.val > col)
733 while (this.blocks[this.row].Index + this.blocks[this.row].Count
737 while (this.blocks[this.row].Index > this.index)
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 ();
749 MIM.Candidates.prototype.CurrentCol = function ()
751 return get_col.call (this);
754 MIM.Candidates.prototype.CurrentGroup = function ()
756 var col, start, end, gnum, gidx;
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)
769 gnum = this.blocks.length;
771 start = this.blocks[this.row].Index;
772 col = this.index - start;
773 end = start + this.blocks[this.row].Count ();
775 var group = new Array ();
776 var indices = new Array (gnum, gidx, col);
777 group.push (indices);
779 var block = this.blocks[row++];
782 var c = block.get (start - block.Index);
785 if (start == block.Index + block.Count ())
786 block = this.blocks[row++];
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);
801 var im_domain = MIM.im_domain;
803 function Finsert (domain, vari, args)
806 if (args[0].type == 'integer')
807 text = String.fromCharCode (args[0].val);
810 domain.context.ins (text, null);
814 function Finsert_candidates (domain, vari, args)
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);
824 function Fdelete (domain, vari, args)
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));
831 function Fselect (domain, vari, args)
833 var ic = domain.context;
834 var can = ic.candidates;
838 var old_text = can.Current ();
839 var new_text = can.Select (args[0]);
840 ic.rep (old_text, new_text, can);
843 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
847 function Fshow (domain, vari, args)
849 domain.context.candidate_show = true;
850 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
854 function Fhide (domain, vari, args)
856 domain.context.candidate_show = false;
857 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
861 function Fchar_at (domain, vari, args)
863 return new Xex.IntTerm (args[0].CharAt (domain.context));
866 function Fmove (domain, vari, args)
868 var ic = domain.context;
869 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
871 return new Xex.IntTerm (pos);
874 function Fmark (domain, vari, args)
876 args[0].Mark (domain.context);
880 function Fpushback (domain, vari, args)
882 var a = (args[0].IsInt ? args[0].Intval ()
883 : args[0].IsStr ? new KeySeq (args[0])
885 domain.context.pushback (a);
889 function Fpop (domain, vari, args)
891 var ic = domain.context;
892 if (ic.key_head < ic.keys.val.length)
893 ic.keys.val.splice (ic.keys_head, 1);
897 function Fundo (domain, vari, args)
899 var ic = domain.context;
900 var n = args.length == 0 ? -2 : args[0].val;
901 Xex.Log ('undo with arg ' + args[0]);
903 ic.keys.val.splice (ic.keys.val.length + n, -n);
905 ic.keys.val.splice (n, ic.keys.val.length);
910 function Fcommit (domain, vari, args)
912 domain.context.commit ();
916 function Funhandle (domain, vari, args)
918 domain.context.commit ();
919 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
922 function Fshift (domain, vari, args)
924 var ic = domain.context;
925 var state_name = args[0].val;
926 var state = ic.im.state_list[state_name];
928 throw ("Unknown state: " + state_name);
933 function Fshiftback (domain, vari, args)
935 domain.context.shift (null);
939 function Fkey_count (domain, vari, args)
941 return new Xex.IntTerm (domain.context.key_head);
944 function Fsurrounding_flag (domain, vari, args)
946 return new Xex.IntTerm (-1);
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);
971 function get_global_var (vname)
973 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
974 MIM.im_global.Load ()
975 return MIM.im_global.domain.variables[vname];
978 function include (node)
980 node = node.firstElement ();
981 if (node.nodeName != 'tags')
984 var lang = null, name = null, extra = null;
985 for (node = node.firstElement (); node; node = node.nextElement ())
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;
994 if (! lang || ! MIM.imlist[lang])
998 if (! name || ! (im = MIM.imlist[lang][name]))
1003 if (! (im = MIM.imextra[lang][extra]))
1006 if (im.load_status != MIM.LoadStatus.Loaded
1007 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
1014 parsers['description'] = function (node)
1016 this.description = node.firstChild.nodeValue;
1018 parsers['variable-list'] = function (node)
1020 for (node = node.firstElement (); node; node = node.nextElement ())
1022 var vname = node.attributes['vname'].nodeValue;
1023 if (this != MIM.im_global)
1025 var vari = get_global_var (vname);
1027 this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
1029 vname = Xex.Term.Parse (this.domain, node)
1032 parsers['command-list'] = function (node)
1035 parsers['macro-list'] = function (node)
1037 for (var n = node.firstElement (); n; n = n.nextElement ())
1038 if (n.nodeName == 'xi:include')
1040 var im = include (n);
1042 alert ('inclusion fail');
1044 for (var macro in im.domain.functions)
1046 var func = im.domain.functions[macro];
1047 if (func instanceof Xex.Macro)
1048 im.domain.CopyFunc (this.domain, macro);
1050 n = n.previousSibling;
1051 node.removeChild (n.nextSibling);
1053 Xex.Term.Parse (this.domain, node.firstElement (), null);
1055 parsers['title'] = function (node)
1057 this.title = node.firstChild.nodeValue;
1059 parsers['map-list'] = function (node)
1061 for (node = node.firstElement (); node; node = node.nextElement ())
1063 if (node.nodeName == 'xi:include')
1065 var im = include (node);
1068 alert ('inclusion fail');
1071 for (var mname in im.map_list)
1072 this.map_list[mname] = im.map_list[mname];
1076 var map = Xex.Term.Parse (this.domain, node);
1077 this.map_list[map.name] = map;
1081 parsers['state-list'] = function (node)
1083 this.domain.map_list = this.map_list;
1084 for (node = node.firstElement (); node; node = node.nextElement ())
1086 if (node.nodeName == 'xi:include')
1088 var im = include (node);
1090 alert ('inclusion fail');
1091 for (var sname in im.state_list)
1093 state = im.state_list[sname];
1094 if (! this.initial_state)
1095 this.initial_state = state;
1096 this.state_list[sname] = state;
1099 else if (node.nodeName == 'state')
1101 var state = Xex.Term.Parse (this.domain, node);
1103 state.title = this.title;
1104 if (! this.initial_state)
1105 this.initial_state = state;
1106 this.state_list[state.name] = state;
1109 delete this.domain.map_list;
1112 MIM.IM = function (lang, name, extra_id, file)
1116 this.extra_id = extra_id;
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);
1128 var node = Xex.Load (null, this.file);
1131 this.load_status = MIM.LoadStatus.Error;
1135 this.initial_state = null;
1136 this.state_list = {};
1137 for (node = node.firstElement (); node; node = node.nextElement ())
1139 var name = node.nodeName;
1140 var parser = parsers[name];
1142 parser.call (this, node);
1144 this.load_status = MIM.LoadStatus.Loaded;
1149 MIM.IM.prototype = proto;
1151 MIM.IC = function (im, target)
1153 if (im.load_status == MIM.LoadStatus.NotLoaded)
1155 if (im.load_status != MIM.LoadStatus.Loaded)
1156 alert ('im:' + im.name + ' error:' + im.load_status);
1158 this.target = target;
1159 this.domain = new Xex.Domain ('context', im.domain, this);
1161 this.range = new Array ();
1162 this.range[0] = this.range[1] = 0;
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 ();
1171 MIM.CandidateTable = function ()
1173 this.table = new Array ();
1176 MIM.CandidateTable.prototype.get = function (pos)
1178 for (var i = 0; i < this.table.length; i++)
1180 var elt = this.table[i];
1181 if (elt.from < pos && pos <= elt.to)
1186 MIM.CandidateTable.prototype.put = function (from, to, candidates)
1188 for (var i = 0; i < this.table.length; i++)
1190 var elt = this.table[i];
1191 if (elt.from < to && elt.to > from)
1195 elt.val = candidates;
1199 this.table.push ({ from: from, to: to, val: candidates });
1202 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
1204 var diff = inserted - (to - from);
1207 for (var i = 0; i < this.table.length; i++)
1209 var elt = this.table[i];
1218 MIM.CandidateTable.prototype.clear = function ()
1220 this.table.length = 0;
1223 function set_cursor (prefix, pos)
1225 this.cursor_pos = pos;
1226 var candidates = this.candidate_table.get (pos);
1227 if (this.candidates != candidates)
1229 this.candidates = candidates;
1230 this.changed |= MIM.ChangedStatus.CandidateList;
1234 function save_state ()
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;
1242 function restore_state ()
1244 this.domain.RestoreValues (this.state_var_values);
1245 this.preedit = this.state_preedit;
1246 set_cursor.call (this, "restore", this.state_pos);
1249 function handle_key ()
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);
1258 if (out.index > this.key_head)
1260 this.key_head = out.index;
1261 Xex.Log (' with submap', false, true);
1262 restore_state.call (this);
1264 if (sub.map_actions)
1266 Xex.Log ('taking map actions:');
1267 if (! this.take_actions (sub.map_actions))
1270 else if (sub.submaps)
1272 Xex.Log ('no map actions');
1273 for (var i = this.state_key_head; i < this.key_head; i++)
1275 Xex.Log ('inserting key:' + this.keys.val[i].key);
1276 this.ins (this.keys.val[i].key, null);
1281 Xex.Log ('terminal:');
1282 if (this.keymap.branch_actions)
1284 Xex.Log ('branch actions:');
1285 if (! this.take_actions (this.keymap.branch_actions))
1288 if (sub != this.state.keymap)
1289 this.shift (this.state);
1294 Xex.Log (' without submap', false, true);
1296 var current_state = this.state;
1297 var map = this.keymap;
1299 if (map.branch_actions)
1301 Xex.Log ('branch actions:');
1302 if (! this.take_actions (map.branch_actions))
1306 if (map == this.keymap)
1308 Xex.Log ('no state change');
1309 if (map == this.initial_state.keymap
1310 && this.key_head < this.keys.val.length)
1312 Xex.Log ('unhandled');
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);
1325 reset: function (clear_keys)
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 = {};
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;
1345 this.preedit_saved = '';
1346 if (this.candidate_show)
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);
1356 catch_args: new Array (Xex.CatchTag._mimtag, null),
1358 take_actions: function (actions)
1360 if (actions.length == 0)
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');
1370 GetSurroundingChar: function (pos)
1374 pos += this.range[0];
1380 pos += this.range[1];
1381 if (pos >= this.target.value.length)
1384 return this.target.value.charCodeAt (pos);
1387 DelSurroundText: function (pos)
1392 pos += this.range[0];
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;
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;
1412 text += this.target.value.substring (pos);
1413 this.target.value = text;
1417 adjust_markers: function (from, to, inserted)
1419 var diff = inserted - (to - from);
1421 for (var name in this.marker_positions)
1423 var pos = this.marker_positions[name];
1427 this.marker_positions[name] += diff;
1428 else if (pos > from)
1429 this.marker_positions[name] = from;
1434 preedit_replace: function (from, to, text, candidates)
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);
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)
1450 ins: function (text, candidates)
1452 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
1455 rep: function (old_text, new_text, candidates)
1457 this.preedit_replace (this.cursor_pos - old_text.length,
1458 this.cursor_pos, new_text, candidates);
1463 var deleted = pos - this.cursor_pos;
1464 if (pos < this.cursor_pos)
1468 this.DelSurroundText (pos);
1469 deleted = - this.cursor_pos;
1472 if (pos < this.cursor_pos)
1473 this.preedit_replace (pos, this.cursor_pos, '', null);
1477 if (pos > this.preedit.length)
1479 this.DelSurroundText (pos - this.preedit.length);
1480 deleted = this.preedit.length - this.cursor_pos;
1481 pos = this.preedit.length;
1483 if (pos > this.cursor_pos)
1484 this.preedit_replace (this.cursor_pos, pos, '', null);
1491 this.candidate_show = true;
1492 this.changed |= MIM.ChangedStatus.CandidateShow;
1497 this.candidate_show = false;
1498 this.changed |= MIM.ChangedStatus.CandidateShow;
1501 move: function (pos)
1505 else if (pos > this.preedit.length)
1506 pos = this.preedit.length;
1507 if (pos != this.cursor_pos)
1509 set_cursor.call (this, 'move', pos);
1510 this.changed |= MIM.ChangedStatus.Preedit;
1514 pushback: function (n)
1516 if (n instanceof MIM.KeySeq)
1518 if (this.key_head > 0)
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]);
1530 if (this.key_head < 0)
1537 this.key_head = - n;
1538 if (this.key_head > this.keys.val.length)
1539 this.key_head = this.keys.val.length;
1545 if (this.key_head < this.keys.val.length)
1546 this.keys.val.splice (this.key_head, 1);
1551 if (this.preedit.length > 0)
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 = '';
1558 this.commit_key_head = this.key_head;
1562 shift: function (state)
1566 if (this.prev_state == null)
1568 state = this.prev_state;
1571 if (state == this.initial_state)
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;
1583 if (state != this.state)
1584 this.prev_state = this.state;
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;
1591 this.keymap = state.keymap;
1592 save_state.call (this);
1595 Filter: function (key)
1599 Xex.Log ("active = false");
1600 this.key_unhandled = true;
1601 this.unhandled_key = key;
1604 if (key.key == '_reload')
1606 this.changed = MIM.ChangedStatus.None;
1608 this.key_unhandled = false;
1609 this.keys.val.push (key);
1611 while (this.key_head < this.keys.val.length)
1613 if (! handle_key.call (this))
1615 if (this.key_head < this.keys.val.length)
1617 this.unhandled_key = this.keys.val[this.key_head];
1618 this.keys.val.splice (this.key_head, this.key_head + 1);
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;
1630 this.key_unhandled = true;
1634 if (this.keymap == this.initial_state.keymap)
1637 if (this.commit_key_head > 0)
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;
1644 if (this.key_unhandled)
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;
1650 if (this.changed & MIM.ChangedStatus.Candidate)
1652 if (this.candidate_show && this.candidates)
1657 return (! this.key_unhandled
1658 && this.produced.length == 0);
1662 MIM.IC.prototype = proto;
1664 var node = Xex.Load (null, "imlist.xml");
1665 for (node = node.firstChild; node; node = node.nextSibling)
1666 if (node.nodeName == 'input-method')
1668 var lang = null, name = null, extra_id = null, file = null;
1670 for (var n = node.firstChild; n; n = n.nextSibling)
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;
1681 if (name && name != 'nil')
1683 if (! MIM.imlist[lang])
1684 MIM.imlist[lang] = {};
1685 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
1687 else if (extra_id && extra_id != 'nil')
1689 if (! MIM.imextra[lang])
1690 MIM.imextra[lang] = {};
1691 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
1694 if (MIM.imextra.t && MIM.imextra.t.global)
1695 MIM.im_global = MIM.imextra.t.global;
1698 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
1699 MIM.im_global.load_status = MIM.LoadStatus.Error;
1705 var keys = new Array ();
1707 keys[0x08] = 'backspace';
1708 keys[0x0D] = 'return';
1709 keys[0x1B] = 'escape';
1710 keys[0x20] = 'space';
1711 keys[0x21] = 'pageup';
1712 keys[0x22] = 'pagedown';
1714 keys[0x24] = 'home';
1715 keys[0x25] = 'left';
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";
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';
1735 modifiers.Shift = 1;
1736 modifiers.Control = 1;
1738 modifiers.AltGraph = 1;
1741 MIM.decode_key_event = function (event)
1743 var key = event.keyIdentifier;
1745 if (key) // keydown event of Chrome
1750 if (event.ctrlKey) mod += 'C-';
1751 if (event.metaKey) mod += 'M-';
1752 if (event.altKey) mod += 'A-';
1753 var keysym = keyids[key];
1756 else if (key.match(/^U\+([0-9A-Z]+)$/))
1758 if (mod.length == 0)
1760 key = String.fromCharCode (parseInt (RegExp.$1, 16));
1763 //key = key.toLowerCase ();
1764 if (event.shiftKey) mod += 'S-';
1765 return new MIM.Key (mod + key);
1769 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
1770 : event.charCode ? event.charCode
1774 if (event.type == 'keydown')
1779 if (event.shiftKey) key = "S-" + key ;
1782 key = String.fromCharCode (key);
1784 if (event.altKey) key = "A-" + key ;
1785 if (event.ctrlKey) key = "C-" + key ;
1786 return new MIM.Key (key);
1790 MIM.add_event_listener
1791 = (window.addEventListener
1792 ? function (target, type, listener) {
1793 target.addEventListener (type, listener, false);
1795 : window.attachEvent
1796 ? function (target, type, listener) {
1797 target.attachEvent ('on' + type,
1799 listener.call (target, window.event);
1802 : function (target, type, listener) {
1804 = function (e) { listener.call (target, e || window.event); };
1807 MIM.debug_print = function () { };
1809 MIM.get_range = function (target, ic)
1812 if (target.selectionStart != null) // for Mozilla
1814 from = target.selectionStart;
1815 to = target.selectionEnd;
1819 var r = document.selection.createRange ();
1820 var rr = r.duplicate ();
1822 rr.moveToElementText (target);
1823 rr.setEndPoint ('EndToEnd', range);
1824 from = rr.text.length - r.text.length;
1825 to = rr.text.length;
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])))
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' };
1862 function copy_style (from, to)
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;
1873 MIM.get_preedit_pos = function (target, ic)
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);
1887 copy_style (target, temp);
1890 for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
1892 ic.abs_top += elm.offsetTop;
1893 ic.abs_left += elm.offsetLeft;
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;
1907 MIM.update_bar = function (target, ic)
1909 if (ic.preedit.length > 0)
1911 MIM.get_preedit_pos (target, ic);
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);
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';
1926 ic.bar.style.display = 'none'
1929 MIM.update = function (target, ic, for_focus_out)
1931 var text = target.value;
1932 target.value = (text.substring (0, ic.range[0])
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)
1941 var pos = ic.range[0] + ic.cursor_pos;
1942 if (target.setSelectionRange) // Mozilla
1944 var scrollTop = target.scrollTop;
1945 target.setSelectionRange (pos, pos);
1946 target.scrollTop = scrollTop;
1950 var range = target.createTextRange ();
1951 range.moveStart ('character', pos);
1952 range.moveEnd ('character', pos);
1959 MIM.show = function (ic)
1961 if (! ic.candidates)
1963 var target = ic.target;
1964 MIM.get_preedit_pos (target, ic);
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);
1975 if (ic.changed & MIM.ChangedStatus.CandidateList)
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++)
1988 var td = document.createElement ('td');
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);
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';
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');
2006 ic.can_node.style.display = 'block';
2009 MIM.hide = function (ic)
2012 ic.can_node.style.display = 'none';
2015 function real_focus_in (target, ic)
2017 if (MIM.get_range (target, ic))
2018 ic.Filter (MIM.Key.FocusIn);
2019 MIM.update (target, ic, false);
2024 MIM.focus_in = function (event)
2026 var target = event.target;
2027 Xex.Log ("Focus in " + target.tagName);
2029 = setTimeout (function () {real_focus_in (target, target.mim_ic)} , 10);
2033 MIM.click = function (event)
2035 var target = event.target;
2036 Xex.Log ("Click in " + target.tagName);
2039 clearTimeout (focus_in_timer);
2040 focus_in_timer = null;
2042 real_focus_in (target, target.mim_ic);
2047 MIM.focus_out = function (event)
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);
2059 MIM.keydown = function (event)
2061 var target = event.target;
2062 if (! (target.type == "text" || target.type == "textarea"))
2065 var ic = target.mim_ic;
2066 if (! ic || ic.im != MIM.current)
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)
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);
2078 MIM.get_range (target, ic)
2079 MIM.debug_print (event, ic);
2080 ic.key = MIM.decode_key_event (event);
2084 var result = ic.Filter (ic.key);
2086 Xex.Log ('Error' + e);
2089 MIM.update (target, ic, false);
2090 if (! ic.key_unhandled)
2091 event.preventDefault ();
2095 MIM.keypress = function (event)
2097 var target = event.target;
2098 if (! (target.type == "text" || target.type == "textarea"))
2101 var ic = target.mim_ic;
2105 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2108 ic.key = MIM.decode_key_event (event);
2116 var result = ic.Filter (ic.key);
2118 Xex.Log ('Error:' + e);
2121 MIM.update (target, ic, false);
2122 if (! ic.key_unhandled)
2123 event.preventDefault ();
2125 Xex.Log ("error:" + e);
2126 event.preventDefault ();
2128 MIM.debug_print (event, ic);
2135 var lang_category = {
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' } },
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' } },
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' } },
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' } },
2190 ii: { name: 'Yii' },
2191 ja: { name: 'Japanese' },
2192 ko: { name: 'Korean' },
2193 zh: { name: 'Chinese' } },
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' } }
2206 function categorize_im ()
2208 var cat, lang, list, name;
2209 for (lang in MIM.imlist)
2212 for (cat in lang_category)
2213 if (lang_category[cat][lang])
2215 list = lang_category[cat][lang].list;
2217 list = lang_category[cat][lang].list = {};
2218 for (name in MIM.imlist[lang])
2219 list[name] = MIM.imlist[lang][name];
2222 for (name in MIM.imlist[lang])
2223 Xex.Log ('no category ' + lang + '-' + name);
2232 clearTimeout (destroy_timer);
2233 destroy_timer = null;
2234 var target = document.getElementById ('mim-menu');
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);
2247 function destroy_menu () {
2248 if (! destroy_timer)
2249 destroy_timer = setTimeout (destroy, 1000);
2253 function show_submenu (event)
2255 var target = event.target;
2256 if (! target.menu_level)
2258 if (! target.parentNode || ! target.parentNode.menu_level)
2260 target = target.parentNode;
2264 clearTimeout (destroy_timer);
2265 destroy_timer = null;
2267 if (last_target && target.parentLi != last_target)
2269 last_target.style.backgroundColor = 'white';
2270 if (target.menu_level < last_target.menu_level)
2272 last_target = last_target.parentLi;
2273 last_target.style.backgroundColor = 'white';
2275 var uls = last_target.getElementsByTagName ('ul');
2276 for (var i = 0; i < uls.length; i++)
2277 uls[i].style.visibility = 'hidden';
2279 last_target = target;
2280 target.style.backgroundColor = 'yellow';
2281 if (target.menu_level < 3)
2283 target.lastChild.style.visibility = 'visible';
2284 target.lastChild.style.left = target.clientWidth + 'px';
2286 event.preventDefault ();
2289 function select_im (event)
2291 var target = event.target;
2294 if (! target.parentNode || ! target.parentNode.menu_level)
2296 event.preventDefault ();
2299 target = target.parentNode;
2303 MIM.current = target.im;
2306 event.preventDefault ();
2309 function create_ul (visibility)
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;
2323 function create_li (level, text)
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);
2341 function create_menu (event)
2343 var target = event.target;
2345 if (! ((target.type == "text" || target.type == "textarea")
2346 && event.which == 1 && event.ctrlKey))
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)
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)
2365 var lang = cat[langname];
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)
2373 var im = lang.list[name];
2374 var subsub_li = create_li (3, im.name);
2375 subsub_li.parentLi = sub_li;
2377 subsub.appendChild (subsub_li);
2379 sub_li.appendChild (subsub);
2380 sub.appendChild (sub_li);
2382 li.appendChild (sub);
2383 menu.appendChild (li);
2385 lang_category = null;
2387 menu.style.left = (event.clientX - 10) + "px";
2388 menu.style.top = (event.clientY - 10) + "px";
2389 document.getElementsByTagName ('body')[0].appendChild (menu);
2392 MIM.init = function ()
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'];