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 MIM.create_list = function (node) {
1692 // Load the list of input methods.
1694 alert ('create-list called: node=' + MIM.A);
1695 var node = node.firstElement;
1696 for (node = node.firstChild; node; node = node.nextSibling)
1697 if (node.nodeName == 'input-method')
1699 var lang = null, name = null, extra_id = null, file = null;
1701 for (var n = node.firstChild; n; n = n.nextSibling)
1703 if (n.nodeName == 'language')
1704 lang = n.firstChild.nodeValue;
1705 else if (n.nodeName == 'name')
1706 name = n.firstChild.nodeValue;
1707 else if (n.nodeName == 'extra-id')
1708 extra_id = n.firstChild.nodeValue;
1709 else if (n.nodeName == 'filename')
1710 file = n.firstChild.nodeValue;
1712 if ((lang == 'ja' && name == 'anthy')
1713 || (lang == 'en' && name == 'ispell'))
1715 if (name && name != 'nil')
1717 if (! MIM.imlist[lang])
1718 MIM.imlist[lang] = {};
1719 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
1721 else if (extra_id && extra_id != 'nil')
1723 if (! MIM.imextra[lang])
1724 MIM.imextra[lang] = {};
1725 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
1728 if (MIM.imextra.t && MIM.imextra.t.global)
1729 MIM.im_global = MIM.imextra.t.global;
1732 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
1733 MIM.im_global.load_status = MIM.LoadStatus.Error;
1736 MIM.current = MIM.imlist['t']['latn-post'];
1741 var keys = new Array ();
1743 keys[0x08] = 'BackSpace';
1744 keys[0x0D] = 'Return';
1745 keys[0x1B] = 'Escape';
1746 keys[0x20] = 'space';
1747 keys[0x21] = 'Page_Up';
1748 keys[0x22] = 'Page_Down';
1750 keys[0x24] = 'Home';
1751 keys[0x25] = 'Left';
1753 keys[0x27] = 'Right';
1754 keys[0x28] = 'Down';
1755 keys[0x2D] = 'Insert';
1756 keys[0x2E] = 'Delete';
1757 for (var i = 1; i <= 12; i++)
1758 keys[111 + i] = "f" + i;
1759 keys[0x90] = "Num_Lock";
1760 keys[0xF0] = "Caps_Lock";
1763 keyids['U+0008'] = 'BackSpace';
1764 keyids['U+0009'] = 'Tab';
1765 keyids['U+0018'] = 'Cancel';
1766 keyids['U+001B'] = 'Escape';
1767 keyids['U+0020'] = 'space';
1768 keyids['U+007F'] = 'Delete';
1769 keyids['PageUp'] = 'Page_Up';
1770 keyids['PageDown'] = 'Page_Down';
1773 modifiers.Shift = 1;
1774 modifiers.Control = 1;
1776 modifiers.AltGraph = 1;
1779 MIM.decode_key_event = function (event)
1781 var key = event.keyIdentifier;
1783 if (key) // keydown event of Chrome
1788 var shifted = event.shiftKey;
1789 if (event.ctrlKey) mod += 'C-';
1790 if (event.metaKey) mod += 'M-';
1791 if (event.altKey) mod += 'A-';
1792 var keysym = keyids[key];
1795 else if (key.match(/^U\+([0-9A-Z]+)$/))
1797 if (mod.length == 0)
1799 var c = parseInt (RegExp.$1, 16);
1800 // Chrome sets, for instance, "U+0x00C1" when the key 'A'
1801 // is typed with control or alt/meta key.
1804 if (c >= 0x41 && c <= 0x5A && ! event.shiftKey)
1806 key = String.fromCharCode (c);
1809 if (shifted) mod += 'S-';
1810 return new MIM.Key (mod + key);
1814 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
1815 : event.charCode ? event.charCode
1819 if (event.type == 'keydown')
1824 if (event.shiftKey) key = "S-" + key ;
1827 key = String.fromCharCode (key);
1829 if (event.altKey) key = "A-" + key ;
1830 if (event.ctrlKey) key = "C-" + key ;
1831 return new MIM.Key (key);
1835 MIM.add_event_listener
1836 = (window.addEventListener
1837 ? function (target, type, listener) {
1838 target.addEventListener (type, listener, false);
1840 : window.attachEvent
1841 ? function (target, type, listener) {
1842 target.attachEvent ('on' + type,
1844 listener.call (target, window.event);
1847 : function (target, type, listener) {
1849 = function (e) { listener.call (target, e || window.event); };
1852 MIM.debug_print = function () { };
1854 MIM.get_range = function (target, ic)
1857 if (target.selectionStart != null) // for Mozilla
1859 from = target.selectionStart;
1860 to = target.selectionEnd;
1864 var r = document.selection.createRange ();
1865 var rr = r.duplicate ();
1867 rr.moveToElementText (target);
1868 rr.setEndPoint ('EndToEnd', range);
1869 from = rr.text.length - r.text.length;
1870 to = rr.text.length;
1873 && from == ic.range[0] + ic.cursor_pos
1874 && (ic.preedit.length == 0
1875 || ic.preedit == target.value.substring (ic.range[0], ic.range[1])))
1887 padingLeft: 'padding-left',
1888 paddingRight: 'padding-right',
1889 paddingTop: 'padding-top',
1890 paddintBottom: 'padding-bottom',
1891 marginRight: 'margin-right',
1892 marginTop: 'margin-top',
1893 borderLeftStyle: 'border-left-style',
1894 borderRightStyle: 'border-right-style',
1895 borderTopStyle: 'border-top-style',
1896 borderBottomStyle: 'border-bottom-style',
1897 borderLeftWidth: 'border-left-width',
1898 borderRightWidth: 'border-right-width',
1899 borderTopWidth: 'border-top-width',
1900 borderBottomWidth: 'border-bottom-width',
1901 fontFamily: 'font-family',
1902 fontSize: 'font-size',
1903 lineHeight: 'line-height',
1904 letterSpacing: 'letter-spacing',
1905 wordSpacing: 'word-spacing' };
1907 function copy_style (from, to)
1909 var from_style = getComputedStyle(from,'');
1910 for(var name in style_props)
1911 to.style[name] = from_style[style_props[name]];
1912 to.style.left = from.offsetLeft + 'px';
1913 to.style.top = from.offsetTop + 'px';
1914 to.style.width = from.offsetWidth;
1915 to.style.height = from.offsetHeight;
1919 var temp; // Temporary 'div' element
1921 MIM.get_preedit_pos = function (target, ic)
1925 temp = document.createElement ('div');
1926 temp.style.visibility = 'hidden';
1927 temp.style.position = 'absolute';
1928 temp.appendChild (document.createElement ('span'));
1929 temp.appendChild (document.createElement ('span'));
1930 document.getElementsByTagName ('body')[0].appendChild (temp);
1934 var styles = copy_style (target, temp);
1935 ic.abs_top = (parseInt (styles.marginTop)
1936 + parseInt (styles.borderTopWidth)
1937 + parseInt (styles.paddingTop));
1938 ic.abs_left = (parseInt (styles.marginLeft)
1939 + parseInt (styles.borderLeftWidth)
1940 + parseInt (styles.paddingLeft));
1941 for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
1943 ic.abs_top += elm.offsetTop;
1944 ic.abs_left += elm.offsetLeft;
1948 temp.firstChild.innerText = target.value.substring (0, ic.range[0]);
1949 temp.lastChild.innerText
1950 = (ic.range[0] == ic.range[1] ? "|"
1951 : target.value.substring (ic.range[0], ic.range[1]));
1952 ic.abs_y = (ic.abs_top + temp.lastChild.offsetTop
1953 + temp.lastChild.offsetHeight - target.scrollTop);
1954 ic.abs_x0 = ic.abs_left + temp.lastChild.offsetLeft;
1955 ic.abs_x1 = ic.abs_x0 + temp.lastChild.offsetWidth;
1959 MIM.update_bar = function (target, ic)
1961 if (ic.preedit.length > 0)
1963 MIM.get_preedit_pos (target, ic);
1966 ic.bar = document.createElement ('div');
1967 ic.bar.style.position = 'absolute';
1968 ic.bar.style.backgroundColor = "black";
1969 ic.bar.style.minHeight = '1px';
1970 document.getElementsByTagName ('body')[0].appendChild (ic.bar);
1972 ic.bar.style.display = 'block'
1973 ic.bar.style.top = ic.abs_y + 'px';
1974 ic.bar.style.left = ic.abs_x0 + 'px';
1975 ic.bar.style.minWidth = ic.bar.style.maxWidth = (ic.abs_x1 - ic.abs_x0) + 'px';
1978 ic.bar.style.display = 'none'
1981 MIM.update = function (target, ic, for_focus_out)
1983 var text = target.value;
1984 target.value = (text.substring (0, ic.range[0])
1987 + text.substring (ic.range[1]));
1988 ic.range[0] += ic.produced.length;
1989 ic.range[1] = ic.range[0] + ic.preedit.length;
1990 MIM.update_bar (target, ic);
1991 if (! for_focus_out)
1993 var pos = ic.range[0] + ic.cursor_pos;
1994 if (target.setSelectionRange) // Mozilla
1996 var scrollTop = target.scrollTop;
1997 target.setSelectionRange (pos, pos);
1998 target.scrollTop = scrollTop;
2002 var range = target.createTextRange ();
2003 range.moveStart ('character', pos);
2004 range.moveEnd ('character', pos);
2011 MIM.show = function (ic)
2013 if (! ic.candidates)
2015 var target = ic.target;
2016 MIM.get_preedit_pos (target, ic);
2019 ic.can_node = document.createElement ('table');
2020 ic.can_node.style.position = 'absolute';
2021 ic.can_node.style.display = 'none';
2022 ic.can_node.style.backgroundColor = "white";
2023 ic.can_node.style.border = "1px solid black";
2024 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
2027 if (ic.changed & MIM.ChangedStatus.CandidateList)
2029 while (ic.can_node.childNodes.length > 0)
2030 ic.can_node.removeChild (ic.can_node.firstChild);
2031 var tr = document.createElement ('tr');
2032 var group = ic.candidates.CurrentGroup ();
2033 var td = document.createElement ('td');
2034 td.innerHTML = group[0][1] + '/' + group[0][0];
2035 td.style.color = 'white';
2036 td.style.backgroundColor = 'black';
2037 tr.appendChild (td);
2038 for (var i = 1; i < group.length; i++)
2040 var td = document.createElement ('td');
2042 td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
2043 if (i == group[0][2] + 1)
2044 td.style.backgroundColor = 'lightblue';
2045 tr.appendChild (td);
2047 ic.can_node.appendChild (tr);
2048 ic.can_node.style.top = (ic.abs_y + 10) + 'px';
2049 ic.can_node.style.left = ic.abs_x0 + 'px';
2053 var td = ic.can_node.firstElement ().firstElement ().nextElement ();
2054 var col = ic.candidates.CurrentCol ();
2055 for (var i = 0; td; td = td.nextElement ())
2056 td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
2058 ic.can_node.style.display = 'block';
2061 MIM.hide = function (ic)
2064 ic.can_node.style.display = 'none';
2067 function real_focus_in (target, ic)
2069 if (MIM.get_range (target, ic))
2070 ic.Filter (MIM.Key.FocusIn);
2071 MIM.update (target, ic, false);
2076 MIM.focus_in = function (event)
2078 var target = event.target;
2079 Xex.Log ("Focus in " + target.tagName);
2081 = setTimeout (function () {real_focus_in (target, target.mim_ic)} , 10);
2085 MIM.click = function (event)
2087 var target = event.target;
2088 Xex.Log ("Click in " + target.tagName);
2091 clearTimeout (focus_in_timer);
2092 focus_in_timer = null;
2094 real_focus_in (target, target.mim_ic);
2099 MIM.focus_out = function (event)
2101 var target = event.target;
2102 var ic = target.mim_ic;
2103 Xex.Log ("Focus out " + target.tagName);
2104 MIM.get_range (target, ic);
2105 MIM.debug_print (event, ic);
2106 ic.Filter (MIM.Key.FocusOut);
2107 MIM.update (target, ic, true);
2111 MIM.keydown = function (event)
2113 var target = event.target;
2114 if (! (target.type == "text" || target.type == "textarea"))
2117 var ic = target.mim_ic;
2118 if (! ic || ic.im != MIM.current)
2120 target.mim_ic = null;
2121 Xex.Log ('creating IC');
2122 ic = new MIM.IC (MIM.current, target);
2123 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2126 MIM.add_event_listener (target, 'focus', MIM.focus_in);
2127 MIM.add_event_listener (target, 'blur', MIM.focus_out);
2128 MIM.add_event_listener (target, 'click', MIM.click);
2130 MIM.get_range (target, ic)
2131 MIM.debug_print (event, ic);
2132 ic.key = MIM.decode_key_event (event);
2136 var result = ic.Filter (ic.key);
2138 Xex.Log ('Error' + e);
2141 MIM.update (target, ic, false);
2142 if (! ic.key_unhandled)
2143 event.preventDefault ();
2147 MIM.keypress = function (event)
2149 var target = event.target;
2150 if (! (target.type == "text" || target.type == "textarea"))
2153 var ic = target.mim_ic;
2157 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2160 ic.key = MIM.decode_key_event (event);
2168 var result = ic.Filter (ic.key);
2170 Xex.Log ('Error:' + e);
2173 MIM.update (target, ic, false);
2174 if (! ic.key_unhandled)
2175 event.preventDefault ();
2177 Xex.Log ("error:" + e);
2178 event.preventDefault ();
2180 MIM.debug_print (event, ic);
2187 var lang_category = {
2189 cs: { name: 'Czech' },
2190 da: { name: 'Danish' },
2191 el: { name: 'Greek' },
2192 en: { name: 'English' },
2193 eo: { name: 'Esperanto' },
2194 fr: { name: 'French' },
2195 grc: { name: 'ClassicGreek' },
2196 hr: { name: 'Croatian' },
2197 hy: { name: 'Armenian' },
2198 ka: { name: 'Georgian' },
2199 kk: { name: 'Kazakh' },
2200 ru: { name: 'Russian' },
2201 sk: { name: 'Slovak' },
2202 sr: { name: 'Serbian' },
2203 sv: { name: 'Swedish' },
2204 yi: { name: 'Yiddish' } },
2206 ar: { name: 'Arabic' },
2207 dv: { name: 'Divehi' },
2208 fa: { name: 'Persian' },
2209 he: { name: 'Hebrew' },
2210 kk: { name: 'Kazakh' },
2211 ps: { name: 'Pushto' },
2212 ug: { name: 'Uighur' },
2213 yi: { name: 'Yiddish' } },
2215 as: { name: 'Assamese' },
2216 bn: { name: 'Bengali' },
2217 bo: { name: 'Tibetan' },
2218 gu: { name: 'Gujarati' },
2219 hi: { name: 'Hindi' },
2220 kn: { name: 'Kannada' },
2221 ks: { name: 'Kashmiri' },
2222 ml: { name: 'Malayalam' },
2223 mr: { name: 'Marathi' },
2224 ne: { name: 'Nepali' },
2225 or: { name: 'Oriya' },
2226 pa: { name: 'Panjabi' },
2227 sa: { name: 'Sanskirit' },
2228 sd: { name: 'Sindhi' },
2229 si: { name: 'Sinhalese' },
2230 ta: { name: 'Tamil' },
2231 te: { name: 'Telugu' },
2232 ur: { name: 'Urdu' } },
2234 cmc: { name: 'Cham' },
2235 km: { name: 'Khmer'},
2236 lo: { name: 'Lao' },
2237 my: { name: 'Burmese' },
2238 tai: { name: 'Tai Viet' },
2239 th: { name: 'Thai' },
2240 vi: { name: 'Vietanamese' } },
2242 ii: { name: 'Yii' },
2243 ja: { name: 'Japanese' },
2244 ko: { name: 'Korean' },
2245 zh: { name: 'Chinese' } },
2247 am: { name: 'Amharic' },
2248 ath: { name: 'Carrier' },
2249 bla: { name: 'Blackfoot' },
2250 cr: { name: 'Cree' },
2251 eo: { name: 'Esperanto' },
2252 iu: { name: 'Inuktitut' },
2253 nsk: { name: 'Naskapi' },
2254 oj: { name: 'Ojibwe' },
2255 t: { name: 'Generic' } }
2258 function categorize_im ()
2260 var cat, lang, list, name;
2261 for (lang in MIM.imlist)
2264 for (cat in lang_category)
2265 if (lang_category[cat][lang])
2267 list = lang_category[cat][lang].list;
2269 list = lang_category[cat][lang].list = {};
2270 for (name in MIM.imlist[lang])
2271 list[name] = MIM.imlist[lang][name];
2274 for (name in MIM.imlist[lang])
2275 Xex.Log ('no category ' + lang + '-' + name);
2284 clearTimeout (destroy_timer);
2285 destroy_timer = null;
2286 var target = document.getElementById ('mim-menu');
2289 for (; last_target && last_target.menu_level;
2290 last_target = last_target.parentLi)
2291 last_target.style.backgroundColor = 'white';
2292 var nodes = target.getElementsByTagName ('ul');
2293 for (var i = 0; i < nodes.length; i++)
2294 nodes[i].style.visibility = 'hidden';
2295 nodes = target.getElementsByTagName ('pre');
2296 for (var i = 0; i < nodes.length; i++)
2297 nodes[i].style.visibility = 'hidden';
2298 document.getElementsByTagName ('body')[0].removeChild (target);
2302 function destroy_menu () {
2303 if (! destroy_timer)
2304 destroy_timer = setTimeout (destroy, 1000);
2308 function show_submenu (event)
2310 var target = event.target;
2311 if (! target.menu_level)
2313 if (! target.parentNode || ! target.parentNode.menu_level)
2315 target = target.parentNode;
2319 clearTimeout (destroy_timer);
2320 destroy_timer = null;
2322 if (last_target && target.parentLi != last_target)
2324 last_target.style.backgroundColor = 'white';
2325 while (target.menu_level < last_target.menu_level)
2327 last_target = last_target.parentLi;
2328 last_target.style.backgroundColor = 'white';
2330 var nodes = last_target.getElementsByTagName ('ul');
2331 for (var i = 0; i < nodes.length; i++)
2332 nodes[i].style.visibility = 'hidden';
2333 nodes = last_target.getElementsByTagName ('pre');
2334 for (var i = 0; i < nodes.length; i++)
2335 nodes[i].style.visibility = 'hidden';
2337 last_target = target;
2338 target.style.backgroundColor = 'yellow';
2339 if (target.menu_level < 3)
2343 target.lastChild.style.visibility = 'visible';
2344 target.lastChild.style.left = target.clientWidth + 'px';
2348 var left = target.clientWidth;
2349 for (var n = target.firstElement ().nextElement (); n; n = n.nextElement ())
2351 n.style.visibility = 'visible';
2352 n.style.left = left + 'px';
2353 left += n.clientWidth;
2357 event.preventDefault ();
2360 function select_im (event)
2362 var target = event.target;
2365 if (! target.parentNode || ! target.parentNode.menu_level)
2367 event.preventDefault ();
2370 target = target.parentNode;
2374 MIM.current = target.im;
2377 event.preventDefault ();
2380 function create_ul (visibility)
2382 var ul = document.createElement ('ul');
2384 ul.style.position = 'absolute';
2385 ul.style.margin = '0px';
2386 ul.style.padding = '0px';
2387 ul.style.border = '1px solid gray';
2388 ul.style.borderBottom = 'none';
2389 ul.style.top = '-1px';
2390 ul.style.backgroundColor = 'white';
2391 ul.style.visibility = visibility;
2395 function create_li (level, text)
2397 var li = document.createElement ('li');
2398 li.style.position = 'relative';
2399 li.style.margin = '0px';
2400 li.style.padding = '1px';
2401 li.style.borderBottom = '1px solid gray';
2402 li.style.top = '0px';
2403 li.style.listStyle = 'none';
2404 li.menu_level = level;
2405 var nobr = document.createElement ('nobr');
2406 nobr.innerHTML = text;
2407 li.appendChild (nobr);
2411 function create_sep ()
2413 var sep = document.createElement ('pre');
2415 sep.innerHTML = ' ';
2416 sep.style.position = 'absolute';
2417 sep.style.margin = '0px';
2418 sep.style.padding = '2px';
2419 sep.style.border = '2px solid black';
2420 sep.style.top = '-1px';
2421 sep.style.backgroundColor = 'black';
2422 sep.style.visibility = 'hidden';
2428 function create_menu (event)
2430 var target = event.target;
2432 if (! ((target.type == "text" || target.type == "textarea")
2433 && event.which == 1 && event.ctrlKey))
2438 menu = create_ul ('visible');
2439 menu.style.fontFamily = 'sans-serif';
2440 menu.style.fontWeight = 'bold';
2441 menu.id = 'mim-menu';
2442 menu.onmousedown = select_im;
2443 menu.onmouseover = show_submenu;
2444 menu.onmouseout = destroy_menu;
2445 for (var catname in lang_category)
2447 var cat = lang_category[catname];
2448 var li = create_li (1, catname);
2449 li.appendChild (create_sep ());
2450 var sub = create_ul ('hidden');
2451 for (var langname in cat)
2453 var lang = cat[langname];
2456 var sub_li = create_li (2, lang.name);
2457 sub_li.parentLi = li;
2458 sub_li.appendChild (create_sep ());
2459 var subsub = create_ul ('hidden');
2460 for (var name in lang.list)
2462 var im = lang.list[name];
2463 var subsub_li = create_li (3, im.name);
2464 subsub_li.parentLi = sub_li;
2466 subsub.appendChild (subsub_li);
2468 sub_li.appendChild (subsub);
2469 sub.appendChild (sub_li);
2471 li.appendChild (sub);
2472 menu.appendChild (li);
2474 lang_category = null;
2476 menu.style.left = (event.clientX - 10) + "px";
2477 menu.style.top = (event.clientY - 10) + "px";
2478 document.getElementsByTagName ('body')[0].appendChild (menu);
2481 MIM.init = function ()
2483 MIM.add_event_listener (window, 'keydown', MIM.keydown);
2484 MIM.add_event_listener (window, 'keypress', MIM.keypress);
2485 MIM.add_event_listener (window, 'mousedown', create_menu);
2486 Xex.Load (MIM.server, "imlist.xml", MIM.create_list);