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"
54 if (window.location == 'http://localhost/mim/index.html')
55 MIM.server = 'http://localhost/mim';
58 var keysyms = new Array ();
59 keysyms["bs"] = "backspace";
60 keysyms["lf"] = "linefeed";
61 keysyms["cr"] = keysyms["enter"] = "return";
62 keysyms["esc"] = "escape";
63 keysyms["spc"] = "space";
64 keysyms["del"] = "delete";
66 function decode_keysym (str) {
69 var parts = str.split ("-");
70 var len = parts.length, i;
71 var has_modifier = len > 1;
73 for (i = 0; i < len - 1; i++)
74 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
76 var key = parts[len - 1];
79 key = keysyms[key.toLowerCase ()];
85 for (i = 1; i < len - 1; i++)
86 str += '-' + parts[i];
102 MIM.Key = function (val)
105 if (val instanceof Xex.Term)
107 else if (typeof val == 'string' || val instanceof String)
109 this.key = decode_keysym (val);
111 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
112 if (this.key instanceof Array)
114 this.key = this.key[0];
115 this.has_modifier = true;
118 else if (typeof val == 'number' || val instanceof Number)
119 this.key = String.fromCharCode (val);
121 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
124 MIM.Key.prototype.toString = function () { return this.key; };
126 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
127 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
128 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
132 MIM.KeySeq = function (seq)
134 this.val = new Array ();
140 var len = seq.val.length;
141 for (var i = 0; i < len; i++)
143 var v = seq.val[i], key;
144 if (v.type == 'symbol' || v.type == 'string')
145 key = new MIM.Key (v);
146 else if (v.type == 'integer')
147 key = new MIM.Key (v.val);
149 throw new Xex.ErrTerm (MIM.Error.ParseError,
150 "Invalid key: " + v);
152 if (key.has_modifier)
153 this.has_modifier = true;
158 var len = seq.val.length;
159 for (var i = 0; i < len; i++)
160 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
163 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
167 var proto = new Xex.Term ('keyseq');
168 proto.Clone = function () { return this; }
169 proto.Parser = function (domain, node)
171 var seq = new Array ();
172 for (node = node.firstChild; node; node = node.nextSibling)
173 if (node.nodeType == 1)
175 var term = Xex.Term.Parse (domain, node);
176 return new MIM.KeySeq (term);
178 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
180 proto.toString = function ()
182 var len = this.val.length;
186 var str = '<keyseq>';
187 for (var i = 0; i < len; i++)
191 else if (this.has_modifier)
193 str += this.val[i].toString ();
195 return str + '</keyseq>';
198 MIM.KeySeq.prototype = proto;
202 MIM.Marker = function () { }
203 MIM.Marker.prototype = new Xex.Term ('marker');
204 MIM.Marker.prototype.CharAt = function (ic)
206 var p = this.Position (ic);
207 if (p < 0 || p >= ic.preedit.length)
209 return ic.preedit.charCodeAt (p);
212 MIM.FloatingMarker = function (name) { this.val = name; };
213 var proto = new MIM.Marker ();
214 MIM.FloatingMarker.prototype = proto;
215 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
216 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
218 MIM.PredefinedMarker = function (name) { this.val = name; }
219 MIM.PredefinedMarker.prototype = new MIM.Marker ();
220 MIM.PredefinedMarker.prototype.Position = function (ic)
222 if (typeof this.pos == 'number')
224 return this.pos (ic);
229 function def_predefined (name, position)
231 predefined[name] = new MIM.PredefinedMarker (name);
232 predefined[name].pos = position;
235 def_predefined ('@<', 0);
236 def_predefined ('@>', function (ic) { return ic.preedit.length; });
237 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
238 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
239 def_predefined ('@[', function (ic) {
240 if (ic.cursor_pos > 0)
242 var pos = ic.cursor_pos;
243 return ic.preedit.FindProp ('candidates', pos - 1).from;
247 def_predefined ('@]', function (ic) {
248 if (ic.cursor_pos < ic.preedit.length - 1)
250 var pos = ic.cursor_pos;
251 return ic.preedit.FindProp ('candidates', pos).to;
253 return ic.preedit.length;
255 for (var i = 0; i < 10; i++)
256 def_predefined ("@" + i, i);
257 predefined['@first'] = predefined['@<'];
258 predefined['@last'] = predefined['@>'];
259 predefined['@previous'] = predefined['@-'];
260 predefined['@next'] = predefined['@+'];
261 predefined['@previous-candidate-change'] = predefined['@['];
262 predefined['@next-candidate-change'] = predefined['@]'];
264 MIM.SurroundMarker = function (name)
267 this.distance = parseInt (name.slice (1));
268 if (isNaN (this.distance))
269 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
271 MIM.SurroundMarker.prototype = new MIM.Marker ();
272 MIM.SurroundMarker.prototype.Position = function (ic)
274 return ic.cursor_pos + this.distance;
276 MIM.SurroundMarker.prototype.CharAt = function (ic)
278 if (this.val == '@-0')
280 var p = this.Position (ic);
282 return ic.GetSurroundingChar (p);
283 else if (p >= ic.preedit.length)
284 return ic.GetSurroundingChar (p - ic.preedit.length);
285 return ic.preedit.charCodeAt (p);
288 MIM.Marker.prototype.Parser = function (domain, node)
290 var name = node.firstChild.nodeValue;
291 if (name.charAt (0) == '@')
293 var n = predefined[name];
296 if (name.charAt (1) == '-' || name.charAt (1) == '+')
297 return new MIM.SurroundMarker (name);
298 throw new Xex.ErrTerm (MIM.Error.ParseError,
299 "Invalid marker: " + name);
301 return new MIM.FloatingMarker (name);;
305 MIM.Selector = function (name)
309 MIM.Selector.prototype = new Xex.Term ('selector');
313 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
314 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
315 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
316 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
317 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
318 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
319 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
321 MIM.Selector.prototype.Parser = function (domain, node)
323 var name = node.firstChild.nodeValue;
324 var s = selectors[name];
326 throw new Xex.ErrTerm (MIM.Error.ParseError,
327 "Invalid selector: " + name);
332 MIM.Rule = function (keyseq, actions)
334 this.keyseq = keyseq;
336 this.actions = actions;
338 MIM.Rule.prototype = new Xex.Term ('rule');
339 MIM.Rule.prototype.Parser = function (domain, node)
342 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
344 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
345 var keyseq = Xex.Term.Parse (domain, n);
346 if (keyseq.type != 'keyseq')
347 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
349 n = n.nextElement ();
351 actions = Xex.Term.Parse (domain, n, null);
352 return new MIM.Rule (keyseq, actions);
354 MIM.Rule.prototype.toString = function ()
359 MIM.Map = function (name)
362 this.rules = new Array ();
366 var proto = new Xex.Term ('map');
368 proto.Parser = function (domain, node)
370 var name = node.attributes['mname'].nodeValue;
372 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
373 var map = new MIM.Map (name);
374 for (var n = node.firstChild; n; n = n.nextSibling)
376 map.rules.push (Xex.Term.Parse (domain, n));
380 proto.toString = function ()
382 var str = '<map mname="' + this.name + '">';
383 var len = this.rules.length;
384 for (i = 0; i < len; i++)
385 str += this.rules[i];
386 return str + '</map>';
389 MIM.Map.prototype = proto;
392 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
394 MIM.Action = function (domain, terms)
396 var args = new Array ();
397 args.push (Xex.CatchTag_.mimtag);
398 for (var i = 0; i < terms.length; i++)
399 args.push (terms[i]);
400 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
403 MIM.Action.prototype.Run = function (domain)
405 var result = this.action.Eval (domain);
406 if (result.type == 'error')
408 domain.context.Error = result.toString ();
411 return (result != Xex.CatchTag._mimtag);
414 MIM.Keymap = function ()
423 function add_rule (keymap, rule, branch_actions)
425 var keyseq = rule.keyseq;
426 var len = keyseq.val.length;
429 for (var i = 0; i < len; i++)
431 var key = keyseq.val[i];
435 if (! keymap.submaps)
438 sub = keymap.submaps[key.key];
440 keymap.submaps[key.key] = sub = new MIM.Keymap ();
444 keymap.map_actions = rule.actions;
446 keymap.branch_actions = branch_actions;
449 proto.Add = function (map, branch_actions)
451 var rules = map.rules;
452 var len = rules.length;
454 for (var i = 0; i < len; i++)
455 add_rule (this, rules[i], branch_actions);
457 proto.Lookup = function (keys, index)
461 if (index < keys.val.length && this.submaps
462 && ! keys.val[index])
464 Xex.Log ('invalid key at ' + index);
468 if (index < keys.val.length && this.submaps
469 && (sub = this.submaps[keys.val[index].key]))
472 return sub.Lookup (keys, index);
474 return { map: this, index: index };
477 MIM.Keymap.prototype = proto;
480 MIM.State = function (name)
483 this.keymap = new MIM.Keymap ();
487 var proto = new Xex.Term ('state');
489 proto.Parser = function (domain, node)
491 var map_list = domain.map_list;
492 var name = node.attributes['sname'].nodeValue;
494 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
495 var state = new MIM.State (name);
496 for (node = node.firstElement (); node; node = node.nextElement ())
498 if (node.nodeName == 'title')
499 state.title = node.firstChild.nodeValue;
502 var n = node.firstElement ();
503 var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
504 if (node.nodeName == 'branch')
505 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
507 else if (node.nodeName == 'state-hook')
508 state.enter_actions = branch_actions;
509 else if (node.nodeName == 'catch-all-branch')
510 state.fallback_actions = branch_actions;
516 proto.toString = function ()
518 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
521 MIM.State.prototype = proto;
525 function Block (index, term)
529 this.Data = term.val;
530 else if (term.IsList)
532 this.Data = new Array ();
533 for (var i = 0; i < term.val.length; i++)
534 this.Data.push (term.val[i].val);
538 Block.prototype.Count = function () { return this.Data.length; }
539 Block.prototype.get = function (i)
541 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
544 MIM.Candidates = function (ic, candidates, column)
547 this.column = column;
551 this.blocks = new Array ();
553 for (var i = 0; i < candidates.length; i++)
555 var block = new Block (this.total, candidates[i]);
556 this.blocks.push (block);
557 this.total += block.Count ();
563 return (this.column > 0 ? this.index % this.column
564 : this.index - this.blocks[this.row].Index);
567 function prev_group ()
569 var col = get_col.call (this);
573 this.index -= this.column;
575 nitems = this.column;
578 var lastcol = (this.total - 1) % this.column;
579 this.index = (col < lastcol ? this.total - lastcol + col
581 this.row = this.blocks.length - 1;
582 nitems = lastcol + 1;
584 while (this.blocks[this.row].Index > this.index)
589 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
590 nitems = this.blocks[this.row].Count ();
591 this.index = (this.blocks[this.row].Index
592 + (col < nitems ? col : nitems - 1));
597 function next_group ()
599 var col = get_col.call (this);
603 this.index += this.column - col;
604 if (this.index < this.total)
606 if (this.index + col >= this.total)
608 nitems = this.total - this.index;
609 this.index = this.total - 1;
613 nitems = this.column;
622 while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
628 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
629 nitems = this.blocks[this.row].Count ();
630 this.index = (this.blocks[this.row].Index
631 + (col < nitems ? col : nitems - 1));
640 this.index = this.total - 1;
641 this.row = this.blocks.length - 1;
646 if (this.blocks[this.row].Index > this.index)
654 if (this.index == this.total)
661 var b = this.blocks[this.row];
662 if (this.index == b.Index + b.Count ())
669 this.index -= get_col.call (this);
670 while (this.blocks[this.row].Index > this.index)
676 var b = this.blocks[this.row];
679 if (this.index + 1 < this.total)
681 this.index += this.column - get_col.call (this) + 1;
682 while (b.Index + b.Count () <= this.index)
683 b = this.blocks[++this.row];
687 this.index = b.Index + b.Count () - 1;
690 MIM.Candidates.prototype.Current = function ()
692 var b = this.blocks[this.row];
693 return b.get (this.index - b.Index);
696 MIM.Candidates.prototype.Select = function (selector)
698 var idx = this.index;
699 var gidx = this.column > 0 ? idx / this.column : this.row;
700 if (selector.type == 'selector')
702 switch (selector.val)
704 case '@<': first.call (this); break;
705 case '@>': last.call (this); break;
706 case '@-': prev.call (this); break;
707 case '@+': next.call (this); break;
708 case '@[': prev_group.call (this); break;
709 case '@]': next_group.call (this); break;
718 col = this.index % this.column;
719 start = this.index - col;
720 end = start + this.column;
724 start = this.blocks[this.row].Index;
725 col = this.index - start;
726 end = start + this.blocks[this.row].Count;
728 if (end > this.total)
730 this.index += selector.val - col;
731 if (this.index >= end)
732 this.index = end - 1;
735 if (selector.val > col)
736 while (this.blocks[this.row].Index + this.blocks[this.row].Count
740 while (this.blocks[this.row].Index > this.index)
744 var newgidx = this.column > 0 ? this.index / this.column : this.row;
745 if (this.index != idx)
746 this.ic.changed |= (gidx == newgidx
747 ? MIM.ChangedStatus.CandidateIndex
748 : MIM.ChangedStatus.CandidateList);
749 return this.Current ();
752 MIM.Candidates.prototype.CurrentCol = function ()
754 return get_col.call (this);
757 MIM.Candidates.prototype.CurrentGroup = function ()
759 var col, start, end, gnum, gidx;
762 gnum = Math.floor ((this.total - 1) / this.column) + 1;
763 col = this.index % this.column;
764 start = this.index - col;
765 gidx = start / this.column + 1;
766 end = start + this.column;
767 if (end > this.total)
772 gnum = this.blocks.length;
774 start = this.blocks[this.row].Index;
775 col = this.index - start;
776 end = start + this.blocks[this.row].Count ();
778 var group = new Array ();
779 var indices = new Array (gnum, gidx, col);
780 group.push (indices);
782 var block = this.blocks[row++];
785 var c = block.get (start - block.Index);
788 if (start == block.Index + block.Count ())
789 block = this.blocks[row++];
795 MIM.im_domain = new Xex.Domain ('input-method', null, null);
796 MIM.im_domain.DefType (MIM.KeySeq.prototype);
797 MIM.im_domain.DefType (MIM.Marker.prototype);
798 MIM.im_domain.DefType (MIM.Selector.prototype);
799 MIM.im_domain.DefType (MIM.Rule.prototype);
800 MIM.im_domain.DefType (MIM.Map.prototype);
801 MIM.im_domain.DefType (MIM.State.prototype);
804 var im_domain = MIM.im_domain;
806 function Finsert (domain, vari, args)
809 if (args[0].type == 'integer')
810 text = String.fromCharCode (args[0].val);
813 domain.context.ins (text, null);
817 function Finsert_candidates (domain, vari, args)
819 var ic = domain.context;
820 var gsize = domain.variables['candidates-group-size'];
821 var candidates = new MIM.Candidates (ic, args,
822 gsize ? gsize.val.Intval () : 0);
823 ic.ins (candidates.Current (), candidates);
827 function Fdelete (domain, vari, args)
829 var ic = domain.context;
830 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
831 return new Xex.IntTerm (ic.del (pos));
834 function Fselect (domain, vari, args)
836 var ic = domain.context;
837 var can = ic.candidates;
841 var old_text = can.Current ();
842 var new_text = can.Select (args[0]);
843 ic.rep (old_text, new_text, can);
846 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
850 function Fshow (domain, vari, args)
852 domain.context.candidate_show = true;
853 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
857 function Fhide (domain, vari, args)
859 domain.context.candidate_show = false;
860 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
864 function Fchar_at (domain, vari, args)
866 return new Xex.IntTerm (args[0].CharAt (domain.context));
869 function Fmove (domain, vari, args)
871 var ic = domain.context;
872 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
874 return new Xex.IntTerm (pos);
877 function Fmark (domain, vari, args)
879 args[0].Mark (domain.context);
883 function Fpushback (domain, vari, args)
885 var a = (args[0].IsInt ? args[0].Intval ()
886 : args[0].IsStr ? new KeySeq (args[0])
888 domain.context.pushback (a);
892 function Fpop (domain, vari, args)
894 var ic = domain.context;
895 if (ic.key_head < ic.keys.val.length)
896 ic.keys.val.splice (ic.keys_head, 1);
900 function Fundo (domain, vari, args)
902 var ic = domain.context;
903 var n = args.length == 0 ? -2 : args[0].val;
904 Xex.Log ('undo with arg ' + args[0]);
906 ic.keys.val.splice (ic.keys.val.length + n, -n);
908 ic.keys.val.splice (n, ic.keys.val.length);
913 function Fcommit (domain, vari, args)
915 domain.context.commit ();
919 function Funhandle (domain, vari, args)
921 domain.context.commit ();
922 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
925 function Fshift (domain, vari, args)
927 var ic = domain.context;
928 var state_name = args[0].val;
929 var state = ic.im.state_list[state_name];
931 throw ("Unknown state: " + state_name);
936 function Fshiftback (domain, vari, args)
938 domain.context.shift (null);
942 function Fkey_count (domain, vari, args)
944 return new Xex.IntTerm (domain.context.key_head);
947 function Fsurrounding_flag (domain, vari, args)
949 return new Xex.IntTerm (-1);
952 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
953 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
954 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
955 im_domain.DefSubr (Fselect, "select", false, 1, 1);
956 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
957 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
958 im_domain.DefSubr (Fmove, "move", false, 1, 1);
959 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
960 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
961 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
962 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
963 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
964 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
965 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
966 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
967 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
968 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
969 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
974 function get_global_var (vname)
976 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
977 MIM.im_global.Load ()
978 return MIM.im_global.domain.variables[vname];
981 function include (node)
983 node = node.firstElement ();
984 if (node.nodeName != 'tags')
987 var lang = null, name = null, extra = null;
988 for (node = node.firstElement (); node; node = node.nextElement ())
990 if (node.nodeName == 'language')
991 lang = node.firstChild.nodeValue;
992 else if (node.nodeName == 'name')
993 name = node.firstChild.nodeValue;
994 else if (node.nodeName == 'extra-id')
995 extra = node.firstChild.nodeValue;
997 if (! lang || ! MIM.imlist[lang])
1001 if (! name || ! (im = MIM.imlist[lang][name]))
1006 if (! (im = MIM.imextra[lang][extra]))
1009 if (im.load_status != MIM.LoadStatus.Loaded
1010 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
1017 parsers['description'] = function (node)
1019 this.description = node.firstChild.nodeValue;
1021 parsers['variable-list'] = function (node)
1023 for (node = node.firstElement (); node; node = node.nextElement ())
1025 var vname = node.attributes['vname'].nodeValue;
1026 if (this != MIM.im_global)
1028 var vari = get_global_var (vname);
1030 this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
1032 vname = Xex.Term.Parse (this.domain, node)
1035 parsers['command-list'] = function (node)
1038 parsers['macro-list'] = function (node)
1040 for (var n = node.firstElement (); n; n = n.nextElement ())
1041 if (n.nodeName == 'xi:include')
1043 var im = include (n);
1045 alert ('inclusion fail');
1047 for (var macro in im.domain.functions)
1049 var func = im.domain.functions[macro];
1050 if (func instanceof Xex.Macro)
1051 im.domain.CopyFunc (this.domain, macro);
1053 n = n.previousSibling;
1054 node.removeChild (n.nextSibling);
1056 Xex.Term.Parse (this.domain, node.firstElement (), null);
1058 parsers['title'] = function (node)
1060 this.title = node.firstChild.nodeValue;
1062 parsers['map-list'] = function (node)
1064 for (node = node.firstElement (); node; node = node.nextElement ())
1066 if (node.nodeName == 'xi:include')
1068 var im = include (node);
1071 alert ('inclusion fail');
1074 for (var mname in im.map_list)
1075 this.map_list[mname] = im.map_list[mname];
1079 var map = Xex.Term.Parse (this.domain, node);
1080 this.map_list[map.name] = map;
1084 parsers['state-list'] = function (node)
1086 this.domain.map_list = this.map_list;
1087 for (node = node.firstElement (); node; node = node.nextElement ())
1089 if (node.nodeName == 'xi:include')
1091 var im = include (node);
1093 alert ('inclusion fail');
1094 for (var sname in im.state_list)
1096 state = im.state_list[sname];
1097 if (! this.initial_state)
1098 this.initial_state = state;
1099 this.state_list[sname] = state;
1102 else if (node.nodeName == 'state')
1104 var state = Xex.Term.Parse (this.domain, node);
1106 state.title = this.title;
1107 if (! this.initial_state)
1108 this.initial_state = state;
1109 this.state_list[state.name] = state;
1112 delete this.domain.map_list;
1115 MIM.IM = function (lang, name, extra_id, file)
1119 this.extra_id = extra_id;
1121 this.load_status = MIM.LoadStatus.NotLoaded;
1122 this.domain = new Xex.Domain (this.lang + '-'
1123 + (this.name != 'nil'
1124 ? this.name : this.extra_id),
1125 MIM.im_domain, null);
1131 var node = Xex.Load (MIM.server, this.file);
1134 this.load_status = MIM.LoadStatus.Error;
1138 this.initial_state = null;
1139 this.state_list = {};
1140 for (node = node.firstElement (); node; node = node.nextElement ())
1142 var name = node.nodeName;
1143 var parser = parsers[name];
1145 parser.call (this, node);
1147 this.load_status = MIM.LoadStatus.Loaded;
1152 MIM.IM.prototype = proto;
1154 MIM.IC = function (im, target)
1156 if (im.load_status == MIM.LoadStatus.NotLoaded)
1158 if (im.load_status != MIM.LoadStatus.Loaded)
1159 alert ('im:' + im.name + ' error:' + im.load_status);
1161 this.target = target;
1162 this.domain = new Xex.Domain ('context', im.domain, this);
1164 this.range = new Array ();
1165 this.range[0] = this.range[1] = 0;
1167 this.initial_state = this.im.initial_state;
1168 this.keys = new MIM.KeySeq ();
1169 this.marker_positions = new Array ();
1170 this.candidate_table = new MIM.CandidateTable ();
1174 MIM.CandidateTable = function ()
1176 this.table = new Array ();
1179 MIM.CandidateTable.prototype.get = function (pos)
1181 for (var i = 0; i < this.table.length; i++)
1183 var elt = this.table[i];
1184 if (elt.from < pos && pos <= elt.to)
1189 MIM.CandidateTable.prototype.put = function (from, to, candidates)
1191 for (var i = 0; i < this.table.length; i++)
1193 var elt = this.table[i];
1194 if (elt.from < to && elt.to > from)
1198 elt.val = candidates;
1202 this.table.push ({ from: from, to: to, val: candidates });
1205 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
1207 var diff = inserted - (to - from);
1210 for (var i = 0; i < this.table.length; i++)
1212 var elt = this.table[i];
1221 MIM.CandidateTable.prototype.clear = function ()
1223 this.table.length = 0;
1226 function set_cursor (prefix, pos)
1228 this.cursor_pos = pos;
1229 var candidates = this.candidate_table.get (pos);
1230 if (this.candidates != candidates)
1232 this.candidates = candidates;
1233 this.changed |= MIM.ChangedStatus.CandidateList;
1237 function save_state ()
1239 this.state_var_values = this.domain.SaveValues ();
1240 this.state_preedit = this.preedit;
1241 this.state_key_head = this.key_head;
1242 this.state_pos = this.cursor_pos;
1245 function restore_state ()
1247 this.domain.RestoreValues (this.state_var_values);
1248 this.preedit = this.state_preedit;
1249 set_cursor.call (this, "restore", this.state_pos);
1252 function handle_key ()
1254 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
1255 + '" in ' + this.state.name + ':' + this.keymap.name
1256 + " key/state/commit-head/len:"
1257 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
1258 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
1261 if (out.index > this.key_head)
1263 this.key_head = out.index;
1264 Xex.Log (' with submap', false, true);
1265 restore_state.call (this);
1267 if (sub.map_actions)
1269 Xex.Log ('taking map actions:');
1270 if (! this.take_actions (sub.map_actions))
1273 else if (sub.submaps)
1275 Xex.Log ('no map actions');
1276 for (var i = this.state_key_head; i < this.key_head; i++)
1278 Xex.Log ('inserting key:' + this.keys.val[i].key);
1279 this.ins (this.keys.val[i].key, null);
1284 Xex.Log ('terminal:');
1285 if (this.keymap.branch_actions)
1287 Xex.Log ('branch actions:');
1288 if (! this.take_actions (this.keymap.branch_actions))
1291 if (sub != this.state.keymap)
1292 this.shift (this.state);
1297 Xex.Log (' without submap', false, true);
1299 var current_state = this.state;
1300 var map = this.keymap;
1302 if (map.branch_actions)
1304 Xex.Log ('branch actions:');
1305 if (! this.take_actions (map.branch_actions))
1309 if (map == this.keymap)
1311 Xex.Log ('no state change');
1312 if (map == this.initial_state.keymap
1313 && this.key_head < this.keys.val.length)
1315 Xex.Log ('unhandled');
1318 if (this.keymap != current_state.keymap)
1319 this.shift (current_state);
1320 else if (this.keymap.actions == null)
1321 this.shift (this.initial_state);
1328 reset: function (clear_keys)
1330 this.cursor_pos = 0;
1331 this.prev_state = null;
1332 this.title = this.initial_state.title;
1333 this.state_preedit = '';
1334 this.state_key_head = 0;
1335 this.state_var_values = {};
1339 this.keys.val.length = 0;
1340 this.commit_key_head = 0;
1341 this.key_unhandled = false;
1342 this.unhandled_key = null;
1343 this.changed = MIM.ChangedStatus.None;
1344 this.error_message = '';
1345 this.title = this.initial_state.title;
1348 this.preedit_saved = '';
1349 if (this.candidate_show)
1351 this.candidate_table.clear ();
1352 this.candidates = null;
1353 this.candidate_show = false;
1354 for (var elt in this.marker_positions)
1355 this.marker_positions[elt] = 0;
1356 this.shift (this.initial_state);
1359 catch_args: new Array (Xex.CatchTag._mimtag, null),
1361 take_actions: function (actions)
1363 if (actions.length == 0)
1365 var func_progn = this.domain.GetFunc ('progn');
1366 var func_catch = this.domain.GetFunc ('catch');
1367 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
1368 var term = new Xex.Funcall (func_catch, null, this.catch_args);
1369 term = term.Eval (this.domain);
1370 return (! term.IsSymbol || term.val != '@mimtag');
1373 GetSurroundingChar: function (pos)
1377 pos += this.range[0];
1383 pos += this.range[1];
1384 if (pos >= this.target.value.length)
1387 return this.target.value.charCodeAt (pos);
1390 DelSurroundText: function (pos)
1395 pos += this.range[0];
1401 text = this.target.value.substring (0, pos);
1402 if (this.range[0] < this.target.value.length)
1403 text += this.target.value.substring (this.range[0]);
1404 this.target.value = text;
1405 this.range[1] -= this.range[0] - pos;
1406 this.range[0] = pos;
1410 pos += this.range[1];
1411 text = this.target.value.substring (0, this.range[1]);
1412 if (pos >= this.target.value.length)
1413 pos = this.target.value.length;
1415 text += this.target.value.substring (pos);
1416 this.target.value = text;
1420 adjust_markers: function (from, to, inserted)
1422 var diff = inserted - (to - from);
1424 for (var name in this.marker_positions)
1426 var pos = this.marker_positions[name];
1430 this.marker_positions[name] += diff;
1431 else if (pos > from)
1432 this.marker_positions[name] = from;
1437 preedit_replace: function (from, to, text, candidates)
1439 var newlen = text.length;
1440 this.preedit = (this.preedit.substring (0, from)
1441 + text + this.preedit.substring (to));
1442 this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
1443 this.adjust_markers (from, to, newlen);
1444 this.candidate_table.adjust (from, to, newlen);
1446 this.candidate_table.put (from, from + newlen, candidates)
1447 if (this.cursor_pos >= to)
1448 set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
1449 else if (this.cursor_pos > from)
1450 set_cursor.call (this, 'adjust', from)
1453 ins: function (text, candidates)
1455 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
1458 rep: function (old_text, new_text, candidates)
1460 this.preedit_replace (this.cursor_pos - old_text.length,
1461 this.cursor_pos, new_text, candidates);
1466 var deleted = pos - this.cursor_pos;
1467 if (pos < this.cursor_pos)
1471 this.DelSurroundText (pos);
1472 deleted = - this.cursor_pos;
1475 if (pos < this.cursor_pos)
1476 this.preedit_replace (pos, this.cursor_pos, '', null);
1480 if (pos > this.preedit.length)
1482 this.DelSurroundText (pos - this.preedit.length);
1483 deleted = this.preedit.length - this.cursor_pos;
1484 pos = this.preedit.length;
1486 if (pos > this.cursor_pos)
1487 this.preedit_replace (this.cursor_pos, pos, '', null);
1494 this.candidate_show = true;
1495 this.changed |= MIM.ChangedStatus.CandidateShow;
1500 this.candidate_show = false;
1501 this.changed |= MIM.ChangedStatus.CandidateShow;
1504 move: function (pos)
1508 else if (pos > this.preedit.length)
1509 pos = this.preedit.length;
1510 if (pos != this.cursor_pos)
1512 set_cursor.call (this, 'move', pos);
1513 this.changed |= MIM.ChangedStatus.Preedit;
1517 pushback: function (n)
1519 if (n instanceof MIM.KeySeq)
1521 if (this.key_head > 0)
1523 if (this.key_head < this.keys.val.length)
1524 this.keys.val.splice (this.key_head,
1525 this.keys.val.length - this.key_head);
1526 for (var i = 0; i < n.val.length; i++)
1527 this.keys.val.push (n.val[i]);
1533 if (this.key_head < 0)
1540 this.key_head = - n;
1541 if (this.key_head > this.keys.val.length)
1542 this.key_head = this.keys.val.length;
1548 if (this.key_head < this.keys.val.length)
1549 this.keys.val.splice (this.key_head, 1);
1554 if (this.preedit.length > 0)
1556 this.candidate_table.clear ();
1557 this.produced += this.preedit;
1558 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
1559 this.preedit_saved = '';
1561 this.commit_key_head = this.key_head;
1565 shift: function (state)
1569 if (this.prev_state == null)
1571 state = this.prev_state;
1574 if (state == this.initial_state)
1579 this.keys.val.splice (0, this.key_head);
1580 this.key_head = this.state_key_head = this.commit_key_head = 0;
1581 this.prev_state = null;
1586 if (state != this.state)
1587 this.prev_state = this.state;
1589 if (state != this.state && state.enter_actions)
1590 this.take_actions (state.enter_actions);
1591 if (! this.state || this.state.title != state.title)
1592 this.changed |= MIM.ChangedStatus.StateTitle;
1594 this.keymap = state.keymap;
1595 save_state.call (this);
1598 Filter: function (key)
1602 Xex.Log ("active = false");
1603 this.key_unhandled = true;
1604 this.unhandled_key = key;
1607 if (key.key == '_reload')
1609 this.changed = MIM.ChangedStatus.None;
1611 this.key_unhandled = false;
1612 this.keys.val.push (key);
1614 while (this.key_head < this.keys.val.length)
1616 if (! handle_key.call (this))
1618 if (this.key_head < this.keys.val.length)
1620 this.unhandled_key = this.keys.val[this.key_head];
1621 this.keys.val.splice (this.key_head, this.key_head + 1);
1623 if (this.state_key_head > 0)
1624 this.state_key_head--;
1625 if (this.commit_key_head > 0)
1626 this.commit_key_head--;
1627 this.key_unhandled = true;
1633 this.key_unhandled = true;
1637 if (this.keymap == this.initial_state.keymap)
1640 if (this.commit_key_head > 0)
1642 this.keys.val.splice (0, this.commit_key_head);
1643 this.key_head -= this.commit_key_head;
1644 this.state_key_head -= this.commit_key_head;
1645 this.commit_key_head = 0;
1647 if (this.key_unhandled)
1649 this.keys.val.length = 0;
1650 //this.keys.val.splice (0, this.keys.val.length);
1651 this.key_head = this.state_key_head = this.commit_key_head = 0;
1653 if (this.changed & MIM.ChangedStatus.Candidate)
1655 if (this.candidate_show && this.candidates)
1660 return (! this.key_unhandled
1661 && this.produced.length == 0);
1665 MIM.IC.prototype = proto;
1667 var node = Xex.Load (MIM.server, "imlist.xml");
1668 for (node = node.firstChild; node; node = node.nextSibling)
1669 if (node.nodeName == 'input-method')
1671 var lang = null, name = null, extra_id = null, file = null;
1673 for (var n = node.firstChild; n; n = n.nextSibling)
1675 if (n.nodeName == 'language')
1676 lang = n.firstChild.nodeValue;
1677 else if (n.nodeName == 'name')
1678 name = n.firstChild.nodeValue;
1679 else if (n.nodeName == 'extra-id')
1680 extra_id = n.firstChild.nodeValue;
1681 else if (n.nodeName == 'filename')
1682 file = n.firstChild.nodeValue;
1684 if (name && name != 'nil')
1686 if (! MIM.imlist[lang])
1687 MIM.imlist[lang] = {};
1688 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
1690 else if (extra_id && extra_id != 'nil')
1692 if (! MIM.imextra[lang])
1693 MIM.imextra[lang] = {};
1694 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
1697 if (MIM.imextra.t && MIM.imextra.t.global)
1698 MIM.im_global = MIM.imextra.t.global;
1701 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
1702 MIM.im_global.load_status = MIM.LoadStatus.Error;
1708 var keys = new Array ();
1710 keys[0x08] = 'backspace';
1711 keys[0x0D] = 'return';
1712 keys[0x1B] = 'escape';
1713 keys[0x20] = 'space';
1714 keys[0x21] = 'pageup';
1715 keys[0x22] = 'pagedown';
1717 keys[0x24] = 'home';
1718 keys[0x25] = 'left';
1720 keys[0x27] = 'right';
1721 keys[0x28] = 'down';
1722 keys[0x2D] = 'insert';
1723 keys[0x2E] = 'delete';
1724 for (var i = 1; i <= 12; i++)
1725 keys[111 + i] = "f" + i;
1726 keys[0x90] = "numlock";
1727 keys[0xF0] = "capslock";
1730 keyids['U+0008'] = 'Backspace';
1731 keyids['U+0009'] = 'Tab';
1732 keyids['U+0018'] = 'Cancel';
1733 keyids['U+001B'] = 'Escape';
1734 keyids['U+0020'] = 'Space';
1735 keyids['U+007F'] = 'Delete';
1738 modifiers.Shift = 1;
1739 modifiers.Control = 1;
1741 modifiers.AltGraph = 1;
1744 MIM.decode_key_event = function (event)
1746 var key = event.keyIdentifier;
1748 if (key) // keydown event of Chrome
1753 if (event.ctrlKey) mod += 'C-';
1754 if (event.metaKey) mod += 'M-';
1755 if (event.altKey) mod += 'A-';
1756 var keysym = keyids[key];
1759 else if (key.match(/^U\+([0-9A-Z]+)$/))
1761 if (mod.length == 0)
1763 key = String.fromCharCode (parseInt (RegExp.$1, 16));
1766 //key = key.toLowerCase ();
1767 if (event.shiftKey) mod += 'S-';
1768 return new MIM.Key (mod + key);
1772 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
1773 : event.charCode ? event.charCode
1777 if (event.type == 'keydown')
1782 if (event.shiftKey) key = "S-" + key ;
1785 key = String.fromCharCode (key);
1787 if (event.altKey) key = "A-" + key ;
1788 if (event.ctrlKey) key = "C-" + key ;
1789 return new MIM.Key (key);
1793 MIM.add_event_listener
1794 = (window.addEventListener
1795 ? function (target, type, listener) {
1796 target.addEventListener (type, listener, false);
1798 : window.attachEvent
1799 ? function (target, type, listener) {
1800 target.attachEvent ('on' + type,
1802 listener.call (target, window.event);
1805 : function (target, type, listener) {
1807 = function (e) { listener.call (target, e || window.event); };
1810 MIM.debug_print = function () { };
1812 MIM.get_range = function (target, ic)
1815 if (target.selectionStart != null) // for Mozilla
1817 from = target.selectionStart;
1818 to = target.selectionEnd;
1822 var r = document.selection.createRange ();
1823 var rr = r.duplicate ();
1825 rr.moveToElementText (target);
1826 rr.setEndPoint ('EndToEnd', range);
1827 from = rr.text.length - r.text.length;
1828 to = rr.text.length;
1831 && from == ic.range[0] + ic.cursor_pos
1832 && (ic.preedit.length == 0
1833 || ic.preedit == target.value.substring (ic.range[0], ic.range[1])))
1847 padingLeft: 'padding-left',
1848 paddingRight: 'padding-right',
1849 paddingTop: 'padding-top',
1850 paddintBottom: 'padding-bottom',
1851 borderLeftStyle: 'border-left-style',
1852 borderRightStyle: 'border-right-style',
1853 borderTopStyle: 'border-top-style',
1854 borderBottomStyle: 'border-bottom-style',
1855 borderLeftWidth: 'border-left-width',
1856 borderRightWidth: 'border-right-width',
1857 borderTopWidth: 'border-top-width',
1858 borderBottomWidth: 'border-bottom-width',
1859 fontFamily: 'font-family',
1860 fontSize: 'font-size',
1861 lineHeight: 'line-height',
1862 letterSpacing: 'letter-spacing',
1863 wordSpacing: 'word-spacing' };
1865 function copy_style (from, to)
1867 var from_style = getComputedStyle(from,'');
1868 for(var name in style_props)
1869 to.style[name] = from_style.getPropertyValue (style_props[name]);
1870 to.style.left = from.offsetLeft + 'px';
1871 to.style.top = from.offsetTop + 'px';
1872 to.style.width = from.offsetWidth;
1873 to.style.height = from.offsetHeight;
1876 MIM.get_preedit_pos = function (target, ic)
1880 temp = document.createElement ('div');
1881 temp.style.visibility = 'hidden';
1882 temp.style.position = 'absolute';
1883 temp.appendChild (document.createElement ('span'));
1884 temp.appendChild (document.createElement ('span'));
1885 document.getElementsByTagName ('body')[0].appendChild (temp);
1890 copy_style (target, temp);
1893 for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
1895 ic.abs_top += elm.offsetTop;
1896 ic.abs_left += elm.offsetLeft;
1899 temp.firstChild.innerText = target.value.substring (0, ic.range[0]);
1900 temp.lastChild.innerText
1901 = (ic.range[0] == ic.range[1] ? "|"
1902 : target.value.substring (ic.range[0], ic.range[1]));
1903 ic.abs_y = (ic.abs_top + temp.lastChild.offsetTop
1904 + temp.lastChild.offsetHeight - target.scrollTop);
1905 ic.abs_x0 = ic.abs_left + temp.lastChild.offsetLeft;
1906 ic.abs_x1 = ic.abs_x0 + temp.lastChild.offsetWidth;
1910 MIM.update_bar = function (target, ic)
1912 if (ic.preedit.length > 0)
1914 MIM.get_preedit_pos (target, ic);
1917 ic.bar = document.createElement ('div');
1918 ic.bar.style.position = 'absolute';
1919 ic.bar.style.backgroundColor = "black";
1920 ic.bar.style.minHeight = '1px';
1921 document.getElementsByTagName ('body')[0].appendChild (ic.bar);
1923 ic.bar.style.display = 'block'
1924 ic.bar.style.top = (ic.abs_y + 1) + 'px';
1925 ic.bar.style.left = ic.abs_x0 + 'px';
1926 ic.bar.style.minWidth = ic.bar.style.maxWidth = (ic.abs_x1 - ic.abs_x0) + 'px';
1929 ic.bar.style.display = 'none'
1932 MIM.update = function (target, ic, for_focus_out)
1934 var text = target.value;
1935 target.value = (text.substring (0, ic.range[0])
1938 + text.substring (ic.range[1]));
1939 ic.range[0] += ic.produced.length;
1940 ic.range[1] = ic.range[0] + ic.preedit.length;
1941 MIM.update_bar (target, ic);
1942 if (! for_focus_out)
1944 var pos = ic.range[0] + ic.cursor_pos;
1945 if (target.setSelectionRange) // Mozilla
1947 var scrollTop = target.scrollTop;
1948 target.setSelectionRange (pos, pos);
1949 target.scrollTop = scrollTop;
1953 var range = target.createTextRange ();
1954 range.moveStart ('character', pos);
1955 range.moveEnd ('character', pos);
1962 MIM.show = function (ic)
1964 if (! ic.candidates)
1966 var target = ic.target;
1967 MIM.get_preedit_pos (target, ic);
1970 ic.can_node = document.createElement ('table');
1971 ic.can_node.style.position = 'absolute';
1972 ic.can_node.style.display = 'none';
1973 ic.can_node.style.backgroundColor = "white";
1974 ic.can_node.style.border = "1px solid black";
1975 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
1978 if (ic.changed & MIM.ChangedStatus.CandidateList)
1980 while (ic.can_node.childNodes.length > 0)
1981 ic.can_node.removeChild (ic.can_node.firstChild);
1982 var tr = document.createElement ('tr');
1983 var group = ic.candidates.CurrentGroup ();
1984 var td = document.createElement ('td');
1985 td.innerHTML = group[0][1] + '/' + group[0][0];
1986 td.style.color = 'white';
1987 td.style.backgroundColor = 'black';
1988 tr.appendChild (td);
1989 for (var i = 1; i < group.length; i++)
1991 var td = document.createElement ('td');
1993 td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
1994 if (i == group[0][2] + 1)
1995 td.style.backgroundColor = 'lightblue';
1996 tr.appendChild (td);
1998 ic.can_node.appendChild (tr);
1999 ic.can_node.style.top = (ic.abs_y + 10) + 'px';
2000 ic.can_node.style.left = ic.abs_x0 + 'px';
2004 var td = ic.can_node.firstElement ().firstElement ().nextElement ();
2005 var col = ic.candidates.CurrentCol ();
2006 for (var i = 0; td; td = td.nextElement ())
2007 td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
2009 ic.can_node.style.display = 'block';
2012 MIM.hide = function (ic)
2015 ic.can_node.style.display = 'none';
2018 function real_focus_in (target, ic)
2020 if (MIM.get_range (target, ic))
2021 ic.Filter (MIM.Key.FocusIn);
2022 MIM.update (target, ic, false);
2027 MIM.focus_in = function (event)
2029 var target = event.target;
2030 Xex.Log ("Focus in " + target.tagName);
2032 = setTimeout (function () {real_focus_in (target, target.mim_ic)} , 10);
2036 MIM.click = function (event)
2038 var target = event.target;
2039 Xex.Log ("Click in " + target.tagName);
2042 clearTimeout (focus_in_timer);
2043 focus_in_timer = null;
2045 real_focus_in (target, target.mim_ic);
2050 MIM.focus_out = function (event)
2052 var target = event.target;
2053 var ic = target.mim_ic;
2054 Xex.Log ("Focus out " + target.tagName);
2055 MIM.get_range (target, ic);
2056 MIM.debug_print (event, ic);
2057 ic.Filter (MIM.Key.FocusOut);
2058 MIM.update (target, ic, true);
2062 MIM.keydown = function (event)
2064 var target = event.target;
2065 if (! (target.type == "text" || target.type == "textarea"))
2068 var ic = target.mim_ic;
2069 if (! ic || ic.im != MIM.current)
2071 target.mim_ic = null;
2072 Xex.Log ('creating IC');
2073 ic = new MIM.IC (MIM.current, target);
2074 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2077 MIM.add_event_listener (target, 'focus', MIM.focus_in);
2078 MIM.add_event_listener (target, 'blur', MIM.focus_out);
2079 MIM.add_event_listener (target, 'click', MIM.click);
2081 MIM.get_range (target, ic)
2082 MIM.debug_print (event, ic);
2083 ic.key = MIM.decode_key_event (event);
2087 var result = ic.Filter (ic.key);
2089 Xex.Log ('Error' + e);
2092 MIM.update (target, ic, false);
2093 if (! ic.key_unhandled)
2094 event.preventDefault ();
2098 MIM.keypress = function (event)
2100 var target = event.target;
2101 if (! (target.type == "text" || target.type == "textarea"))
2104 var ic = target.mim_ic;
2108 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2111 ic.key = MIM.decode_key_event (event);
2119 var result = ic.Filter (ic.key);
2121 Xex.Log ('Error:' + e);
2124 MIM.update (target, ic, false);
2125 if (! ic.key_unhandled)
2126 event.preventDefault ();
2128 Xex.Log ("error:" + e);
2129 event.preventDefault ();
2131 MIM.debug_print (event, ic);
2138 var lang_category = {
2140 cs: { name: 'Czech' },
2141 da: { name: 'Danish' },
2142 el: { name: 'Greek' },
2143 en: { name: 'English' },
2144 eo: { name: 'Esperanto' },
2145 fr: { name: 'French' },
2146 grc: { name: 'ClassicGreek' },
2147 hr: { name: 'Croatian' },
2148 hy: { name: 'Armenian' },
2149 ka: { name: 'Georgian' },
2150 kk: { name: 'Kazakh' },
2151 ru: { name: 'Russian' },
2152 sk: { name: 'Slovak' },
2153 sr: { name: 'Serbian' },
2154 sv: { name: 'Swedish' },
2155 yi: { name: 'Yiddish' } },
2157 ar: { name: 'Arabic' },
2158 dv: { name: 'Divehi' },
2159 fa: { name: 'Persian' },
2160 he: { name: 'Hebrew' },
2161 kk: { name: 'Kazakh' },
2162 ps: { name: 'Pushto' },
2163 ug: { name: 'Uighur' },
2164 yi: { name: 'Yiddish' } },
2166 as: { name: 'Assamese' },
2167 bn: { name: 'Bengali' },
2168 bo: { name: 'Tibetan' },
2169 gu: { name: 'Gujarati' },
2170 hi: { name: 'Hindi' },
2171 kn: { name: 'Kannada' },
2172 ks: { name: 'Kashmiri' },
2173 ml: { name: 'Malayalam' },
2174 mr: { name: 'Marathi' },
2175 ne: { name: 'Nepali' },
2176 or: { name: 'Oriya' },
2177 pa: { name: 'Panjabi' },
2178 sa: { name: 'Sanskirit' },
2179 sd: { name: 'Sindhi' },
2180 si: { name: 'Sinhalese' },
2181 ta: { name: 'Tamil' },
2182 te: { name: 'Telugu' },
2183 ur: { name: 'Urdu' } },
2185 cmc: { name: 'Cham' },
2186 km: { name: 'Khmer'},
2187 lo: { name: 'Lao' },
2188 my: { name: 'Burmese' },
2189 tai: { name: 'Tai Viet' },
2190 th: { name: 'Thai' },
2191 vi: { name: 'Vietanamese' } },
2193 ii: { name: 'Yii' },
2194 ja: { name: 'Japanese' },
2195 ko: { name: 'Korean' },
2196 zh: { name: 'Chinese' } },
2198 am: { name: 'Amharic' },
2199 ath: { name: 'Carrier' },
2200 bla: { name: 'Blackfoot' },
2201 cr: { name: 'Cree' },
2202 eo: { name: 'Esperanto' },
2203 iu: { name: 'Inuktitut' },
2204 nsk: { name: 'Naskapi' },
2205 oj: { name: 'Ojibwe' },
2206 t: { name: 'Generic' } }
2209 function categorize_im ()
2211 var cat, lang, list, name;
2212 for (lang in MIM.imlist)
2215 for (cat in lang_category)
2216 if (lang_category[cat][lang])
2218 list = lang_category[cat][lang].list;
2220 list = lang_category[cat][lang].list = {};
2221 for (name in MIM.imlist[lang])
2222 list[name] = MIM.imlist[lang][name];
2225 for (name in MIM.imlist[lang])
2226 Xex.Log ('no category ' + lang + '-' + name);
2235 clearTimeout (destroy_timer);
2236 destroy_timer = null;
2237 var target = document.getElementById ('mim-menu');
2240 for (; last_target && last_target.menu_level;
2241 last_target = last_target.parentLi)
2242 last_target.style.backgroundColor = 'white';
2243 var nodes = target.getElementsByTagName ('ul');
2244 for (var i = 0; i < nodes.length; i++)
2245 nodes[i].style.visibility = 'hidden';
2246 document.getElementsByTagName ('body')[0].removeChild (target);
2250 function destroy_menu () {
2251 if (! destroy_timer)
2252 destroy_timer = setTimeout (destroy, 1000);
2256 function show_submenu (event)
2258 var target = event.target;
2259 if (! target.menu_level)
2261 if (! target.parentNode || ! target.parentNode.menu_level)
2263 target = target.parentNode;
2267 clearTimeout (destroy_timer);
2268 destroy_timer = null;
2270 if (last_target && target.parentLi != last_target)
2272 last_target.style.backgroundColor = 'white';
2273 if (target.menu_level < last_target.menu_level)
2275 last_target = last_target.parentLi;
2276 last_target.style.backgroundColor = 'white';
2278 var uls = last_target.getElementsByTagName ('ul');
2279 for (var i = 0; i < uls.length; i++)
2280 uls[i].style.visibility = 'hidden';
2282 last_target = target;
2283 target.style.backgroundColor = 'yellow';
2284 if (target.menu_level < 3)
2286 target.lastChild.style.visibility = 'visible';
2287 target.lastChild.style.left = target.clientWidth + 'px';
2289 event.preventDefault ();
2292 function select_im (event)
2294 var target = event.target;
2297 if (! target.parentNode || ! target.parentNode.menu_level)
2299 event.preventDefault ();
2302 target = target.parentNode;
2306 MIM.current = target.im;
2309 event.preventDefault ();
2312 function create_ul (visibility)
2314 var ul = document.createElement ('ul');
2315 ul.style.position = 'absolute';
2316 ul.style.margin = '0px';
2317 ul.style.padding = '0px';
2318 ul.style.border = '1px solid gray';
2319 ul.style.borderBottom = 'none';
2320 ul.style.top = '-1px';
2321 ul.style.backgroundColor = 'white';
2322 ul.style.visibility = visibility;
2326 function create_li (level, text)
2328 var li = document.createElement ('li');
2329 li.style.position = 'relative';
2330 li.style.margin = '0px';
2331 li.style.padding = '1px';
2332 li.style.borderBottom = '1px solid gray';
2333 li.style.top = '0px';
2334 li.style.listStyle = 'none';
2335 li.menu_level = level;
2336 var nobr = document.createElement ('nobr');
2337 nobr.innerHTML = text;
2338 li.appendChild (nobr);
2344 function create_menu (event)
2346 var target = event.target;
2348 if (! ((target.type == "text" || target.type == "textarea")
2349 && event.which == 1 && event.ctrlKey))
2354 menu = create_ul ('visible');
2355 menu.style.fontFamily = 'sans-serif';
2356 menu.style.fontWeight = 'bold';
2357 menu.id = 'mim-menu';
2358 menu.onmousedown = select_im;
2359 menu.onmouseover = show_submenu;
2360 menu.onmouseout = destroy_menu;
2361 for (var catname in lang_category)
2363 var cat = lang_category[catname];
2364 var li = create_li (1, catname);
2365 var sub = create_ul ('hidden');
2366 for (var langname in cat)
2368 var lang = cat[langname];
2371 var sub_li = create_li (2, lang.name);
2372 sub_li.parentLi = li;
2373 var subsub = create_ul ('hidden');
2374 for (var name in lang.list)
2376 var im = lang.list[name];
2377 var subsub_li = create_li (3, im.name);
2378 subsub_li.parentLi = sub_li;
2380 subsub.appendChild (subsub_li);
2382 sub_li.appendChild (subsub);
2383 sub.appendChild (sub_li);
2385 li.appendChild (sub);
2386 menu.appendChild (li);
2388 lang_category = null;
2390 menu.style.left = (event.clientX - 10) + "px";
2391 menu.style.top = (event.clientY - 10) + "px";
2392 document.getElementsByTagName ('body')[0].appendChild (menu);
2395 MIM.init = function ()
2397 MIM.add_event_listener (window, 'keydown', MIM.keydown);
2398 MIM.add_event_listener (window, 'keypress', MIM.keypress);
2399 MIM.add_event_listener (window, 'mousedown', create_menu);
2400 MIM.current = MIM.imlist['t']['latn-post'];