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 // * MIM.get_preedit_pos () returns incorrect position sometimes.
30 // URL of the input method server.
31 server: "http://www.m17n.org/common/mim-js",
32 // Boolean flag to tell if MIM is active or not.
34 // Boolean flag to tell if MIM is running in debug mode or not.
36 // List of main input methods.
38 // List of extra input methods;
40 // Global input method data
42 // Currently selected input method.
46 LoadStatus: { NotLoaded:0, Loading:1, Loaded:2, Error:-1 },
55 Preedit: 0x06, // PreeditText | CursorPos
56 Candidate: 0x38 // CandidateList | CandidateIndex | CandidateShow
78 ParseError: "parse-error"
82 //if (window.location.hostname == 'localhost')
83 // MIM.server = 'http://localhost/mim';
86 var keysyms = new Array ();
87 keysyms["bs"] = "BackSpace";
88 keysyms["lf"] = "Linefeed";
89 keysyms["cr"] = keysyms["enter"] = "Return";
90 keysyms["esc"] = "Escape";
91 keysyms["spc"] = "space";
92 keysyms["del"] = "Delete";
94 function decode_keysym (str) {
97 var parts = str.split ("-");
98 var len = parts.length, i;
99 var has_modifier = len > 1;
101 for (i = 0; i < len - 1; i++)
102 if (! MIM.KeyModifier.hasOwnProperty (parts[i]))
104 var key = parts[len - 1];
107 key = keysyms[key.toLowerCase ()];
113 for (i = 1; i < len - 1; i++)
114 str += '-' + parts[i];
123 parts = new Array ();
130 MIM.Key = function (val)
133 if (val instanceof Xex.Term)
135 else if (typeof val == 'string' || val instanceof String)
137 this.key = decode_keysym (val);
139 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
140 if (this.key instanceof Array)
142 this.key = this.key[0];
143 this.has_modifier = true;
146 else if (typeof val == 'number' || val instanceof Number)
147 this.key = String.fromCharCode (val);
149 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + val);
152 MIM.Key.prototype.toString = function () { return this.key; };
154 MIM.Key.FocusIn = new MIM.Key (new Xex.StrTerm ('input-focus-in'));
155 MIM.Key.FocusOut = new MIM.Key (new Xex.StrTerm ('input-focus-out'));
156 MIM.Key.FocusMove = new MIM.Key (new Xex.StrTerm ('input-focus-move'));
160 MIM.KeySeq = function (seq)
162 this.val = new Array ();
168 var len = seq.val.length;
169 for (var i = 0; i < len; i++)
171 var v = seq.val[i], key;
172 if (v.type == 'symbol' || v.type == 'string')
173 key = new MIM.Key (v);
174 else if (v.type == 'integer')
175 key = new MIM.Key (v.val);
177 throw new Xex.ErrTerm (MIM.Error.ParseError,
178 "Invalid key: " + v);
180 if (key.has_modifier)
181 this.has_modifier = true;
186 var len = seq.val.length;
187 for (var i = 0; i < len; i++)
188 this.val.push (new MIM.Key (seq.val.charCodeAt (i)));
191 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid key: " + seq);
195 var proto = new Xex.Term ('keyseq');
196 proto.Clone = function () { return this; }
197 proto.Parser = function (domain, node)
199 var seq = new Array ();
200 for (node = node.firstChild; node; node = node.nextSibling)
201 if (node.nodeType == 1)
203 var term = Xex.Term.Parse (domain, node);
204 return new MIM.KeySeq (term);
206 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid keyseq");
208 proto.toString = function ()
210 var len = this.val.length;
214 var str = '<keyseq>';
215 for (var i = 0; i < len; i++)
219 else if (this.has_modifier)
221 str += this.val[i].toString ();
223 return str + '</keyseq>';
226 MIM.KeySeq.prototype = proto;
230 MIM.Marker = function () { }
231 MIM.Marker.prototype = new Xex.Term ('marker');
232 MIM.Marker.prototype.CharAt = function (ic)
234 var p = this.Position (ic);
235 if (p < 0 || p >= ic.preedit.length)
237 return ic.preedit.charCodeAt (p);
240 MIM.FloatingMarker = function (name) { this.val = name; };
241 var proto = new MIM.Marker ();
242 MIM.FloatingMarker.prototype = proto;
243 proto.Position = function (ic) { return ic.marker_positions[this.val]; };
244 proto.Mark = function (ic) { ic.marker_positions[this.val] = ic.cursor_pos; };
246 MIM.PredefinedMarker = function (name) { this.val = name; }
247 MIM.PredefinedMarker.prototype = new MIM.Marker ();
248 MIM.PredefinedMarker.prototype.Position = function (ic)
250 if (typeof this.pos == 'number')
252 return this.pos (ic);
257 function def_predefined (name, position)
259 predefined[name] = new MIM.PredefinedMarker (name);
260 predefined[name].pos = position;
263 def_predefined ('@<', 0);
264 def_predefined ('@>', function (ic) { return ic.preedit.length; });
265 def_predefined ('@-', function (ic) { return ic.cursor_pos - 1; });
266 def_predefined ('@+', function (ic) { return ic.cursor_pos + 1; });
267 def_predefined ('@[', function (ic) {
268 if (ic.cursor_pos > 0)
270 var pos = ic.cursor_pos;
271 return ic.preedit.FindProp ('candidates', pos - 1).from;
275 def_predefined ('@]', function (ic) {
276 if (ic.cursor_pos < ic.preedit.length - 1)
278 var pos = ic.cursor_pos;
279 return ic.preedit.FindProp ('candidates', pos).to;
281 return ic.preedit.length;
283 for (var i = 0; i < 10; i++)
284 def_predefined ("@" + i, i);
285 predefined['@first'] = predefined['@<'];
286 predefined['@last'] = predefined['@>'];
287 predefined['@previous'] = predefined['@-'];
288 predefined['@next'] = predefined['@+'];
289 predefined['@previous-candidate-change'] = predefined['@['];
290 predefined['@next-candidate-change'] = predefined['@]'];
292 MIM.SurroundMarker = function (name)
295 this.distance = parseInt (name.slice (1));
296 if (isNaN (this.distance))
297 throw new Xex.ErrTerm (MIM.Error.ParseError, "Invalid marker: " + name);
299 MIM.SurroundMarker.prototype = new MIM.Marker ();
300 MIM.SurroundMarker.prototype.Position = function (ic)
302 return ic.cursor_pos + this.distance;
304 MIM.SurroundMarker.prototype.CharAt = function (ic)
306 if (this.val == '@-0')
308 var p = this.Position (ic);
310 return ic.GetSurroundingChar (p);
311 else if (p >= ic.preedit.length)
312 return ic.GetSurroundingChar (p - ic.preedit.length);
313 return ic.preedit.charCodeAt (p);
316 MIM.Marker.prototype.Parser = function (domain, node)
318 var name = node.firstChild.nodeValue;
319 if (name.charAt (0) == '@')
321 var n = predefined[name];
324 if (name.charAt (1) == '-' || name.charAt (1) == '+')
325 return new MIM.SurroundMarker (name);
326 throw new Xex.ErrTerm (MIM.Error.ParseError,
327 "Invalid marker: " + name);
329 return new MIM.FloatingMarker (name);;
333 MIM.Selector = function (name)
337 MIM.Selector.prototype = new Xex.Term ('selector');
341 selectors["@<"] = selectors["@first"] = new MIM.Selector ('@<');
342 selectors["@="] = selectors["@current"] = new MIM.Selector ('@=');
343 selectors["@>"] = selectors["@last"] = new MIM.Selector ('@>');
344 selectors["@-"] = selectors["@previous"] = new MIM.Selector ('@-');
345 selectors["@+"] = selectors["@next"] = new MIM.Selector ('@+');
346 selectors["@["] = selectors["@previous-group"] = new MIM.Selector ('@[');
347 selectors["@]"] = selectors["@next-group"] = new MIM.Selector ('@]');
349 MIM.Selector.prototype.Parser = function (domain, node)
351 var name = node.firstChild.nodeValue;
352 var s = selectors[name];
354 throw new Xex.ErrTerm (MIM.Error.ParseError,
355 "Invalid selector: " + name);
360 MIM.Rule = function (keyseq, actions)
362 this.keyseq = keyseq;
364 this.actions = actions;
366 MIM.Rule.prototype = new Xex.Term ('rule');
367 MIM.Rule.prototype.Parser = function (domain, node)
370 for (n = node.firstChild; n && n.nodeType != 1; n = n.nextSibling);
372 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
373 var keyseq = Xex.Term.Parse (domain, n);
374 if (keyseq.type != 'keyseq')
375 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid rule:" + node);
377 n = n.nextElement ();
379 actions = Xex.Term.Parse (domain, n, null);
380 return new MIM.Rule (keyseq, actions);
382 MIM.Rule.prototype.toString = function ()
387 MIM.Map = function (name)
390 this.rules = new Array ();
394 var proto = new Xex.Term ('map');
396 proto.Parser = function (domain, node)
398 var name = node.attributes['mname'].nodeValue;
400 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
401 var map = new MIM.Map (name);
402 for (var n = node.firstChild; n; n = n.nextSibling)
404 map.rules.push (Xex.Term.Parse (domain, n));
408 proto.toString = function ()
410 var str = '<map mname="' + this.name + '">';
411 var len = this.rules.length;
412 for (i = 0; i < len; i++)
413 str += this.rules[i];
414 return str + '</map>';
417 MIM.Map.prototype = proto;
420 Xex.CatchTag._mimtag = new Xex.SymTerm ('@mimtag');
422 MIM.Action = function (domain, terms)
424 var args = new Array ();
425 args.push (Xex.CatchTag_.mimtag);
426 for (var i = 0; i < terms.length; i++)
427 args.push (terms[i]);
428 this.action = Xex.Funcall.prototype.New (domain, 'catch', null, args);
431 MIM.Action.prototype.Run = function (domain)
433 var result = this.action.Eval (domain);
434 if (result.type == 'error')
436 domain.context.Error = result.toString ();
439 return (result != Xex.CatchTag._mimtag);
442 MIM.Keymap = function ()
451 function add_rule (keymap, rule, branch_actions)
453 var keyseq = rule.keyseq;
454 var len = keyseq.val.length;
457 for (var i = 0; i < len; i++)
459 var key = keyseq.val[i];
463 if (! keymap.submaps)
466 sub = keymap.submaps[key.key];
468 keymap.submaps[key.key] = sub = new MIM.Keymap ();
473 keymap.map_actions = rule.actions;
475 keymap.branch_actions = branch_actions;
478 proto.Add = function (map, branch_actions)
480 var rules = map.rules;
481 var len = rules.length;
483 for (var i = 0; i < len; i++)
484 add_rule (this, rules[i], branch_actions);
486 proto.Lookup = function (keys, index)
490 if (index < keys.val.length && this.submaps
491 && ! keys.val[index])
493 Xex.Log ('invalid key at ' + index);
497 if (index < keys.val.length && this.submaps
498 && (sub = this.submaps[keys.val[index].key]))
501 return sub.Lookup (keys, index);
503 return { map: this, index: index };
506 MIM.Keymap.prototype = proto;
509 MIM.State = function (name)
512 this.keymap = new MIM.Keymap ();
516 var proto = new Xex.Term ('state');
518 proto.Parser = function (domain, node)
520 var map_list = domain.map_list;
521 var name = node.attributes['sname'].nodeValue;
523 throw new Xex.ErrTerm (MIM.Error.ParseError, "invalid map");
524 var state = new MIM.State (name);
525 for (node = node.firstElement (); node; node = node.nextElement ())
527 if (node.nodeName == 'title')
528 state.title = node.firstChild.nodeValue;
531 var n = node.firstElement ();
532 var branch_actions = n ? Xex.Term.Parse (domain, n, null) : null;
533 if (node.nodeName == 'branch')
534 state.keymap.Add (map_list[node.attributes['mname'].nodeValue],
536 else if (node.nodeName == 'state-hook')
537 state.keymap.map_actions = branch_actions;
538 else if (node.nodeName == 'catch-all-branch')
539 state.keymap.branch_actions = branch_actions;
545 proto.toString = function ()
547 return '<state sname="' + this.name + '">' + this.keymap + '</state>';
550 MIM.State.prototype = proto;
554 function Block (index, term)
558 this.Data = term.val;
559 else if (term.IsList)
561 this.Data = new Array ();
562 for (var i = 0; i < term.val.length; i++)
563 this.Data.push (term.val[i].val);
567 Block.prototype.Count = function () { return this.Data.length; }
568 Block.prototype.get = function (i)
570 return (this.Data instanceof Array ? this.Data[i] : this.Data.charAt (i));
573 MIM.Candidates = function (ic, candidates, column)
576 this.column = column;
580 this.blocks = new Array ();
582 for (var i = 0; i < candidates.length; i++)
584 var block = new Block (this.total, candidates[i]);
585 this.blocks.push (block);
586 this.total += block.Count ();
592 return (this.column > 0 ? this.index % this.column
593 : this.index - this.blocks[this.row].Index);
596 function prev_group ()
598 var col = get_col.call (this);
602 this.index -= this.column;
604 nitems = this.column;
607 var lastcol = (this.total - 1) % this.column;
608 this.index = (col < lastcol ? this.total - lastcol + col
610 this.row = this.blocks.length - 1;
611 nitems = lastcol + 1;
613 while (this.blocks[this.row].Index > this.index)
618 this.row = this.row > 0 ? this.row - 1 : this.blocks.length - 1;
619 nitems = this.blocks[this.row].Count ();
620 this.index = (this.blocks[this.row].Index
621 + (col < nitems ? col : nitems - 1));
626 function next_group ()
628 var col = get_col.call (this);
632 this.index += this.column - col;
633 if (this.index < this.total)
635 if (this.index + col >= this.total)
637 nitems = this.total - this.index;
638 this.index = this.total - 1;
642 nitems = this.column;
651 while (this.blocks[this.row].Index + this.blocks[this.row].Count ()
657 this.row = this.row < this.blocks.length - 1 ? this.row + 1 : 0;
658 nitems = this.blocks[this.row].Count ();
659 this.index = (this.blocks[this.row].Index
660 + (col < nitems ? col : nitems - 1));
669 this.index = this.total - 1;
670 this.row = this.blocks.length - 1;
675 if (this.blocks[this.row].Index > this.index)
683 if (this.index == this.total)
690 var b = this.blocks[this.row];
691 if (this.index == b.Index + b.Count ())
698 this.index -= get_col.call (this);
699 while (this.blocks[this.row].Index > this.index)
705 var b = this.blocks[this.row];
708 if (this.index + 1 < this.total)
710 this.index += this.column - get_col.call (this) + 1;
711 while (b.Index + b.Count () <= this.index)
712 b = this.blocks[++this.row];
716 this.index = b.Index + b.Count () - 1;
719 MIM.Candidates.prototype.Current = function ()
721 var b = this.blocks[this.row];
722 return b.get (this.index - b.Index);
725 MIM.Candidates.prototype.Select = function (selector)
727 var idx = this.index;
728 var gidx = this.column > 0 ? idx / this.column : this.row;
729 if (selector.type == 'selector')
731 switch (selector.val)
733 case '@<': first.call (this); break;
734 case '@>': last.call (this); break;
735 case '@-': prev.call (this); break;
736 case '@+': next.call (this); break;
737 case '@[': prev_group.call (this); break;
738 case '@]': next_group.call (this); break;
747 col = this.index % this.column;
748 start = this.index - col;
749 end = start + this.column;
753 start = this.blocks[this.row].Index;
754 col = this.index - start;
755 end = start + this.blocks[this.row].Count;
757 if (end > this.total)
759 this.index += selector.val - col;
760 if (this.index >= end)
761 this.index = end - 1;
764 if (selector.val > col)
765 while (this.blocks[this.row].Index + this.blocks[this.row].Count
769 while (this.blocks[this.row].Index > this.index)
773 var newgidx = this.column > 0 ? this.index / this.column : this.row;
774 if (this.index != idx)
775 this.ic.changed |= (gidx == newgidx
776 ? MIM.ChangedStatus.CandidateIndex
777 : MIM.ChangedStatus.CandidateList);
778 return this.Current ();
781 MIM.Candidates.prototype.CurrentCol = function ()
783 return get_col.call (this);
786 MIM.Candidates.prototype.CurrentGroup = function ()
788 var col, start, end, gnum, gidx;
791 gnum = Math.floor ((this.total - 1) / this.column) + 1;
792 col = this.index % this.column;
793 start = this.index - col;
794 gidx = start / this.column + 1;
795 end = start + this.column;
796 if (end > this.total)
801 gnum = this.blocks.length;
803 start = this.blocks[this.row].Index;
804 col = this.index - start;
805 end = start + this.blocks[this.row].Count ();
807 var group = new Array ();
808 var indices = new Array (gnum, gidx, col);
809 group.push (indices);
811 var block = this.blocks[row++];
814 var c = block.get (start - block.Index);
817 if (start == block.Index + block.Count ())
818 block = this.blocks[row++];
824 MIM.im_domain = new Xex.Domain ('input-method', null, null);
825 MIM.im_domain.DefType (MIM.KeySeq.prototype);
826 MIM.im_domain.DefType (MIM.Marker.prototype);
827 MIM.im_domain.DefType (MIM.Selector.prototype);
828 MIM.im_domain.DefType (MIM.Rule.prototype);
829 MIM.im_domain.DefType (MIM.Map.prototype);
830 MIM.im_domain.DefType (MIM.State.prototype);
833 var im_domain = MIM.im_domain;
835 function Finsert (domain, vari, args)
838 if (args[0].type == 'integer')
839 text = String.fromCharCode (args[0].val);
842 domain.context.ins (text, null);
846 function Finsert_candidates (domain, vari, args)
848 var ic = domain.context;
849 var gsize = domain.variables['candidates-group-size'];
850 var candidates = new MIM.Candidates (ic, args,
851 gsize ? gsize.val.Intval () : 0);
852 ic.ins (candidates.Current (), candidates);
856 function Fdelete (domain, vari, args)
858 var ic = domain.context;
859 var pos = args[0].IsInt ? args[0].Intval () : args[0].Position (ic);
860 return new Xex.IntTerm (ic.del (pos));
863 function Fselect (domain, vari, args)
865 var ic = domain.context;
866 var can = ic.candidates;
870 var old_text = can.Current ();
871 var new_text = can.Select (args[0]);
872 ic.rep (old_text, new_text, can);
875 Xex.Log ('no candidates at ' + ic.cursor_pos + ' of ' + ic.candidate_table.table.length);
879 function Fshow (domain, vari, args)
881 domain.context.candidate_show = true;
882 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
886 function Fhide (domain, vari, args)
888 domain.context.candidate_show = false;
889 domain.context.changed |= MIM.ChangedStatus.CandidateShow;
893 function Fchar_at (domain, vari, args)
895 return new Xex.IntTerm (args[0].CharAt (domain.context));
898 function Fmove (domain, vari, args)
900 var ic = domain.context;
901 var pos = args[0].IsInt ? args[0].val : args[0].Position (ic);
903 return new Xex.IntTerm (pos);
906 function Fmark (domain, vari, args)
908 args[0].Mark (domain.context);
912 function Fpushback (domain, vari, args)
914 var a = (args[0].IsInt ? args[0].Intval ()
915 : args[0].IsStr ? new KeySeq (args[0])
917 domain.context.pushback (a);
921 function Fpop (domain, vari, args)
923 var ic = domain.context;
924 if (ic.key_head < ic.keys.val.length)
925 ic.keys.val.splice (ic.keys_head, 1);
929 function Fundo (domain, vari, args)
931 var ic = domain.context;
932 var n = args.length == 0 ? -2 : args[0].val;
933 Xex.Log ('undo with arg ' + args[0]);
935 ic.keys.val.splice (ic.keys.val.length + n, -n);
937 ic.keys.val.splice (n, ic.keys.val.length);
942 function Fcommit (domain, vari, args)
944 domain.context.commit ();
948 function Funhandle (domain, vari, args)
950 domain.context.commit ();
951 return Xex.Fthrow (domain, vari, Xex.CatchTag._mimtag);
954 function Fshift (domain, vari, args)
956 var ic = domain.context;
957 var state_name = args[0].val;
958 var state = ic.im.state_list[state_name];
960 throw ("Unknown state: " + state_name);
965 function Fshiftback (domain, vari, args)
967 domain.context.shift (null);
971 function Fkey_count (domain, vari, args)
973 return new Xex.IntTerm (domain.context.key_head);
976 function Fsurrounding_flag (domain, vari, args)
978 return new Xex.IntTerm (-1);
981 im_domain.DefSubr (Finsert, "insert", false, 1, 1);
982 im_domain.DefSubr (Finsert_candidates, "insert-candidates", false, 1, 1);
983 im_domain.DefSubr (Fdelete, "delete", false, 1, 1);
984 im_domain.DefSubr (Fselect, "select", false, 1, 1);
985 im_domain.DefSubr (Fshow, "show-candidates", false, 0, 0);
986 im_domain.DefSubr (Fhide, "hide-candidates", false, 0, 0);
987 im_domain.DefSubr (Fmove, "move", false, 1, 1);
988 im_domain.DefSubr (Fmark, "mark", false, 1, 1);
989 im_domain.DefSubr (Fpushback, "pushback", false, 1, 1);
990 im_domain.DefSubr (Fpop, "pop", false, 0, 0);
991 im_domain.DefSubr (Fundo, "undo", false, 0, 1);
992 im_domain.DefSubr (Fcommit, "commit", false, 0, 0);
993 im_domain.DefSubr (Funhandle, "unhandle", false, 0, 0);
994 im_domain.DefSubr (Fshift, "shift", false, 1, 1);
995 im_domain.DefSubr (Fshiftback, "shiftback", false, 0, 0);
996 im_domain.DefSubr (Fchar_at, "char-at", false, 1, 1);
997 im_domain.DefSubr (Fkey_count, "key-count", false, 0, 0);
998 im_domain.DefSubr (Fsurrounding_flag, "surrounding-text-flag", false, 0, 0);
1003 function get_global_var (vname)
1005 if (MIM.im_global.load_status == MIM.LoadStatus.NotLoaded)
1006 MIM.im_global.Load ()
1007 return MIM.im_global.domain.variables[vname];
1010 function include (node)
1012 node = node.firstElement ();
1013 if (node.nodeName != 'tags')
1016 var lang = null, name = null, extra = null, im;
1017 for (node = node.firstElement (); node; node = node.nextElement ())
1019 if (node.nodeName == 'language')
1020 lang = node.firstChild.nodeValue;
1021 else if (node.nodeName == 'name')
1022 name = node.firstChild.nodeValue;
1023 else if (node.nodeName == 'extra-id')
1024 extra = node.firstChild.nodeValue;
1026 if (! lang || ! MIM.imlist[lang])
1030 if (! name || ! (im = MIM.imlist[lang][name]))
1035 if (! (im = MIM.imextra[lang][extra]))
1038 if (im.load_status != MIM.LoadStatus.Loaded)
1040 if (im.load_status == MIM.LoadStatus.NotLoaded)
1042 if (im.load_status != MIM.LoadStatus.Loading)
1050 parsers['description'] = function (node)
1052 this.description = node.firstChild.nodeValue;
1055 parsers['variable-list'] = function (node)
1057 for (node = node.firstElement (); node; node = node.nextElement ())
1059 var vname = node.attributes['vname'].nodeValue;
1060 if (this != MIM.im_global)
1062 var vari = get_global_var (vname);
1064 this.domain.Defvar (vname, vari.desc, vari.val, vari.range);
1066 vname = Xex.Term.Parse (this.domain, node)
1070 parsers['command-list'] = function (node)
1074 parsers['macro-list'] = function (node)
1076 for (var n = node.firstElement (); n; n = n.nextElement ())
1077 if (n.nodeName == 'xi:include')
1079 var im = include (n);
1082 alert ('inclusion fail');
1083 throw new Xex.ErrTerm (MIM.Error.ParseError, "inclusion fail: ");
1085 if (im.load_status == MIM.LoadStatus.Loading)
1086 return false; // force reloading
1087 for (var macro in im.domain.functions)
1089 var func = im.domain.functions[macro];
1090 if (func instanceof Xex.Macro)
1091 im.domain.CopyFunc (this.domain, macro);
1093 n = n.previousSibling;
1094 node.removeChild (n.nextSibling);
1096 Xex.Term.Parse (this.domain, node.firstElement (), null);
1099 parsers['title'] = function (node)
1101 this.title = node.firstChild.nodeValue;
1104 parsers['map-list'] = function (node)
1106 for (node = node.firstElement (); node; node = node.nextElement ())
1108 if (node.nodeName == 'xi:include')
1110 var im = include (node);
1113 alert ('inclusion fail');
1114 throw new Xex.ErrTerm (MIM.Error.ParseError, "inclusion fail: ");
1116 else if (im.load_status == MIM.LoadStatus.Loading)
1118 for (var mname in im.map_list)
1119 this.map_list[mname] = im.map_list[mname];
1123 var map = Xex.Term.Parse (this.domain, node);
1124 this.map_list[map.name] = map;
1129 parsers['state-list'] = function (node)
1131 this.domain.map_list = this.map_list;
1132 for (node = node.firstElement (); node; node = node.nextElement ())
1134 if (node.nodeName == 'xi:include')
1136 var im = include (node);
1139 alert ('inclusion fail');
1140 throw new Xex.ErrTerm (MIM.Error.ParseError, "inclusion fail: ");
1142 else if (im.load_status == MIM.LoadStatus.Loading)
1144 for (var sname in im.state_list)
1146 state = im.state_list[sname];
1147 if (! this.initial_state)
1148 this.initial_state = state;
1149 this.state_list[sname] = state;
1152 else if (node.nodeName == 'state')
1154 var state = Xex.Term.Parse (this.domain, node);
1156 state.title = this.title;
1157 if (! this.initial_state)
1158 this.initial_state = state;
1159 this.state_list[state.name] = state;
1162 delete this.domain.map_list;
1166 MIM.IM = function (lang, name, extra_id, file)
1170 this.extra_id = extra_id;
1172 this.load_status = MIM.LoadStatus.NotLoaded;
1173 this.domain = new Xex.Domain (this.lang + '-'
1174 + (this.name != 'nil'
1175 ? this.name : this.extra_id),
1176 MIM.im_domain, null);
1179 function load_im (node, im)
1181 //alert ('Loading IM (' + im + ':' + im.lang + '-' + im.name + ')');
1183 im.initial_state = null;
1185 for (node = node.firstElement (); node; node = node.nextElement ())
1187 var name = node.nodeName;
1188 var parser = parsers[name];
1189 if (parser && ! parser.call (im, node))
1195 //alert ('initial state = ' + im.initial_state);
1196 im.load_status = MIM.LoadStatus.Loaded;
1199 MIM.IM.prototype = {
1202 this.load_status = MIM.LoadStatus.Loading;
1203 Xex.Load (MIM.server, this.file, load_im, this);
1207 MIM.IC = function (im, target)
1209 if (im.load_status == MIM.LoadStatus.NotLoaded)
1211 if (im.load_status != MIM.LoadStatus.Loaded)
1212 alert ('im:' + im.name + ' error:' + im.load_status);
1214 this.target = target;
1215 this.domain = new Xex.Domain ('context', im.domain, this);
1217 this.range = new Array ();
1218 this.range[0] = this.range[1] = 0;
1220 this.initial_state = this.im.initial_state;
1221 this.keys = new MIM.KeySeq ();
1222 this.marker_positions = new Array ();
1223 this.candidate_table = new MIM.CandidateTable ();
1227 MIM.CandidateTable = function ()
1229 this.table = new Array ();
1232 MIM.CandidateTable.prototype.get = function (pos)
1234 for (var i = 0; i < this.table.length; i++)
1236 var elt = this.table[i];
1237 if (elt.from < pos && pos <= elt.to)
1242 MIM.CandidateTable.prototype.put = function (from, to, candidates)
1244 for (var i = 0; i < this.table.length; i++)
1246 var elt = this.table[i];
1247 if (elt.from < to && elt.to > from)
1251 elt.val = candidates;
1255 this.table.push ({ from: from, to: to, val: candidates });
1258 MIM.CandidateTable.prototype.adjust = function (from, to, inserted)
1260 var diff = inserted - (to - from);
1263 for (var i = 0; i < this.table.length; i++)
1265 var elt = this.table[i];
1274 MIM.CandidateTable.prototype.clear = function ()
1276 this.table.length = 0;
1279 function set_cursor (prefix, pos)
1281 this.cursor_pos = pos;
1282 var candidates = this.candidate_table.get (pos);
1283 if (this.candidates != candidates)
1285 this.candidates = candidates;
1286 this.changed |= MIM.ChangedStatus.CandidateList;
1290 function save_state ()
1292 this.state_var_values = this.domain.SaveValues ();
1293 this.state_preedit = this.preedit;
1294 this.state_key_head = this.key_head;
1295 this.state_pos = this.cursor_pos;
1298 function restore_state ()
1300 this.domain.RestoreValues (this.state_var_values);
1301 this.preedit = this.state_preedit;
1302 set_cursor.call (this, "restore", this.state_pos);
1305 function handle_key ()
1307 Xex.Log ('Key(' + this.key_head + ') "' + this.keys.val[this.key_head]
1308 + '" in ' + this.state.name + ':' + this.keymap.name
1309 + " key/state/commit-head/len:"
1310 + this.key_head + '/' + this.state_key_head + '/' + this.commit_key_head + '/' + this.keys.val.length);
1311 var out = this.state.keymap.Lookup (this.keys, this.state_key_head);
1314 if (out.index > this.key_head)
1316 this.key_head = out.index;
1317 Xex.Log (' with submap', -1);
1318 restore_state.call (this);
1320 if (sub.map_actions)
1322 Xex.Log ('taking map actions:');
1323 if (! this.take_actions (sub.map_actions))
1326 else if (sub.submaps)
1328 Xex.Log ('no map actions');
1329 for (var i = this.state_key_head; i < this.key_head; i++)
1331 Xex.Log ('inserting key:' + this.keys.val[i].key);
1332 this.ins (this.keys.val[i].key, null);
1337 Xex.Log ('terminal:');
1338 if (this.keymap.branch_actions)
1340 Xex.Log ('branch actions:');
1341 if (! this.take_actions (this.keymap.branch_actions))
1344 if (sub != this.state.keymap)
1345 this.shift (this.state);
1350 Xex.Log (' without submap', -1);
1352 var current_state = this.state;
1353 var map = this.keymap;
1355 if (map.branch_actions)
1357 Xex.Log ('branch actions:');
1358 if (! this.take_actions (map.branch_actions))
1362 if (map == this.keymap)
1364 Xex.Log ('no state change');
1365 if (map == this.initial_state.keymap
1366 && this.key_head < this.keys.val.length)
1368 Xex.Log ('unhandled');
1371 if (map != current_state.keymap)
1372 this.shift (current_state);
1373 else if (this.keymap.actions == null)
1374 this.shift (this.initial_state);
1380 MIM.IC.prototype = {
1381 reset: function (clear_keys)
1383 this.cursor_pos = 0;
1384 this.prev_state = null;
1385 this.title = this.initial_state.title;
1386 this.state_preedit = '';
1387 this.state_key_head = 0;
1388 this.state_var_values = {};
1392 this.keys.val.length = 0;
1393 this.commit_key_head = 0;
1394 this.key_unhandled = false;
1395 this.unhandled_key = null;
1396 this.changed = MIM.ChangedStatus.None;
1397 this.error_message = '';
1398 this.title = this.initial_state.title;
1401 this.preedit_saved = '';
1402 if (this.candidate_show)
1404 this.candidate_table.clear ();
1405 this.candidates = null;
1406 this.candidate_show = false;
1407 for (var elt in this.marker_positions)
1408 this.marker_positions[elt] = 0;
1409 this.shift (this.initial_state);
1412 catch_args: new Array (Xex.CatchTag._mimtag, null),
1414 take_actions: function (actions)
1416 if (actions.length == 0)
1418 var func_progn = this.domain.GetFunc ('progn');
1419 var func_catch = this.domain.GetFunc ('catch');
1420 this.catch_args[1] = new Xex.Funcall (func_progn, null, actions);
1421 var term = new Xex.Funcall (func_catch, null, this.catch_args);
1422 term = term.Eval (this.domain);
1423 return (! term.IsSymbol || term.val != '@mimtag');
1426 GetSurroundingChar: function (pos)
1430 pos += this.range[0];
1436 pos += this.range[1];
1437 if (pos >= this.target.value.length)
1440 return this.target.value.charCodeAt (pos);
1443 DelSurroundText: function (pos)
1448 pos += this.range[0];
1454 text = this.target.value.substring (0, pos);
1455 if (this.range[0] < this.target.value.length)
1456 text += this.target.value.substring (this.range[0]);
1457 this.target.value = text;
1458 this.range[1] -= this.range[0] - pos;
1459 this.range[0] = pos;
1463 pos += this.range[1];
1464 text = this.target.value.substring (0, this.range[1]);
1465 if (pos >= this.target.value.length)
1466 pos = this.target.value.length;
1468 text += this.target.value.substring (pos);
1469 this.target.value = text;
1473 adjust_markers: function (from, to, inserted)
1475 var diff = inserted - (to - from);
1477 for (var name in this.marker_positions)
1479 var pos = this.marker_positions[name];
1483 this.marker_positions[name] += diff;
1484 else if (pos > from)
1485 this.marker_positions[name] = from;
1490 preedit_replace: function (from, to, text, candidates)
1492 var newlen = text.length;
1493 this.preedit = (this.preedit.substring (0, from)
1494 + text + this.preedit.substring (to));
1495 this.changed |= MIM.ChangedStatus.Preedit | MIM.ChangedStatus.CursorPos;
1496 this.adjust_markers (from, to, newlen);
1497 this.candidate_table.adjust (from, to, newlen);
1499 this.candidate_table.put (from, from + newlen, candidates)
1500 if (this.cursor_pos >= to)
1501 set_cursor.call (this, 'adjust', this.cursor_pos + text.length - (to - from));
1502 else if (this.cursor_pos > from)
1503 set_cursor.call (this, 'adjust', from)
1506 ins: function (text, candidates)
1508 this.preedit_replace (this.cursor_pos, this.cursor_pos, text, candidates);
1511 rep: function (old_text, new_text, candidates)
1513 this.preedit_replace (this.cursor_pos - old_text.length,
1514 this.cursor_pos, new_text, candidates);
1519 var deleted = pos - this.cursor_pos;
1520 if (pos < this.cursor_pos)
1524 this.DelSurroundText (pos);
1525 deleted = - this.cursor_pos;
1528 if (pos < this.cursor_pos)
1529 this.preedit_replace (pos, this.cursor_pos, '', null);
1533 if (pos > this.preedit.length)
1535 this.DelSurroundText (pos - this.preedit.length);
1536 deleted = this.preedit.length - this.cursor_pos;
1537 pos = this.preedit.length;
1539 if (pos > this.cursor_pos)
1540 this.preedit_replace (this.cursor_pos, pos, '', null);
1547 this.candidate_show = true;
1548 this.changed |= MIM.ChangedStatus.CandidateShow;
1553 this.candidate_show = false;
1554 this.changed |= MIM.ChangedStatus.CandidateShow;
1557 move: function (pos)
1561 else if (pos > this.preedit.length)
1562 pos = this.preedit.length;
1563 if (pos != this.cursor_pos)
1565 set_cursor.call (this, 'move', pos);
1566 this.changed |= MIM.ChangedStatus.Preedit;
1570 pushback: function (n)
1572 if (n instanceof MIM.KeySeq)
1574 if (this.key_head > 0)
1576 if (this.key_head < this.keys.val.length)
1577 this.keys.val.splice (this.key_head,
1578 this.keys.val.length - this.key_head);
1579 for (var i = 0; i < n.val.length; i++)
1580 this.keys.val.push (n.val[i]);
1586 if (this.key_head < 0)
1593 this.key_head = - n;
1594 if (this.key_head > this.keys.val.length)
1595 this.key_head = this.keys.val.length;
1601 if (this.key_head < this.keys.val.length)
1602 this.keys.val.splice (this.key_head, 1);
1607 if (this.preedit.length > 0)
1609 this.candidate_table.clear ();
1610 this.produced += this.preedit;
1611 this.preedit_replace.call (this, 0, this.preedit.length, '', null);
1612 this.preedit_saved = '';
1614 this.commit_key_head = this.key_head;
1618 shift: function (state)
1622 if (this.prev_state == null)
1624 state = this.prev_state;
1627 if (state == this.initial_state)
1632 this.keys.val.splice (0, this.key_head);
1633 this.key_head = this.state_key_head = this.commit_key_head = 0;
1634 this.prev_state = null;
1639 if (state != this.state)
1640 this.prev_state = this.state;
1642 if (state != this.state && state.keymap.map_actions)
1643 this.take_actions (state.keymap.map_actions);
1644 if (! this.state || this.state.title != state.title)
1645 this.changed |= MIM.ChangedStatus.StateTitle;
1647 this.keymap = state.keymap;
1648 save_state.call (this);
1651 Filter: function (key)
1655 Xex.Log ("active = false");
1656 this.key_unhandled = true;
1657 this.unhandled_key = key;
1660 if (key.key == '_reload')
1662 this.changed = MIM.ChangedStatus.None;
1664 this.key_unhandled = false;
1665 this.keys.val.push (key);
1667 while (this.key_head < this.keys.val.length)
1669 if (! handle_key.call (this))
1671 if (this.key_head < this.keys.val.length)
1673 this.unhandled_key = this.keys.val[this.key_head];
1674 this.keys.val.splice (this.key_head, this.key_head + 1);
1676 if (this.state_key_head > 0)
1677 this.state_key_head--;
1678 if (this.commit_key_head > 0)
1679 this.commit_key_head--;
1680 this.key_unhandled = true;
1686 this.key_unhandled = true;
1690 if (this.keymap == this.initial_state.keymap)
1693 if (this.commit_key_head > 0)
1695 this.keys.val.splice (0, this.commit_key_head);
1696 this.key_head -= this.commit_key_head;
1697 this.state_key_head -= this.commit_key_head;
1698 this.commit_key_head = 0;
1700 if (this.key_unhandled)
1702 this.keys.val.length = 0;
1703 //this.keys.val.splice (0, this.keys.val.length);
1704 this.key_head = this.state_key_head = this.commit_key_head = 0;
1706 if (this.changed & MIM.ChangedStatus.Candidate)
1708 if (this.candidate_show && this.candidates)
1713 return (! this.key_unhandled
1714 && this.produced.length == 0);
1718 MIM.create_list = function (node)
1720 // Load the list of input methods.
1721 for (node = node.firstChild; node; node = node.nextSibling)
1722 if (node.nodeName == 'input-method')
1724 var lang = null, name = null, extra_id = null, file = null;
1726 for (var n = node.firstChild; n; n = n.nextSibling)
1728 if (n.nodeName == 'language')
1729 lang = n.firstChild.nodeValue;
1730 else if (n.nodeName == 'name')
1731 name = n.firstChild.nodeValue;
1732 else if (n.nodeName == 'extra-id')
1733 extra_id = n.firstChild.nodeValue;
1734 else if (n.nodeName == 'filename')
1735 file = n.firstChild.nodeValue;
1737 if ((lang == 'ja' && name == 'anthy')
1738 || (lang == 'en' && name == 'ispell'))
1740 if (name && name != 'nil')
1742 if (! MIM.imlist[lang])
1743 MIM.imlist[lang] = {};
1744 MIM.imlist[lang][name] = new MIM.IM (lang, name, extra_id, file);
1746 else if (extra_id && extra_id != 'nil')
1748 if (! MIM.imextra[lang])
1749 MIM.imextra[lang] = {};
1750 MIM.imextra[lang][extra_id] = new MIM.IM (lang, name, extra_id, file);
1753 if (MIM.imextra.t && MIM.imextra.t.global)
1755 MIM.im_global = MIM.imextra.t.global;
1756 MIM.im_global.Load ();
1760 MIM.im_global = new MIM.IM ('t', 'nil', 'global', null);
1761 MIM.im_global.load_status = MIM.LoadStatus.Error;
1763 MIM.current = MIM.imlist['t']['latn-post'];
1764 MIM.current.Load ();
1769 var keys = new Array ();
1771 keys[0x08] = 'BackSpace';
1772 keys[0x0D] = 'Return';
1773 keys[0x1B] = 'Escape';
1774 keys[0x20] = 'space';
1775 keys[0x21] = 'Page_Up';
1776 keys[0x22] = 'Page_Down';
1778 keys[0x24] = 'Home';
1779 keys[0x25] = 'Left';
1781 keys[0x27] = 'Right';
1782 keys[0x28] = 'Down';
1783 keys[0x2D] = 'Insert';
1784 keys[0x2E] = 'Delete';
1785 for (var i = 1; i <= 12; i++)
1786 keys[111 + i] = "f" + i;
1787 keys[0x90] = "Num_Lock";
1788 keys[0xF0] = "Caps_Lock";
1791 keyids['U+0008'] = 'BackSpace';
1792 keyids['U+0009'] = 'Tab';
1793 keyids['U+0018'] = 'Cancel';
1794 keyids['U+001B'] = 'Escape';
1795 keyids['U+0020'] = 'space';
1796 keyids['U+007F'] = 'Delete';
1797 keyids['PageUp'] = 'Page_Up';
1798 keyids['PageDown'] = 'Page_Down';
1801 modifiers.Shift = 1;
1802 modifiers.Control = 1;
1804 modifiers.AltGraph = 1;
1807 MIM.decode_key_event = function (event)
1809 var key = event.keyIdentifier;
1811 if (key) // keydown event of Chrome
1816 var shifted = event.shiftKey;
1817 if (event.ctrlKey) mod += 'C-';
1818 if (event.metaKey) mod += 'M-';
1819 if (event.altKey) mod += 'A-';
1820 var keysym = keyids[key];
1823 else if (key.match(/^U\+([0-9A-Z]+)$/))
1825 if (mod.length == 0)
1827 var c = parseInt (RegExp.$1, 16);
1828 // Chrome sets, for instance, "U+0x00C1" when the key 'A'
1829 // is typed with control or alt/meta key.
1832 if (c >= 0x41 && c <= 0x5A && ! event.shiftKey)
1834 key = String.fromCharCode (c);
1837 if (shifted) mod += 'S-';
1838 return new MIM.Key (mod + key);
1842 key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode
1843 : event.charCode ? event.charCode
1847 if (event.type == 'keydown')
1852 if (event.shiftKey) key = "S-" + key ;
1855 key = String.fromCharCode (key);
1857 if (event.altKey) key = "A-" + key ;
1858 if (event.ctrlKey) key = "C-" + key ;
1859 return new MIM.Key (key);
1863 MIM.add_event_listener
1864 = (window.addEventListener
1865 ? function (target, type, listener) {
1866 target.addEventListener (type, listener, false);
1868 : window.attachEvent
1869 ? function (target, type, listener) {
1870 target.attachEvent ('on' + type,
1872 listener.call (target, window.event);
1875 : function (target, type, listener) {
1877 = function (e) { listener.call (target, e || window.event); };
1880 MIM.debug_print = function () { };
1882 MIM.get_range = function (target, ic)
1885 if (target.selectionStart != null) // for Mozilla
1887 from = target.selectionStart;
1888 to = target.selectionEnd;
1892 var r = document.selection.createRange ();
1893 var rr = r.duplicate ();
1895 rr.moveToElementText (target);
1896 rr.setEndPoint ('EndToEnd', range);
1897 from = rr.text.length - r.text.length;
1898 to = rr.text.length;
1901 && from == ic.range[0] + ic.cursor_pos
1902 && (ic.preedit.length == 0
1903 || ic.preedit == target.value.substring (ic.range[0], ic.range[1])))
1915 padingLeft: 'padding-left',
1916 paddingRight: 'padding-right',
1917 paddingTop: 'padding-top',
1918 paddintBottom: 'padding-bottom',
1919 marginRight: 'margin-right',
1920 marginTop: 'margin-top',
1921 borderLeftStyle: 'border-left-style',
1922 borderRightStyle: 'border-right-style',
1923 borderTopStyle: 'border-top-style',
1924 borderBottomStyle: 'border-bottom-style',
1925 borderLeftWidth: 'border-left-width',
1926 borderRightWidth: 'border-right-width',
1927 borderTopWidth: 'border-top-width',
1928 borderBottomWidth: 'border-bottom-width',
1929 fontFamily: 'font-family',
1930 fontSize: 'font-size',
1931 lineHeight: 'line-height',
1932 letterSpacing: 'letter-spacing',
1933 wordSpacing: 'word-spacing' };
1935 function copy_style (from, to)
1937 var from_style = getComputedStyle(from,'');
1938 for(var name in style_props)
1939 to.style[name] = from_style[style_props[name]];
1940 to.style.left = from.offsetLeft + 'px';
1941 to.style.top = from.offsetTop + 'px';
1942 to.style.width = from.offsetWidth;
1943 to.style.height = from.offsetHeight;
1947 var temp; // Temporary 'div' element
1949 MIM.get_preedit_pos = function (target, ic)
1953 temp = document.createElement ('div');
1954 temp.style.visibility = 'hidden';
1955 temp.style.position = 'absolute';
1956 temp.appendChild (document.createElement ('span'));
1957 temp.appendChild (document.createElement ('span'));
1958 document.getElementsByTagName ('body')[0].appendChild (temp);
1962 var styles = copy_style (target, temp);
1963 ic.abs_top = (parseInt (styles.marginTop)
1964 + parseInt (styles.borderTopWidth)
1965 + parseInt (styles.paddingTop));
1966 ic.abs_left = (parseInt (styles.marginLeft)
1967 + parseInt (styles.borderLeftWidth)
1968 + parseInt (styles.paddingLeft));
1969 for (var elm = target.offsetParent; elm; elm = elm.offsetParent)
1971 ic.abs_top += elm.offsetTop;
1972 ic.abs_left += elm.offsetLeft;
1976 temp.firstChild.innerText = target.value.substring (0, ic.range[0]);
1977 temp.lastChild.innerText
1978 = (ic.range[0] == ic.range[1] ? "|"
1979 : target.value.substring (ic.range[0], ic.range[1]));
1980 ic.abs_y = (ic.abs_top + temp.lastChild.offsetTop
1981 + temp.lastChild.offsetHeight - target.scrollTop);
1982 ic.abs_x0 = ic.abs_left + temp.lastChild.offsetLeft;
1983 ic.abs_x1 = ic.abs_x0 + temp.lastChild.offsetWidth;
1987 MIM.update_bar = function (target, ic)
1989 if (ic.preedit.length > 0)
1991 MIM.get_preedit_pos (target, ic);
1994 ic.bar = document.createElement ('div');
1995 ic.bar.style.position = 'absolute';
1996 ic.bar.style.backgroundColor = "black";
1997 ic.bar.style.minHeight = '1px';
1998 document.getElementsByTagName ('body')[0].appendChild (ic.bar);
2000 ic.bar.style.display = 'block'
2001 ic.bar.style.top = ic.abs_y + 'px';
2002 ic.bar.style.left = ic.abs_x0 + 'px';
2003 ic.bar.style.minWidth = ic.bar.style.maxWidth = (ic.abs_x1 - ic.abs_x0) + 'px';
2006 ic.bar.style.display = 'none'
2009 MIM.update = function (target, ic, for_focus_out)
2011 var text = target.value;
2012 target.value = (text.substring (0, ic.range[0])
2015 + text.substring (ic.range[1]));
2016 ic.range[0] += ic.produced.length;
2017 ic.range[1] = ic.range[0] + ic.preedit.length;
2018 MIM.update_bar (target, ic);
2019 if (! for_focus_out)
2021 var pos = ic.range[0] + ic.cursor_pos;
2022 if (target.setSelectionRange) // Mozilla
2024 var scrollTop = target.scrollTop;
2025 target.setSelectionRange (pos, pos);
2026 target.scrollTop = scrollTop;
2030 var range = target.createTextRange ();
2031 range.moveStart ('character', pos);
2032 range.moveEnd ('character', pos);
2039 MIM.show = function (ic)
2041 if (! ic.candidates)
2043 var target = ic.target;
2044 MIM.get_preedit_pos (target, ic);
2047 ic.can_node = document.createElement ('table');
2048 ic.can_node.style.position = 'absolute';
2049 ic.can_node.style.display = 'none';
2050 ic.can_node.style.backgroundColor = "white";
2051 ic.can_node.style.border = "1px solid black";
2052 document.getElementsByTagName ('body')[0].appendChild (ic.can_node);
2055 if (ic.changed & MIM.ChangedStatus.CandidateList)
2057 while (ic.can_node.childNodes.length > 0)
2058 ic.can_node.removeChild (ic.can_node.firstChild);
2059 var tr = document.createElement ('tr');
2060 var group = ic.candidates.CurrentGroup ();
2061 var td = document.createElement ('td');
2062 td.innerHTML = group[0][1] + '/' + group[0][0];
2063 td.style.color = 'white';
2064 td.style.backgroundColor = 'black';
2065 tr.appendChild (td);
2066 for (var i = 1; i < group.length; i++)
2068 var td = document.createElement ('td');
2070 td.innerHTML = (i < 10 ? i : i == 10 ? '0' : String.fromCharCode (0x60 + (i - 10))) + '.' + group[i];
2071 if (i == group[0][2] + 1)
2072 td.style.backgroundColor = 'lightblue';
2073 tr.appendChild (td);
2075 ic.can_node.appendChild (tr);
2076 ic.can_node.style.top = (ic.abs_y + 10) + 'px';
2077 ic.can_node.style.left = ic.abs_x0 + 'px';
2081 var td = ic.can_node.firstElement ().firstElement ().nextElement ();
2082 var col = ic.candidates.CurrentCol ();
2083 for (var i = 0; td; td = td.nextElement ())
2084 td.style.backgroundColor = (i++ == col ? 'lightblue' : 'white');
2086 ic.can_node.style.display = 'block';
2089 MIM.hide = function (ic)
2092 ic.can_node.style.display = 'none';
2095 function real_focus_in (target, ic)
2097 if (MIM.get_range (target, ic))
2098 ic.Filter (MIM.Key.FocusIn);
2099 MIM.update (target, ic, false);
2104 MIM.focus_in = function (event)
2106 var target = event.target;
2107 Xex.Log ("Focus in " + target.tagName);
2109 = setTimeout (function () {real_focus_in (target, target.mim_ic)} , 10);
2113 MIM.click = function (event)
2115 var target = event.target;
2116 Xex.Log ("Click in " + target.tagName);
2119 clearTimeout (focus_in_timer);
2120 focus_in_timer = null;
2122 real_focus_in (target, target.mim_ic);
2127 MIM.focus_out = function (event)
2129 var target = event.target;
2130 var ic = target.mim_ic;
2131 Xex.Log ("Focus out " + target.tagName);
2132 MIM.get_range (target, ic);
2133 MIM.debug_print (event, ic);
2134 ic.Filter (MIM.Key.FocusOut);
2135 MIM.update (target, ic, true);
2139 MIM.keydown = function (event)
2141 var target = event.target;
2142 if (! (target.type == "text" || target.type == "textarea"))
2145 var ic = target.mim_ic;
2146 if (! ic || ic.im != MIM.current)
2148 target.mim_ic = null;
2149 Xex.Log ('creating IC for ' + MIM.current.lang + '-' + MIM.current.name);
2150 ic = new MIM.IC (MIM.current, target);
2151 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2154 MIM.add_event_listener (target, 'focus', MIM.focus_in);
2155 MIM.add_event_listener (target, 'blur', MIM.focus_out);
2156 MIM.add_event_listener (target, 'click', MIM.click);
2158 MIM.get_range (target, ic)
2159 MIM.debug_print (event, ic);
2160 ic.key = MIM.decode_key_event (event);
2164 var result = ic.Filter (ic.key);
2166 Xex.Log ('Error' + e);
2169 MIM.update (target, ic, false);
2170 if (! ic.key_unhandled)
2171 event.preventDefault ();
2175 MIM.keypress = function (event)
2177 var target = event.target;
2178 if (! (target.type == "text" || target.type == "textarea"))
2181 var ic = target.mim_ic;
2185 if (ic.im.load_status != MIM.LoadStatus.Loaded)
2188 ic.key = MIM.decode_key_event (event);
2196 var result = ic.Filter (ic.key);
2198 Xex.Log ('Error:' + e);
2201 MIM.update (target, ic, false);
2202 if (! ic.key_unhandled)
2203 event.preventDefault ();
2205 Xex.Log ("error:" + e);
2206 event.preventDefault ();
2208 MIM.debug_print (event, ic);
2215 var lang_category = {
2217 cs: { name: 'Czech' },
2218 da: { name: 'Danish' },
2219 el: { name: 'Greek' },
2220 en: { name: 'English' },
2221 eo: { name: 'Esperanto' },
2222 fr: { name: 'French' },
2223 grc: { name: 'ClassicGreek' },
2224 hr: { name: 'Croatian' },
2225 hy: { name: 'Armenian' },
2226 ka: { name: 'Georgian' },
2227 kk: { name: 'Kazakh' },
2228 ru: { name: 'Russian' },
2229 sk: { name: 'Slovak' },
2230 sr: { name: 'Serbian' },
2231 sv: { name: 'Swedish' },
2232 yi: { name: 'Yiddish' } },
2234 ar: { name: 'Arabic' },
2235 dv: { name: 'Divehi' },
2236 fa: { name: 'Persian' },
2237 he: { name: 'Hebrew' },
2238 kk: { name: 'Kazakh' },
2239 ps: { name: 'Pushto' },
2240 ug: { name: 'Uighur' },
2241 yi: { name: 'Yiddish' } },
2243 as: { name: 'Assamese' },
2244 bn: { name: 'Bengali' },
2245 bo: { name: 'Tibetan' },
2246 gu: { name: 'Gujarati' },
2247 hi: { name: 'Hindi' },
2248 kn: { name: 'Kannada' },
2249 ks: { name: 'Kashmiri' },
2250 ml: { name: 'Malayalam' },
2251 mr: { name: 'Marathi' },
2252 ne: { name: 'Nepali' },
2253 or: { name: 'Oriya' },
2254 pa: { name: 'Panjabi' },
2255 sa: { name: 'Sanskirit' },
2256 sd: { name: 'Sindhi' },
2257 si: { name: 'Sinhalese' },
2258 ta: { name: 'Tamil' },
2259 te: { name: 'Telugu' },
2260 ur: { name: 'Urdu' } },
2262 cmc: { name: 'Cham' },
2263 km: { name: 'Khmer'},
2264 lo: { name: 'Lao' },
2265 my: { name: 'Burmese' },
2266 tai: { name: 'Tai Viet' },
2267 th: { name: 'Thai' },
2268 vi: { name: 'Vietanamese' } },
2270 ii: { name: 'Yii' },
2271 ja: { name: 'Japanese' },
2272 ko: { name: 'Korean' },
2273 zh: { name: 'Chinese' } },
2275 am: { name: 'Amharic' },
2276 ath: { name: 'Carrier' },
2277 bla: { name: 'Blackfoot' },
2278 cr: { name: 'Cree' },
2279 eo: { name: 'Esperanto' },
2280 iu: { name: 'Inuktitut' },
2281 nsk: { name: 'Naskapi' },
2282 oj: { name: 'Ojibwe' },
2283 t: { name: 'Generic' } }
2286 function categorize_im ()
2288 var cat, lang, list, name;
2289 for (lang in MIM.imlist)
2292 for (cat in lang_category)
2293 if (lang_category[cat][lang])
2295 list = lang_category[cat][lang].list;
2297 list = lang_category[cat][lang].list = {};
2298 for (name in MIM.imlist[lang])
2299 list[name] = MIM.imlist[lang][name];
2302 for (name in MIM.imlist[lang])
2303 Xex.Log ('no category ' + lang + '-' + name);
2312 clearTimeout (destroy_timer);
2313 destroy_timer = null;
2314 var target = document.getElementById ('mim-menu');
2317 for (; last_target && last_target.menu_level;
2318 last_target = last_target.parentLi)
2319 last_target.style.backgroundColor = 'white';
2320 var nodes = target.getElementsByTagName ('ul');
2321 for (var i = 0; i < nodes.length; i++)
2322 nodes[i].style.visibility = 'hidden';
2323 nodes = target.getElementsByTagName ('pre');
2324 for (var i = 0; i < nodes.length; i++)
2325 nodes[i].style.visibility = 'hidden';
2326 document.getElementsByTagName ('body')[0].removeChild (target);
2330 function destroy_menu () {
2331 if (! destroy_timer)
2332 destroy_timer = setTimeout (destroy, 1000);
2336 function show_submenu (event)
2338 var target = event.target;
2339 if (! target.menu_level)
2341 if (! target.parentNode || ! target.parentNode.menu_level)
2343 target = target.parentNode;
2347 clearTimeout (destroy_timer);
2348 destroy_timer = null;
2350 if (last_target && target.parentLi != last_target)
2352 last_target.style.backgroundColor = 'white';
2353 while (target.menu_level < last_target.menu_level)
2355 last_target = last_target.parentLi;
2356 last_target.style.backgroundColor = 'white';
2358 var nodes = last_target.getElementsByTagName ('ul');
2359 for (var i = 0; i < nodes.length; i++)
2360 nodes[i].style.visibility = 'hidden';
2361 nodes = last_target.getElementsByTagName ('pre');
2362 for (var i = 0; i < nodes.length; i++)
2363 nodes[i].style.visibility = 'hidden';
2365 last_target = target;
2366 target.style.backgroundColor = 'yellow';
2367 if (target.menu_level < 3)
2371 target.lastChild.style.visibility = 'visible';
2372 target.lastChild.style.left = target.clientWidth + 'px';
2376 var left = target.clientWidth;
2377 for (var n = target.firstElement ().nextElement (); n; n = n.nextElement ())
2379 n.style.visibility = 'visible';
2380 n.style.left = left + 'px';
2381 left += n.clientWidth;
2385 event.preventDefault ();
2388 function select_im (event)
2390 var target = event.target;
2393 if (! target.parentNode || ! target.parentNode.menu_level)
2395 event.preventDefault ();
2398 target = target.parentNode;
2402 MIM.current = target.im;
2403 MIM.current.Load ();
2406 event.preventDefault ();
2409 function create_ul (visibility)
2411 var ul = document.createElement ('ul');
2413 ul.style.position = 'absolute';
2414 ul.style.margin = '0px';
2415 ul.style.padding = '0px';
2416 ul.style.border = '1px solid gray';
2417 ul.style.borderBottom = 'none';
2418 ul.style.top = '-1px';
2419 ul.style.backgroundColor = 'white';
2420 ul.style.visibility = visibility;
2424 function create_li (level, text)
2426 var li = document.createElement ('li');
2427 li.style.position = 'relative';
2428 li.style.margin = '0px';
2429 li.style.padding = '1px';
2430 li.style.borderBottom = '1px solid gray';
2431 li.style.top = '0px';
2432 li.style.listStyle = 'none';
2433 li.menu_level = level;
2434 var nobr = document.createElement ('nobr');
2435 nobr.innerHTML = text;
2436 li.appendChild (nobr);
2440 function create_sep ()
2442 var sep = document.createElement ('pre');
2444 sep.innerHTML = ' ';
2445 sep.style.position = 'absolute';
2446 sep.style.margin = '0px';
2447 sep.style.padding = '2px';
2448 sep.style.border = '2px solid black';
2449 sep.style.top = '-1px';
2450 sep.style.backgroundColor = 'black';
2451 sep.style.visibility = 'hidden';
2457 function create_menu (event)
2459 var target = event.target;
2461 if (! ((target.type == "text" || target.type == "textarea")
2462 && event.which == 1 && event.ctrlKey))
2467 menu = create_ul ('visible');
2468 menu.style.fontFamily = 'sans-serif';
2469 menu.style.fontWeight = 'bold';
2470 menu.id = 'mim-menu';
2471 menu.onmousedown = select_im;
2472 menu.onmouseover = show_submenu;
2473 menu.onmouseout = destroy_menu;
2474 for (var catname in lang_category)
2476 var cat = lang_category[catname];
2477 var li = create_li (1, catname);
2478 li.appendChild (create_sep ());
2479 var sub = create_ul ('hidden');
2480 for (var langname in cat)
2482 var lang = cat[langname];
2485 var sub_li = create_li (2, lang.name);
2486 sub_li.parentLi = li;
2487 sub_li.appendChild (create_sep ());
2488 var subsub = create_ul ('hidden');
2489 for (var name in lang.list)
2491 var im = lang.list[name];
2492 var subsub_li = create_li (3, im.name);
2493 subsub_li.parentLi = sub_li;
2495 subsub.appendChild (subsub_li);
2497 sub_li.appendChild (subsub);
2498 sub.appendChild (sub_li);
2500 li.appendChild (sub);
2501 menu.appendChild (li);
2503 lang_category = null;
2505 menu.style.left = (event.clientX - 10) + "px";
2506 menu.style.top = (event.clientY - 10) + "px";
2507 document.getElementsByTagName ('body')[0].appendChild (menu);
2510 MIM.init = function ()
2512 MIM.add_event_listener (window, 'keydown', MIM.keydown);
2513 MIM.add_event_listener (window, 'keypress', MIM.keypress);
2514 MIM.add_event_listener (window, 'mousedown', create_menu);
2515 Xex.Load (MIM.server, "imlist.xml", MIM.create_list);