1 // mim.js -- M17N Input Method driver
3 // National Institute of Advanced Industrial Science and Technology (AIST)
4 // Registration Number H15PRO112
6 // This file is part of the m17n database; a sub-part of the m17n
9 // The m17n library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public License
11 // as published by the Free Software Foundation; either version 2.1 of
12 // the License, or (at your option) any later version.
14 // The m17n library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with the m17n library; if not, write to the Free
21 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 // Boston, MA 02110-1301, USA.
24 // Please note that the code is not yet matured.
27 // URL of the input method server.
28 server: "http://www.m17n.org/common/mim-js",
29 // Boolean flag to tell if MIM is active or not.
31 // Boolean flag to tell if MIM is running in debug mode or not.
33 // List of main input methods.
35 // List of extra input methods;
37 // Global input method data
39 // Currently selected input method.
43 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
52 Preedit: 0x06, // PreeditText | CursorPos
53 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
75 ParseError: "parse-error"
79 if (window.location.hostname == 'localhost')
80 MIM.server = 'http://localhost/mim';
83 var keysyms = new Array ();
84 keysyms["bs"] = "BackSpace";
85 keysyms["lf"] = "Linefeed";
86 keysyms["cr"] = keysyms["enter"] = "Return";
87 keysyms["esc"] = "Escape";
88 keysyms["spc"] = "space";
89 keysyms["del"] = "Delete";
91 function decode_keysym (str) {
94 var parts = str.split ("-");
95 var len = parts.length, i;
96 var has_modifier = len > 1;
98 for (i = 0; i < len - 1; i++)
99 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
101 var key = parts[len - 1];
104 key = keysyms[key.toLowerCase ()];
110 for (i = 1; i < len - 1; i++)
111 str += '-' + parts[i];
120 parts = new Array ();
127 MIM.Key = function (val)
130 if (val instanceof Xex.Term)
132 else if (typeof val == 'string' || val instanceof String)
134 this.key = decode_keysym (val);
136 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
137 if (this.key instanceof Array)
139 this.key = this.key[0];
140 this.has_modifier = true;
143 else if (typeof val == 'number' || val instanceof Number)
144 this.key = String.fromCharCode (val);
146 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
149 MIM.Key.prototype.toString = function () { return this.key; };
151 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
152 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
153 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
157 MIM.KeySeq = function (seq)
159 this.val = new Array ();
165 var len = seq.val.length;
166 for (var i = 0; i < len; i++)
168 var v = seq.val[i], key;
169 if (v.type == 'symbol' || v.type == 'string')
170 key = new MIM.Key (v);
171 else if (v.type == 'integer')
172 key = new MIM.Key (v.val);
174 throw new Xex.ErrTerm (MIM.Error.ParseError,
175 "Invalid key: " + v);
177 if (key.has_modifier)
178 this.has_modifier = true;
183 var len = seq.val.length;
184 for (var i = 0; i < len; i++)
185 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
188 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
192 var proto = new Xex.Term ('keyseq');
193 proto.Clone = function () { return this; }
194 proto.Parser = function (domain, node)
196 var seq = new Array ();
197 for (node = node.firstChild; node; node = node.nextSibling)
198 if (node.nodeType == 1)
200 var term = Xex.Term.Parse (domain, node);
201 return new MIM.KeySeq (term);
203 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
205 proto.toString = function ()
207 var len = this.val.length;
211 var str = '<keyseq>';
212 for (var i = 0; i < len; i++)
216 else if (this.has_modifier)
218 str += this.val[i].toString ();
220 return str + '</keyseq>';
223 MIM.KeySeq.prototype = proto;
227 MIM.Marker = function () { }
228 MIM.Marker.prototype = new Xex.Term ('marker');
229 MIM.Marker.prototype.CharAt = function (ic)
231 var p = this.Position (ic);
232 if (p < 0 || p >= ic.preedit.length)
234 return ic.preedit.charCodeAt (p);
237 MIM.FloatingMarker = function (name) { this.val = name; };
238 var proto = new MIM.Marker ();
239 MIM.FloatingMarker.prototype = proto;
240 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
241 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
243 MIM.PredefinedMarker = function (name) { this.val = name; }
244 MIM.PredefinedMarker.prototype = new MIM.Marker ();
245 MIM.PredefinedMarker.prototype.Position = function (ic)
247 if (typeof this.pos == 'number')
249 return this.pos (ic);
254 function def_predefined (name, position)
256 predefined[name] = new MIM.PredefinedMarker (name);
257 predefined[name].pos = position;
260 def_predefined ('@<', 0);
261 def_predefined ('@>', function (ic) { return ic.preedit.length; });
262 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
263 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
264 def_predefined ('@[', function (ic) {
265 if (ic.cursor_pos > 0)
267 var pos = ic.cursor_pos;
268 return ic.preedit.FindProp ('candidates', pos - 1).from;
272 def_predefined ('@]', function (ic) {
273 if (ic.cursor_pos < ic.preedit.length - 1)
275 var pos = ic.cursor_pos;
276 return ic.preedit.FindProp ('candidates', pos).to;
278 return ic.preedit.length;
280 for (var i = 0; i < 10; i++)
281 def_predefined ("@" + i, i);
282 predefined['@first'] = predefined['@<'];
283 predefined['@last'] = predefined['@>'];
284 predefined['@previous'] = predefined['@-'];
285 predefined['@next'] = predefined['@+'];
286 predefined['@previous-candidate-change'] = predefined['@['];
287 predefined['@next-candidate-change'] = predefined['@]'];
289 MIM.SurroundMarker = function (name)
292 this.distance = parseInt (name.slice (1));
293 if (isNaN (this.distance))
294 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
296 MIM.SurroundMarker.prototype = new MIM.Marker ();
297 MIM.SurroundMarker.prototype.Position = function (ic)
299 return ic.cursor_pos + this.distance;
301 MIM.SurroundMarker.prototype.CharAt = function (ic)
303 if (this.val == '@-0')
305 var p = this.Position (ic);
307 return ic.GetSurroundingChar (p);
308 else if (p >= ic.preedit.length)
309 return ic.GetSurroundingChar (p - ic.preedit.length);
310 return ic.preedit.charCodeAt (p);
313 MIM.Marker.prototype.Parser = function (domain, node)
315 var name = node.firstChild.nodeValue;
316 if (name.charAt (0) == '@')
318 var n = predefined[name];
321 if (name.charAt (1) == '-' || name.charAt (1) == '+')
322 return new MIM.SurroundMarker (name);
323 throw new Xex.ErrTerm (MIM.Error.ParseError,
324 "Invalid marker: " + name);
326 return new MIM.FloatingMarker (name);;
330 MIM.Selector = function (name)
334 MIM.Selector.prototype = new Xex.Term ('selector');
338 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
339 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
340 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
341 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
342 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
343 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
344 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
346 MIM.Selector.prototype.Parser = function (domain, node)
348 var name = node.firstChild.nodeValue;
349 var s = selectors[name];
351 throw new Xex.ErrTerm (MIM.Error.ParseError,
352 "Invalid selector: " + name);
357 MIM.Rule = function (keyseq, actions)
359 this.keyseq = keyseq;
361 this.actions = actions;
363 MIM.Rule.prototype = new Xex.Term ('rule');
364 MIM.Rule.prototype.Parser = function (domain, node)
367 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
369 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
370 var keyseq = Xex.Term.Parse (domain, n);
371 if (keyseq.type != 'keyseq')
372 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
374 n = n.nextElement ();
376 actions = Xex.Term.Parse (domain, n, null);
377 return new MIM.Rule (keyseq, actions);
379 MIM.Rule.prototype.toString = function ()
384 MIM.Map = function (name)
387 this.rules = new Array ();
391 var proto = new Xex.Term ('map');
393 proto.Parser = function (domain, node)
395 var name = node.attributes['mname'].nodeValue;
397 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
398 var map = new MIM.Map (name);
399 for (var n = node.firstChild; n; n = n.nextSibling)
401 map.rules.push (Xex.Term.Parse (domain, n));
405 proto.toString = function ()
407 var str = '<map mname="' + this.name + '">';
408 var len = this.rules.length;
409 for (i = 0; i < len; i++)
410 str += this.rules[i];
411 return str + '</map>';
414 MIM.Map.prototype = proto;
417 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
419 MIM.Action = function (domain, terms)
421 var args = new Array ();
422 args.push (Xex.CatchTag_.mimtag);
423 for (var i = 0; i < terms.length; i++)
424 args.push (terms[i]);
425 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
428 MIM.Action.prototype.Run = function (domain)
430 var result = this.action.Eval (domain);
431 if (result.type == 'error')
433 domain.context.Error = result.toString ();
436 return (result != Xex.CatchTag._mimtag);
439 MIM.Keymap = function ()
448 function add_rule (keymap, rule, branch_actions)
450 var keyseq = rule.keyseq;
451 var len = keyseq.val.length;
454 for (var i = 0; i < len; i++)
456 var key = keyseq.val[i];
460 if (! keymap.submaps)
463 sub = keymap.submaps[key.key];
465 keymap.submaps[key.key] = sub = new MIM.Keymap ();
470 keymap.map_actions = rule.actions;
472 keymap.branch_actions = branch_actions;
475 proto.Add = function (map, branch_actions)
477 var rules = map.rules;
478 var len = rules.length;
480 for (var i = 0; i < len; i++)
481 add_rule (this, rules[i], branch_actions);
483 proto.Lookup = function (keys, index)
487 if (index < keys.val.length && this.submaps
488 && ! keys.val[index])
490 Xex.Log ('invalid key at ' + index);
494 if (index < keys.val.length && this.submaps
495 && (sub = this.submaps[keys.val[index].key]))
498 return sub.Lookup (keys, index);
500 return { map: this, index: index };
503 MIM.Keymap.prototype = proto;
506 MIM.State = function (name)
509 this.keymap = new MIM.Keymap ();
513 var proto = new Xex.Term ('state');
515 proto.Parser = function (domain, node)
517 var map_list = domain.map_list;
518 var name = node.attributes['sname'].nodeValue;
520 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
521 var state = new MIM.State (name);
522 for (node = node.firstElement (); node; node = node.nextElement ())
524 if (node.nodeName == 'title')
525 state.title = node.firstChild.nodeValue;
528 var n = node.firstElement ();
529 var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
530 if (node.nodeName == 'branch')
531 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
533 else if (node.nodeName == 'state-hook')
534 state.keymap.map_actions = branch_actions;
535 else if (node.nodeName == 'catch-all-branch')
536 state.keymap.branch_actions = branch_actions;
542 proto.toString = function ()
544 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
547 MIM.State.prototype = proto;
551 function Block (index, term)
555 this.Data = term.val;
556 else if (term.IsList)
558 this.Data = new Array ();
559 for (var i = 0; i < term.val.length; i++)
560 this.Data.push (term.val[i].val);
564 Block.prototype.Count = function () { return this.Data.length; }
565 Block.prototype.get = function (i)
567 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
570 MIM.Candidates = function (ic, candidates, column)
573 this.column = column;
577 this.blocks = new Array ();
579 for (var i = 0; i < candidates.length; i++)
581 var block = new Block (this.total, candidates[i]);
582 this.blocks.push (block);
583 this.total += block.Count ();
589 return (this.column > 0 ? this.index % this.column
590 : this.index - this.blocks[this.row].Index);
593 function prev_group ()
595 var col = get_col.call (this);
599 this.index -= this.column;
601 nitems = this.column;
604 var lastcol = (this.total - 1) % this.column;
605 this.index = (col < lastcol ? this.total - lastcol + col
607 this.row = this.blocks.length - 1;
608 nitems = lastcol + 1;
610 while (this.blocks[this.row].Index > this.index)
615 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
616 nitems = this.blocks[this.row].Count ();
617 this.index = (this.blocks[this.row].Index
618 + (col < nitems ? col : nitems - 1));
623 function next_group ()
625 var col = get_col.call (this);
629 this.index += this.column - col;
630 if (this.index < this.total)
632 if (this.index + col >= this.total)
634 nitems = this.total - this.index;
635 this.index = this.total - 1;
639 nitems = this.column;
648 while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
654 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
655 nitems = this.blocks[this.row].Count ();
656 this.index = (this.blocks[this.row].Index
657 + (col < nitems ? col : nitems - 1));
666 this.index = this.total - 1;
667 this.row = this.blocks.length - 1;
672 if (this.blocks[this.row].Index > this.index)
680 if (this.index == this.total)
687 var b = this.blocks[this.row];
688 if (this.index == b.Index + b.Count ())
695 this.index -= get_col.call (this);
696 while (this.blocks[this.row].Index > this.index)
702 var b = this.blocks[this.row];
705 if (this.index + 1 < this.total)
707 this.index += this.column - get_col.call (this) + 1;
708 while (b.Index + b.Count () <= this.index)
709 b = this.blocks[++this.row];
713 this.index = b.Index + b.Count () - 1;
716 MIM.Candidates.prototype.Current = function ()
718 var b = this.blocks[this.row];
719 return b.get (this.index - b.Index);
722 MIM.Candidates.prototype.Select = function (selector)
724 var idx = this.index;
725 var gidx = this.column > 0 ? idx / this.column : this.row;
726 if (selector.type == 'selector')
728 switch (selector.val)
730 case '@<': first.call (this); break;
731 case '@>': last.call (this); break;
732 case '@-': prev.call (this); break;
733 case '@+': next.call (this); break;
734 case '@[': prev_group.call (this); break;
735 case '@]': next_group.call (this); break;
744 col = this.index % this.column;
745 start = this.index - col;
746 end = start + this.column;
750 start = this.blocks[this.row].Index;
751 col = this.index - start;
752 end = start + this.blocks[this.row].Count;
754 if (end > this.total)
756 this.index += selector.val - col;
757 if (this.index >= end)
758 this.index = end - 1;
761 if (selector.val > col)
762 while (this.blocks[this.row].Index + this.blocks[this.row].Count
766 while (this.blocks[this.row].Index > this.index)
770 var newgidx = this.column > 0 ? this.index / this.column : this.row;
771 if (this.index != idx)
772 this.ic.changed |= (gidx == newgidx
773 ? MIM.ChangedStatus.CandidateIndex
774 : MIM.ChangedStatus.CandidateList);
775 return this.Current ();
778 MIM.Candidates.prototype.CurrentCol = function ()
780 return get_col.call (this);
783 MIM.Candidates.prototype.CurrentGroup = function ()
785 var col, start, end, gnum, gidx;
788 gnum = Math.floor ((this.total - 1) / this.column) + 1;
789 col = this.index % this.column;
790 start = this.index - col;
791 gidx = start / this.column + 1;
792 end = start + this.column;
793 if (end > this.total)
798 gnum = this.blocks.length;
800 start = this.blocks[this.row].Index;
801 col = this.index - start;
802 end = start + this.blocks[this.row].Count ();
804 var group = new Array ();
805 var indices = new Array (gnum, gidx, col);
806 group.push (indices);
808 var block = this.blocks[row++];
811 var c = block.get (start - block.Index);
814 if (start == block.Index + block.Count ())
815 block = this.blocks[row++];
821 MIM.im_domain = new Xex.Domain ('input-method', null, null);
822 MIM.im_domain.DefType (MIM.KeySeq.prototype);
823 MIM.im_domain.DefType (MIM.Marker.prototype);
824 MIM.im_domain.DefType (MIM.Selector.prototype);
825 MIM.im_domain.DefType (MIM.Rule.prototype);
826 MIM.im_domain.DefType (MIM.Map.prototype);
827 MIM.im_domain.DefType (MIM.State.prototype);
830 var im_domain = MIM.im_domain;
832 function Finsert (domain, vari, args)
835 if (args[0].type == 'integer')
836 text = String.fromCharCode (args[0].val);
839 domain.context.ins (text, null);
843 function Finsert_candidates (domain, vari, args)
845 var ic = domain.context;
846 var gsize = domain.variables['candidates-group-size'];
847 var candidates = new MIM.Candidates (ic, args,
848 gsize ? gsize.val.Intval () : 0);
849 ic.ins (candidates.Current (), candidates);
853 function Fdelete (domain, vari, args)
855 var ic = domain.context;
856 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
857 return new Xex.IntTerm (ic.del (pos));
860 function Fselect (domain, vari, args)
862 var ic = domain.context;
863 var can = ic.candidates;
867 var old_text = can.Current ();
868 var new_text = can.Select (args[0]);
869 ic.rep (old_text, new_text, can);
872 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
876 function Fshow (domain, vari, args)
878 domain.context.candidate_show = true;
879 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
883 function Fhide (domain, vari, args)
885 domain.context.candidate_show = false;
886 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
890 function Fchar_at (domain, vari, args)
892 return new Xex.IntTerm (args[0].CharAt (domain.context));
895 function Fmove (domain, vari, args)
897 var ic = domain.context;
898 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
900 return new Xex.IntTerm (pos);
903 function Fmark (domain, vari, args)
905 args[0].Mark (domain.context);
909 function Fpushback (domain, vari, args)
911 var a = (args[0].IsInt ? args[0].Intval ()
912 : args[0].IsStr ? new KeySeq (args[0])
914 domain.context.pushback (a);
918 function Fpop (domain, vari, args)
920 var ic = domain.context;
921 if (ic.key_head < ic.keys.val.length)
922 ic.keys.val.splice (ic.keys_head, 1);
926 function Fundo (domain, vari, args)
928 var ic = domain.context;
929 var n = args.length == 0 ? -2 : args[0].val;
930 Xex.Log ('undo with arg ' + args[0]);
932 ic.keys.val.splice (ic.keys.val.length + n, -n);
934 ic.keys.val.splice (n, ic.keys.val.length);
939 function Fcommit (domain, vari, args)
941 domain.context.commit ();
945 function Funhandle (domain, vari, args)
947 domain.context.commit ();
948 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
951 function Fshift (domain, vari, args)
953 var ic = domain.context;
954 var state_name = args[0].val;
955 var state = ic.im.state_list[state_name];
957 throw ("Unknown state: " + state_name);
962 function Fshiftback (domain, vari, args)
964 domain.context.shift (null);
968 function Fkey_count (domain, vari, args)
970 return new Xex.IntTerm (domain.context.key_head);
973 function Fsurrounding_flag (domain, vari, args)
975 return new Xex.IntTerm (-1);
978 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
979 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
980 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
981 im_domain.DefSubr (Fselect, "select", false, 1, 1);
982 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
983 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
984 im_domain.DefSubr (Fmove, "move", false, 1, 1);
985 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
986 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
987 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
988 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
989 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
990 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
991 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
992 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
993 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
994 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
995 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
1000 function get_global_var (vname)
1002 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
1003 MIM.im_global.Load ()
1004 return MIM.im_global.domain.variables[vname];
1007 function include (node)
1009 node = node.firstElement ();
1010 if (node.nodeName != 'tags')
1013 var lang = null, name = null, extra = null;
1014 for (node = node.firstElement (); node; node = node.nextElement ())
1016 if (node.nodeName == 'language')
1017 lang = node.firstChild.nodeValue;
1018 else if (node.nodeName == 'name')
1019 name = node.firstChild.nodeValue;
1020 else if (node.nodeName == 'extra-id')
1021 extra = node.firstChild.nodeValue;
1023 if (! lang || ! MIM.imlist[lang])
1027 if (! name || ! (im = MIM.imlist[lang][name]))
1032 if (! (im = MIM.imextra[lang][extra]))
1035 if (im.load_status != MIM.LoadStatus.Loaded
1036 && (im.load_status != MIM.LoadStatus.NotLoaded || ! im.Load ()))
1043 parsers['description'] = function (node)
1045 this.description = node.firstChild.nodeValue;
1047 parsers['variable-list'] = function (node)
1049 for (node = node.firstElement (); node; node = node.nextElement ())
1051 var vname = node.attributes['vname'].nodeValue;
1052 if (this != MIM.im_global)
1054 var vari = get_global_var (vname);
1056 this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
1058 vname = Xex.Term.Parse (this.domain, node)
1061 parsers['command-list'] = function (node)
1064 parsers['macro-list'] = function (node)
1066 for (var n = node.firstElement (); n; n = n.nextElement ())
1067 if (n.nodeName == 'xi:include')
1069 var im = include (n);
1071 alert ('inclusion fail');
1073 for (var macro in im.domain.functions)
1075 var func = im.domain.functions[macro];
1076 if (func instanceof Xex.Macro)
1077 im.domain.CopyFunc (this.domain, macro);
1079 n = n.previousSibling;
1080 node.removeChild (n.nextSibling);
1082 Xex.Term.Parse (this.domain, node.firstElement (), null);
1084 parsers['title'] = function (node)
1086 this.title = node.firstChild.nodeValue;
1088 parsers['map-list'] = function (node)
1090 for (node = node.firstElement (); node; node = node.nextElement ())
1092 if (node.nodeName == 'xi:include')
1094 var im = include (node);
1097 alert ('inclusion fail');
1100 for (var mname in im.map_list)
1101 this.map_list[mname] = im.map_list[mname];
1105 var map = Xex.Term.Parse (this.domain, node);
1106 this.map_list[map.name] = map;
1110 parsers['state-list'] = function (node)
1112 this.domain.map_list = this.map_list;
1113 for (node = node.firstElement (); node; node = node.nextElement ())
1115 if (node.nodeName == 'xi:include')
1117 var im = include (node);
1119 alert ('inclusion fail');
1120 for (var sname in im.state_list)
1122 state = im.state_list[sname];
1123 if (! this.initial_state)
1124 this.initial_state = state;
1125 this.state_list[sname] = state;
1128 else if (node.nodeName == 'state')
1130 var state = Xex.Term.Parse (this.domain, node);
1132 state.title = this.title;
1133 if (! this.initial_state)
1134 this.initial_state = state;
1135 this.state_list[state.name] = state;
1138 delete this.domain.map_list;
1141 MIM.IM = function (lang, name, extra_id, file)
1145 this.extra_id = extra_id;
1147 this.load_status = MIM.LoadStatus.NotLoaded;
1148 this.domain = new Xex.Domain (this.lang + '-'
1149 + (this.name != 'nil'
1150 ? this.name : this.extra_id),
1151 MIM.im_domain, null);
1157 var node = Xex.Load (MIM.server, this.file);
1160 this.load_status = MIM.LoadStatus.Error;
1164 this.initial_state = null;
1165 this.state_list = {};
1166 for (node = node.firstElement (); node; node = node.nextElement ())
1168 var name = node.nodeName;
1169 var parser = parsers[name];
1171 parser.call (this, node);
1173 this.load_status = MIM.LoadStatus.Loaded;
1178 MIM.IM.prototype = proto;
1180 MIM.IC = function (im, target)
1182 if (im.load_status == MIM.LoadStatus.NotLoaded)
1184 if (im.load_status != MIM.LoadStatus.Loaded)
1185 alert ('im:' + im.name + ' error:' + im.load_status);
1187 this.target = target;
1188 this.domain = new Xex.Domain ('context', im.domain, this);
1190 this.range = new Array ();
1191 this.range[0] = this.range[1] = 0;
1193 this.initial_state = this.im.initial_state;
1194 this.keys = new MIM.KeySeq ();
1195 this.marker_positions = new Array ();
1196 this.candidate_table = new MIM.CandidateTable ();
1200 MIM.CandidateTable = function ()
1202 this.table = new Array ();
1205 MIM.CandidateTable.prototype.get = function (pos)
1207 for (var i = 0; i < this.table.length; i++)
1209 var elt = this.table[i];
1210 if (elt.from < pos && pos <= elt.to)
1215 MIM.CandidateTable.prototype.put = function (from, to, candidates)
1217 for (var i = 0; i < this.table.length; i++)
1219 var elt = this.table[i];
1220 if (elt.from < to && elt.to > from)
1224 elt.val = candidates;
1228 this.table.push ({ from: from, to: to, val: candidates });
1231 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
1233 var diff = inserted - (to - from);
1236 for (var i = 0; i < this.table.length; i++)
1238 var elt = this.table[i];
1247 MIM.CandidateTable.prototype.clear = function ()
1249 this.table.length = 0;
1252 function set_cursor (prefix, pos)
1254 this.cursor_pos = pos;
1255 var candidates = this.candidate_table.get (pos);
1256 if (this.candidates != candidates)
1258 this.candidates = candidates;
1259 this.changed |= MIM.ChangedStatus.CandidateList;
1263 function save_state ()
1265 this.state_var_values = this.domain.SaveValues ();
1266 this.state_preedit = this.preedit;
1267 this.state_key_head = this.key_head;
1268 this.state_pos = this.cursor_pos;
1271 function restore_state ()
1273 this.domain.RestoreValues (this.state_var_values);
1274 this.preedit = this.state_preedit;
1275 set_cursor.call (this, "restore", this.state_pos);
1278 function handle_key ()
1280 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
1281 + '" in ' + this.state.name + ':' + this.keymap.name
1282 + " key/state/commit-head/len:"
1283 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
1284 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
1287 if (out.index > this.key_head)
1289 this.key_head = out.index;
1290 Xex.Log (' with submap', -1);
1291 restore_state.call (this);
1293 if (sub.map_actions)
1295 Xex.Log ('taking map actions:');
1296 if (! this.take_actions (sub.map_actions))
1299 else if (sub.submaps)
1301 Xex.Log ('no map actions');
1302 for (var i = this.state_key_head; i < this.key_head; i++)
1304 Xex.Log ('inserting key:' + this.keys.val[i].key);
1305 this.ins (this.keys.val[i].key, null);
1310 Xex.Log ('terminal:');
1311 if (this.keymap.branch_actions)
1313 Xex.Log ('branch actions:');
1314 if (! this.take_actions (this.keymap.branch_actions))
1317 if (sub != this.state.keymap)
1318 this.shift (this.state);
1323 Xex.Log (' without submap', -1);
1325 var current_state = this.state;
1326 var map = this.keymap;
1328 if (map.branch_actions)
1330 Xex.Log ('branch actions:');
1331 if (! this.take_actions (map.branch_actions))
1335 if (map == this.keymap)
1337 Xex.Log ('no state change');
1338 if (map == this.initial_state.keymap
1339 && this.key_head < this.keys.val.length)
1341 Xex.Log ('unhandled');
1344 if (map != current_state.keymap)
1345 this.shift (current_state);
1346 else if (this.keymap.actions == null)
1347 this.shift (this.initial_state);
1353 MIM.IC.prototype = {
1354 reset: function (clear_keys)
1356 this.cursor_pos = 0;
1357 this.prev_state = null;
1358 this.title = this.initial_state.title;
1359 this.state_preedit = '';
1360 this.state_key_head = 0;
1361 this.state_var_values = {};
1365 this.keys.val.length = 0;
1366 this.commit_key_head = 0;
1367 this.key_unhandled = false;
1368 this.unhandled_key = null;
1369 this.changed = MIM.ChangedStatus.None;
1370 this.error_message = '';
1371 this.title = this.initial_state.title;
1374 this.preedit_saved = '';
1375 if (this.candidate_show)
1377 this.candidate_table.clear ();
1378 this.candidates = null;
1379 this.candidate_show = false;
1380 for (var elt in this.marker_positions)
1381 this.marker_positions[elt] = 0;
1382 this.shift (this.initial_state);
1385 catch_args: new Array (Xex.CatchTag._mimtag, null),
1387 take_actions: function (actions)
1389 if (actions.length == 0)
1391 var func_progn = this.domain.GetFunc ('progn');
1392 var func_catch = this.domain.GetFunc ('catch');
1393 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
1394 var term = new Xex.Funcall (func_catch, null, this.catch_args);
1395 term = term.Eval (this.domain);
1396 return (! term.IsSymbol || term.val != '@mimtag');
1399 GetSurroundingChar: function (pos)
1403 pos += this.range[0];
1409 pos += this.range[1];
1410 if (pos >= this.target.value.length)
1413 return this.target.value.charCodeAt (pos);
1416 DelSurroundText: function (pos)
1421 pos += this.range[0];
1427 text = this.target.value.substring (0, pos);
1428 if (this.range[0] < this.target.value.length)
1429 text += this.target.value.substring (this.range[0]);
1430 this.target.value = text;
1431 this.range[1] -= this.range[0] - pos;
1432 this.range[0] = pos;
1436 pos += this.range[1];
1437 text = this.target.value.substring (0, this.range[1]);
1438 if (pos >= this.target.value.length)
1439 pos = this.target.value.length;
1441 text += this.target.value.substring (pos);
1442 this.target.value = text;
1446 adjust_markers: function (from, to, inserted)
1448 var diff = inserted - (to - from);
1450 for (var name in this.marker_positions)
1452 var pos = this.marker_positions[name];
1456 this.marker_positions[name] += diff;
1457 else if (pos > from)
1458 this.marker_positions[name] = from;
1463 preedit_replace: function (from, to, text, candidates)
1465 var newlen = text.length;
1466 this.preedit = (this.preedit.substring (0, from)
1467 + text + this.preedit.substring (to));
1468 this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
1469 this.adjust_markers (from, to, newlen);
1470 this.candidate_table.adjust (from, to, newlen);
1472 this.candidate_table.put (from, from + newlen, candidates)
1473 if (this.cursor_pos >= to)
1474 set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
1475 else if (this.cursor_pos > from)
1476 set_cursor.call (this, 'adjust', from)
1479 ins: function (text, candidates)
1481 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
1484 rep: function (old_text, new_text, candidates)
1486 this.preedit_replace (this.cursor_pos - old_text.length,
1487 this.cursor_pos, new_text, candidates);
1492 var deleted = pos - this.cursor_pos;
1493 if (pos < this.cursor_pos)
1497 this.DelSurroundText (pos);
1498 deleted = - this.cursor_pos;
1501 if (pos < this.cursor_pos)
1502 this.preedit_replace (pos, this.cursor_pos, '', null);
1506 if (pos > this.preedit.length)
1508 this.DelSurroundText (pos - this.preedit.length);
1509 deleted = this.preedit.length - this.cursor_pos;
1510 pos = this.preedit.length;
1512 if (pos > this.cursor_pos)
1513 this.preedit_replace (this.cursor_pos, pos, '', null);
1520 this.candidate_show = true;
1521 this.changed |= MIM.ChangedStatus.CandidateShow;
1526 this.candidate_show = false;
1527 this.changed |= MIM.ChangedStatus.CandidateShow;
1530 move: function (pos)
1534 else if (pos > this.preedit.length)
1535 pos = this.preedit.length;
1536 if (pos != this.cursor_pos)
1538 set_cursor.call (this, 'move', pos);
1539 this.changed |= MIM.ChangedStatus.Preedit;
1543 pushback: function (n)
1545 if (n instanceof MIM.KeySeq)
1547 if (this.key_head > 0)
1549 if (this.key_head < this.keys.val.length)
1550 this.keys.val.splice (this.key_head,
1551 this.keys.val.length - this.key_head);
1552 for (var i = 0; i < n.val.length; i++)
1553 this.keys.val.push (n.val[i]);
1559 if (this.key_head < 0)
1566 this.key_head = - n;
1567 if (this.key_head > this.keys.val.length)
1568 this.key_head = this.keys.val.length;
1574 if (this.key_head < this.keys.val.length)
1575 this.keys.val.splice (this.key_head, 1);
1580 if (this.preedit.length > 0)
1582 this.candidate_table.clear ();
1583 this.produced += this.preedit;
1584 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
1585 this.preedit_saved = '';
1587 this.commit_key_head = this.key_head;
1591 shift: function (state)
1595 if (this.prev_state == null)
1597 state = this.prev_state;
1600 if (state == this.initial_state)
1605 this.keys.val.splice (0, this.key_head);
1606 this.key_head = this.state_key_head = this.commit_key_head = 0;
1607 this.prev_state = null;
1612 if (state != this.state)
1613 this.prev_state = this.state;
1615 if (state != this.state && state.keymap.map_actions)
1616 this.take_actions (state.keymap.map_actions);
1617 if (! this.state || this.state.title != state.title)
1618 this.changed |= MIM.ChangedStatus.StateTitle;
1620 this.keymap = state.keymap;
1621 save_state.call (this);
1624 Filter: function (key)
1628 Xex.Log ("active = false");
1629 this.key_unhandled = true;
1630 this.unhandled_key = key;
1633 if (key.key == '_reload')
1635 this.changed = MIM.ChangedStatus.None;
1637 this.key_unhandled = false;
1638 this.keys.val.push (key);
1640 while (this.key_head < this.keys.val.length)
1642 if (! handle_key.call (this))
1644 if (this.key_head < this.keys.val.length)
1646 this.unhandled_key = this.keys.val[this.key_head];
1647 this.keys.val.splice (this.key_head, this.key_head + 1);
1649 if (this.state_key_head > 0)
1650 this.state_key_head--;
1651 if (this.commit_key_head > 0)
1652 this.commit_key_head--;
1653 this.key_unhandled = true;
1659 this.key_unhandled = true;
1663 if (this.keymap == this.initial_state.keymap)
1666 if (this.commit_key_head > 0)
1668 this.keys.val.splice (0, this.commit_key_head);
1669 this.key_head -= this.commit_key_head;
1670 this.state_key_head -= this.commit_key_head;
1671 this.commit_key_head = 0;
1673 if (this.key_unhandled)
1675 this.keys.val.length = 0;
1676 //this.keys.val.splice (0, this.keys.val.length);
1677 this.key_head = this.state_key_head = this.commit_key_head = 0;
1679 if (this.changed & MIM.ChangedStatus.Candidate)
1681 if (this.candidate_show && this.candidates)
1686 return (! this.key_unhandled
1687 && this.produced.length == 0);
1691 // Load the list of input methods.
1692 var node = Xex.Load (MIM.server, "imlist.xml");
1693 for (node = node.firstChild; node; node = node.nextSibling)
1694 if (node.nodeName == 'input-method')
1696 var lang = null, name = null, extra_id = null, file = null;
1698 for (var n = node.firstChild; n; n = n.nextSibling)
1700 if (n.nodeName == 'language')
1701 lang = n.firstChild.nodeValue;
1702 else if (n.nodeName == 'name')
1703 name = n.firstChild.nodeValue;
1704 else if (n.nodeName == 'extra-id')
1705 extra_id = n.firstChild.nodeValue;
1706 else if (n.nodeName == 'filename')
1707 file = n.firstChild.nodeValue;
1709 if (name && name != 'nil')
1711 if (! MIM.imlist[lang])
1712 MIM.imlist[lang] = {};
1713 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
1715 else if (extra_id && extra_id != 'nil')
1717 if (! MIM.imextra[lang])
1718 MIM.imextra[lang] = {};
1719 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
1722 if (MIM.imextra.t && MIM.imextra.t.global)
1723 MIM.im_global = MIM.imextra.t.global;
1726 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
1727 MIM.im_global.load_status = MIM.LoadStatus.Error;
1733 var keys = new Array ();
1735 keys[0x08] = 'BackSpace';
1736 keys[0x0D] = 'Return';
1737 keys[0x1B] = 'Escape';
1738 keys[0x20] = 'space';
1739 keys[0x21] = 'Page_Up';
1740 keys[0x22] = 'Page_Down';
1742 keys[0x24] = 'Home';
1743 keys[0x25] = 'Left';
1745 keys[0x27] = 'Right';
1746 keys[0x28] = 'Down';
1747 keys[0x2D] = 'Insert';
1748 keys[0x2E] = 'Delete';
1749 for (var i = 1; i <= 12; i++)
1750 keys[111 + i] = "f" + i;
1751 keys[0x90] = "Num_Lock";
1752 keys[0xF0] = "Caps_Lock";
1755 keyids['U+0008'] = 'BackSpace';
1756 keyids['U+0009'] = 'Tab';
1757 keyids['U+0018'] = 'Cancel';
1758 keyids['U+001B'] = 'Escape';
1759 keyids['U+0020'] = 'space';
1760 keyids['U+007F'] = 'Delete';
1761 keyids['PageUp'] = 'Page_Up';
1762 keyids['PageDown'] = 'Page_Down';
1765 modifiers.Shift = 1;
1766 modifiers.Control = 1;
1768 modifiers.AltGraph = 1;
1771 MIM.decode_key_event = function (event)
1773 var key = event.keyIdentifier;
1775 if (key) // keydown event of Chrome
1780 var shifted = event.shiftKey;
1781 if (event.ctrlKey) mod += 'C-';
1782 if (event.metaKey) mod += 'M-';
1783 if (event.altKey) mod += 'A-';
1784 var keysym = keyids[key];
1787 else if (key.match(/^U\+([0-9A-Z]+)$/))
1789 if (mod.length == 0)
1791 var c = parseInt (RegExp.$1, 16);
1792 // Chrome sets, for instance, "U+0x00C1" when the key 'A'
1793 // is typed with control or alt/meta key.
1796 if (c >= 0x41 && c <= 0x5A && ! event.shiftKey)
1798 key = String.fromCharCode (c);
1801 if (shifted) mod += 'S-';
1802 return new MIM.Key (mod + key);
1806 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
1807 : event.charCode ? event.charCode
1811 if (event.type == 'keydown')
1816 if (event.shiftKey) key = "S-" + key ;
1819 key = String.fromCharCode (key);
1821 if (event.altKey) key = "A-" + key ;
1822 if (event.ctrlKey) key = "C-" + key ;
1823 return new MIM.Key (key);
1827 MIM.add_event_listener
1828 = (window.addEventListener
1829 ? function (target, type, listener) {
1830 target.addEventListener (type, listener, false);
1832 : window.attachEvent
1833 ? function (target, type, listener) {
1834 target.attachEvent ('on' + type,
1836 listener.call (target, window.event);
1839 : function (target, type, listener) {
1841 = function (e) { listener.call (target, e || window.event); };
1844 MIM.debug_print = function () { };
1846 MIM.get_range = function (target, ic)
1849 if (target.selectionStart != null) // for Mozilla
1851 from = target.selectionStart;
1852 to = target.selectionEnd;
1856 var r = document.selection.createRange ();
1857 var rr = r.duplicate ();
1859 rr.moveToElementText (target);
1860 rr.setEndPoint ('EndToEnd', range);
1861 from = rr.text.length - r.text.length;
1862 to = rr.text.length;
1865 && from == ic.range[0] + ic.cursor_pos
1866 && (ic.preedit.length == 0
1867 || ic.preedit == target.value.substring (ic.range[0], ic.range[1])))
1879 padingLeft: 'padding-left',
1880 paddingRight: 'padding-right',
1881 paddingTop: 'padding-top',
1882 paddintBottom: 'padding-bottom',
1883 marginRight: 'margin-right',
1884 marginTop: 'margin-top',
1885 borderLeftStyle: 'border-left-style',
1886 borderRightStyle: 'border-right-style',
1887 borderTopStyle: 'border-top-style',
1888 borderBottomStyle: 'border-bottom-style',
1889 borderLeftWidth: 'border-left-width',
1890 borderRightWidth: 'border-right-width',
1891 borderTopWidth: 'border-top-width',
1892 borderBottomWidth: 'border-bottom-width',
1893 fontFamily: 'font-family',
1894 fontSize: 'font-size',
1895 lineHeight: 'line-height',
1896 letterSpacing: 'letter-spacing',
1897 wordSpacing: 'word-spacing' };
1899 function copy_style (from, to)
1901 var from_style = getComputedStyle(from,'');
1902 for(var name in style_props)
1903 to.style[name] = from_style[style_props[name]];
1904 to.style.left = from.offsetLeft + 'px';
1905 to.style.top = from.offsetTop + 'px';
1906 to.style.width = from.offsetWidth;
1907 to.style.height = from.offsetHeight;
1911 var temp; // Temporary 'div' element
1913 MIM.get_preedit_pos = function (target, ic)
1917 temp = document.createElement ('div');
1918 temp.style.visibility = 'hidden';
1919 temp.style.position = 'absolute';
1920 temp.appendChild (document.createElement ('span'));
1921 temp.appendChild (document.createElement ('span'));
1922 document.getElementsByTagName ('body')[0].appendChild (temp);
1926 var styles = copy_style (target, temp);
1927 ic.abs_top = (parseInt (styles.marginTop)
1928 + parseInt (styles.borderTopWidth)
1929 + parseInt (styles.paddingTop));
1930 ic.abs_left = (parseInt (styles.marginLeft)
1931 + parseInt (styles.borderLeftWidth)
1932 + parseInt (styles.paddingLeft));
1933 for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
1935 ic.abs_top += elm.offsetTop;
1936 ic.abs_left += elm.offsetLeft;
1940 temp.firstChild.innerText = target.value.substring (0, ic.range[0]);
1941 temp.lastChild.innerText
1942 = (ic.range[0] == ic.range[1] ? "|"
1943 : target.value.substring (ic.range[0], ic.range[1]));
1944 ic.abs_y = (ic.abs_top + temp.lastChild.offsetTop
1945 + temp.lastChild.offsetHeight - target.scrollTop);
1946 ic.abs_x0 = ic.abs_left + temp.lastChild.offsetLeft;
1947 ic.abs_x1 = ic.abs_x0 + temp.lastChild.offsetWidth;
1951 MIM.update_bar = function (target, ic)
1953 if (ic.preedit.length > 0)
1955 MIM.get_preedit_pos (target, ic);
1958 ic.bar = document.createElement ('div');
1959 ic.bar.style.position = 'absolute';
1960 ic.bar.style.backgroundColor = "black";
1961 ic.bar.style.minHeight = '1px';
1962 document.getElementsByTagName ('body')[0].appendChild (ic.bar);
1964 ic.bar.style.display = 'block'
1965 ic.bar.style.top = ic.abs_y + 'px';
1966 ic.bar.style.left = ic.abs_x0 + 'px';
1967 ic.bar.style.minWidth = ic.bar.style.maxWidth = (ic.abs_x1 - ic.abs_x0) + 'px';
1970 ic.bar.style.display = 'none'
1973 MIM.update = function (target, ic, for_focus_out)
1975 var text = target.value;
1976 target.value = (text.substring (0, ic.range[0])
1979 + text.substring (ic.range[1]));
1980 ic.range[0] += ic.produced.length;
1981 ic.range[1] = ic.range[0] + ic.preedit.length;
1982 MIM.update_bar (target, ic);
1983 if (! for_focus_out)
1985 var pos = ic.range[0] + ic.cursor_pos;
1986 if (target.setSelectionRange) // Mozilla
1988 var scrollTop = target.scrollTop;
1989 target.setSelectionRange (pos, pos);
1990 target.scrollTop = scrollTop;
1994 var range = target.createTextRange ();
1995 range.moveStart ('character', pos);
1996 range.moveEnd ('character', pos);
2003 MIM.show = function (ic)
2005 if (! ic.candidates)
2007 var target = ic.target;
2008 MIM.get_preedit_pos (target, ic);
2011 ic.can_node = document.createElement ('table');
2012 ic.can_node.style.position = 'absolute';
2013 ic.can_node.style.display = 'none';
2014 ic.can_node.style.backgroundColor = "white";
2015 ic.can_node.style.border = "1px solid black";
2016 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
2019 if (ic.changed & MIM.ChangedStatus.CandidateList)
2021 while (ic.can_node.childNodes.length > 0)
2022 ic.can_node.removeChild (ic.can_node.firstChild);
2023 var tr = document.createElement ('tr');
2024 var group = ic.candidates.CurrentGroup ();
2025 var td = document.createElement ('td');
2026 td.innerHTML = group[0][1] + '/' + group[0][0];
2027 td.style.color = 'white';
2028 td.style.backgroundColor = 'black';
2029 tr.appendChild (td);
2030 for (var i = 1; i < group.length; i++)
2032 var td = document.createElement ('td');
2034 td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
2035 if (i == group[0][2] + 1)
2036 td.style.backgroundColor = 'lightblue';
2037 tr.appendChild (td);
2039 ic.can_node.appendChild (tr);
2040 ic.can_node.style.top = (ic.abs_y + 10) + 'px';
2041 ic.can_node.style.left = ic.abs_x0 + 'px';
2045 var td = ic.can_node.firstElement ().firstElement ().nextElement ();
2046 var col = ic.candidates.CurrentCol ();
2047 for (var i = 0; td; td = td.nextElement ())
2048 td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
2050 ic.can_node.style.display = 'block';
2053 MIM.hide = function (ic)
2056 ic.can_node.style.display = 'none';
2059 function real_focus_in (target, ic)
2061 if (MIM.get_range (target, ic))
2062 ic.Filter (MIM.Key.FocusIn);
2063 MIM.update (target, ic, false);
2068 MIM.focus_in = function (event)
2070 var target = event.target;
2071 Xex.Log ("Focus in " + target.tagName);
2073 = setTimeout (function () {real_focus_in (target, target.mim_ic)} , 10);
2077 MIM.click = function (event)
2079 var target = event.target;
2080 Xex.Log ("Click in " + target.tagName);
2083 clearTimeout (focus_in_timer);
2084 focus_in_timer = null;
2086 real_focus_in (target, target.mim_ic);
2091 MIM.focus_out = function (event)
2093 var target = event.target;
2094 var ic = target.mim_ic;
2095 Xex.Log ("Focus out " + target.tagName);
2096 MIM.get_range (target, ic);
2097 MIM.debug_print (event, ic);
2098 ic.Filter (MIM.Key.FocusOut);
2099 MIM.update (target, ic, true);
2103 MIM.keydown = function (event)
2105 var target = event.target;
2106 if (! (target.type == "text" || target.type == "textarea"))
2109 var ic = target.mim_ic;
2110 if (! ic || ic.im != MIM.current)
2112 target.mim_ic = null;
2113 Xex.Log ('creating IC');
2114 ic = new MIM.IC (MIM.current, target);
2115 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2118 MIM.add_event_listener (target, 'focus', MIM.focus_in);
2119 MIM.add_event_listener (target, 'blur', MIM.focus_out);
2120 MIM.add_event_listener (target, 'click', MIM.click);
2122 MIM.get_range (target, ic)
2123 MIM.debug_print (event, ic);
2124 ic.key = MIM.decode_key_event (event);
2128 var result = ic.Filter (ic.key);
2130 Xex.Log ('Error' + e);
2133 MIM.update (target, ic, false);
2134 if (! ic.key_unhandled)
2135 event.preventDefault ();
2139 MIM.keypress = function (event)
2141 var target = event.target;
2142 if (! (target.type == "text" || target.type == "textarea"))
2145 var ic = target.mim_ic;
2149 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2152 ic.key = MIM.decode_key_event (event);
2160 var result = ic.Filter (ic.key);
2162 Xex.Log ('Error:' + e);
2165 MIM.update (target, ic, false);
2166 if (! ic.key_unhandled)
2167 event.preventDefault ();
2169 Xex.Log ("error:" + e);
2170 event.preventDefault ();
2172 MIM.debug_print (event, ic);
2179 var lang_category = {
2181 cs: { name: 'Czech' },
2182 da: { name: 'Danish' },
2183 el: { name: 'Greek' },
2184 en: { name: 'English' },
2185 eo: { name: 'Esperanto' },
2186 fr: { name: 'French' },
2187 grc: { name: 'ClassicGreek' },
2188 hr: { name: 'Croatian' },
2189 hy: { name: 'Armenian' },
2190 ka: { name: 'Georgian' },
2191 kk: { name: 'Kazakh' },
2192 ru: { name: 'Russian' },
2193 sk: { name: 'Slovak' },
2194 sr: { name: 'Serbian' },
2195 sv: { name: 'Swedish' },
2196 yi: { name: 'Yiddish' } },
2198 ar: { name: 'Arabic' },
2199 dv: { name: 'Divehi' },
2200 fa: { name: 'Persian' },
2201 he: { name: 'Hebrew' },
2202 kk: { name: 'Kazakh' },
2203 ps: { name: 'Pushto' },
2204 ug: { name: 'Uighur' },
2205 yi: { name: 'Yiddish' } },
2207 as: { name: 'Assamese' },
2208 bn: { name: 'Bengali' },
2209 bo: { name: 'Tibetan' },
2210 gu: { name: 'Gujarati' },
2211 hi: { name: 'Hindi' },
2212 kn: { name: 'Kannada' },
2213 ks: { name: 'Kashmiri' },
2214 ml: { name: 'Malayalam' },
2215 mr: { name: 'Marathi' },
2216 ne: { name: 'Nepali' },
2217 or: { name: 'Oriya' },
2218 pa: { name: 'Panjabi' },
2219 sa: { name: 'Sanskirit' },
2220 sd: { name: 'Sindhi' },
2221 si: { name: 'Sinhalese' },
2222 ta: { name: 'Tamil' },
2223 te: { name: 'Telugu' },
2224 ur: { name: 'Urdu' } },
2226 cmc: { name: 'Cham' },
2227 km: { name: 'Khmer'},
2228 lo: { name: 'Lao' },
2229 my: { name: 'Burmese' },
2230 tai: { name: 'Tai Viet' },
2231 th: { name: 'Thai' },
2232 vi: { name: 'Vietanamese' } },
2234 ii: { name: 'Yii' },
2235 ja: { name: 'Japanese' },
2236 ko: { name: 'Korean' },
2237 zh: { name: 'Chinese' } },
2239 am: { name: 'Amharic' },
2240 ath: { name: 'Carrier' },
2241 bla: { name: 'Blackfoot' },
2242 cr: { name: 'Cree' },
2243 eo: { name: 'Esperanto' },
2244 iu: { name: 'Inuktitut' },
2245 nsk: { name: 'Naskapi' },
2246 oj: { name: 'Ojibwe' },
2247 t: { name: 'Generic' } }
2250 function categorize_im ()
2252 var cat, lang, list, name;
2253 for (lang in MIM.imlist)
2256 for (cat in lang_category)
2257 if (lang_category[cat][lang])
2259 list = lang_category[cat][lang].list;
2261 list = lang_category[cat][lang].list = {};
2262 for (name in MIM.imlist[lang])
2263 list[name] = MIM.imlist[lang][name];
2266 for (name in MIM.imlist[lang])
2267 Xex.Log ('no category ' + lang + '-' + name);
2276 clearTimeout (destroy_timer);
2277 destroy_timer = null;
2278 var target = document.getElementById ('mim-menu');
2281 for (; last_target && last_target.menu_level;
2282 last_target = last_target.parentLi)
2283 last_target.style.backgroundColor = 'white';
2284 var nodes = target.getElementsByTagName ('ul');
2285 for (var i = 0; i < nodes.length; i++)
2286 nodes[i].style.visibility = 'hidden';
2287 document.getElementsByTagName ('body')[0].removeChild (target);
2291 function destroy_menu () {
2292 if (! destroy_timer)
2293 destroy_timer = setTimeout (destroy, 1000);
2297 function show_submenu (event)
2299 var target = event.target;
2300 if (! target.menu_level)
2302 if (! target.parentNode || ! target.parentNode.menu_level)
2304 target = target.parentNode;
2308 clearTimeout (destroy_timer);
2309 destroy_timer = null;
2311 if (last_target && target.parentLi != last_target)
2313 last_target.style.backgroundColor = 'white';
2314 while (target.menu_level < last_target.menu_level)
2316 last_target = last_target.parentLi;
2317 last_target.style.backgroundColor = 'white';
2319 var uls = last_target.getElementsByTagName ('ul');
2320 for (var i = 0; i < uls.length; i++)
2321 uls[i].style.visibility = 'hidden';
2323 last_target = target;
2324 target.style.backgroundColor = 'yellow';
2325 if (target.menu_level < 3)
2327 target.lastChild.style.visibility = 'visible';
2328 target.lastChild.style.left = target.clientWidth + 'px';
2330 event.preventDefault ();
2333 function select_im (event)
2335 var target = event.target;
2338 if (! target.parentNode || ! target.parentNode.menu_level)
2340 event.preventDefault ();
2343 target = target.parentNode;
2347 MIM.current = target.im;
2350 event.preventDefault ();
2353 function create_ul (visibility)
2355 var ul = document.createElement ('ul');
2356 ul.style.position = 'absolute';
2357 ul.style.margin = '0px';
2358 ul.style.padding = '0px';
2359 ul.style.border = '1px solid gray';
2360 ul.style.borderBottom = 'none';
2361 ul.style.top = '-1px';
2362 ul.style.backgroundColor = 'white';
2363 ul.style.visibility = visibility;
2367 function create_li (level, text)
2369 var li = document.createElement ('li');
2370 li.style.position = 'relative';
2371 li.style.margin = '0px';
2372 li.style.padding = '1px';
2373 li.style.borderBottom = '1px solid gray';
2374 li.style.top = '0px';
2375 li.style.listStyle = 'none';
2376 li.menu_level = level;
2377 var nobr = document.createElement ('nobr');
2378 nobr.innerHTML = text;
2379 li.appendChild (nobr);
2385 function create_menu (event)
2387 var target = event.target;
2389 if (! ((target.type == "text" || target.type == "textarea")
2390 && event.which == 1 && event.ctrlKey))
2395 menu = create_ul ('visible');
2396 menu.style.fontFamily = 'sans-serif';
2397 menu.style.fontWeight = 'bold';
2398 menu.id = 'mim-menu';
2399 menu.onmousedown = select_im;
2400 menu.onmouseover = show_submenu;
2401 menu.onmouseout = destroy_menu;
2402 for (var catname in lang_category)
2404 var cat = lang_category[catname];
2405 var li = create_li (1, catname);
2406 var sub = create_ul ('hidden');
2407 for (var langname in cat)
2409 var lang = cat[langname];
2412 var sub_li = create_li (2, lang.name);
2413 sub_li.parentLi = li;
2414 var subsub = create_ul ('hidden');
2415 for (var name in lang.list)
2417 var im = lang.list[name];
2418 var subsub_li = create_li (3, im.name);
2419 subsub_li.parentLi = sub_li;
2421 subsub.appendChild (subsub_li);
2423 sub_li.appendChild (subsub);
2424 sub.appendChild (sub_li);
2426 li.appendChild (sub);
2427 menu.appendChild (li);
2429 lang_category = null;
2431 menu.style.left = (event.clientX - 10) + "px";
2432 menu.style.top = (event.clientY - 10) + "px";
2433 document.getElementsByTagName ('body')[0].appendChild (menu);
2436 MIM.init = function ()
2438 MIM.add_event_listener (window, 'keydown', MIM.keydown);
2439 MIM.add_event_listener (window, 'keypress', MIM.keypress);
2440 MIM.add_event_listener (window, 'mousedown', create_menu);
2441 MIM.current = MIM.imlist['t']['latn-post'];