From 12a64efd42c76ad34a424aaf463c285227a5433a Mon Sep 17 00:00:00 2001 From: drry Date: Mon, 14 Apr 2008 19:16:37 +0000 Subject: lang/javascript/vimperator-plugins/trunk/feedSomeKeys.js: * Array オブジェクトに対する `for each` 文を除去しました。 * 正規表現を修正しました。 * ほか。 git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@9460 d0d07461-0603-4401-acd4-de1884942a52 --- feedSomeKeys.js | 118 +++++++++++++++++++++++--------------------------------- 1 file changed, 49 insertions(+), 69 deletions(-) diff --git a/feedSomeKeys.js b/feedSomeKeys.js index 599bf54..f3058f0 100644 --- a/feedSomeKeys.js +++ b/feedSomeKeys.js @@ -10,7 +10,7 @@ * 英語での説明を放棄する * * keyイベント(正確にはkepressイベント)をWebコンテンツ側へ送る事を可能にするプラグイン - * GmailとかLivedoor ReaderとかGreasemonkeyでキーを割り当てている場合に活躍するでしょう。 + * Gmailとかlivedoor ReaderとかGreasemonkeyでキーを割り当てている場合に活躍するでしょう。 * それ以外の場面ではむしろ邪魔になる諸刃の剣 * * :f[eed]map lhs -> lhsのキーマップをそのままWebコンテンツへ @@ -23,18 +23,16 @@ * == LDR の場合 == js <","","z","b","<",">"]);'); + 'js plugins.feedKey.setup("j k s a p o v c z b < >".split(/ +/));'); EOF * とかやると幸せになれるかも。 - * + * * == Gmail の場合 == js <"] ], - [ KeyEvent.DOM_VK_RETURN, ["Return", "CR", "Enter"] ], - [ KeyEvent.DOM_VK_TAB, ["Tab"] ], - [ KeyEvent.DOM_VK_DELETE, ["Del"] ], +const keyTable = [ [ KeyEvent.DOM_VK_BACK_SPACE, ["BS"] ], - [ KeyEvent.DOM_VK_HOME, ["Home"] ], - [ KeyEvent.DOM_VK_INSERT, ["Insert", "Ins"] ], + [ KeyEvent.DOM_VK_TAB, ["Tab"] ], + [ KeyEvent.DOM_VK_RETURN, ["Return", "CR", "Enter"] ], + //[ KeyEvent.DOM_VK_ENTER, ["Enter"] ], + [ KeyEvent.DOM_VK_ESCAPE, ["Esc", "Escape"] ], + [ KeyEvent.DOM_VK_SPACE, ["Spc", "Space"] ], + [ KeyEvent.DOM_VK_PAGE_UP, ["PageUp"] ], + [ KeyEvent.DOM_VK_PAGE_DOWN, ["PageDown"] ], [ KeyEvent.DOM_VK_END, ["End"] ], + [ KeyEvent.DOM_VK_HOME, ["Home"] ], [ KeyEvent.DOM_VK_LEFT, ["Left"] ], - [ KeyEvent.DOM_VK_RIGHT, ["Right"] ], [ KeyEvent.DOM_VK_UP, ["Up"] ], + [ KeyEvent.DOM_VK_RIGHT, ["Right"] ], [ KeyEvent.DOM_VK_DOWN, ["Down"] ], - [ KeyEvent.DOM_VK_PAGE_UP, ["PageUp"] ], - [ KeyEvent.DOM_VK_PAGE_DOWN, ["PageDown"] ], + [ KeyEvent.DOM_VK_INSERT, ["Ins", "Insert"] ], + [ KeyEvent.DOM_VK_DELETE, ["Del", "Delete"] ], [ KeyEvent.DOM_VK_F1, ["F1"] ], [ KeyEvent.DOM_VK_F2, ["F2"] ], [ KeyEvent.DOM_VK_F3, ["F3"] ], @@ -92,24 +90,21 @@ var keyTable = [ ]; function getKeyCode(str) { str = str.toLowerCase(); - for (var i in keyTable) { - for (var k in keyTable[i][1]) { - if (keyTable[i][1][k].toLowerCase() == str) return keyTable[i][0]; - } - } - return 0; + var ret = 0; + keyTable.some(function(i) i[1].some(function(k) k.toLowerCase() == str && (ret = i[0]))); + return ret; } function init(keys){ destroy(); - for each(var key in keys){ + keys.forEach(function(key){ var origKey, feedKey; - if (typeof(key) == 'object'){ - [origKey, feedKey] = [key[0],key[1]]; + if (key instanceof Array){ + [origKey, feedKey] = key; } else if (typeof(key) == 'string'){ [origKey, feedKey] = [key,key]; } replaceUserMap(origKey, feedKey); - } + }); } function replaceUserMap(origKey, feedKey){ if (mappings.hasMap(modes.NORMAL, origKey)){ @@ -117,11 +112,11 @@ function replaceUserMap(origKey, feedKey){ if (origMap.description.indexOf(origKey+' -> ') != 0) { // origMapをそのままpushするとオブジェクト内の参照先を消されてしまう // 仕方なく複製を試みる - var clone = new Map(origMap.modes.map(function(m){return m;}), - origMap.names.map(function(n){return n;}), + var clone = new Map(origMap.modes.map(function(m) m), + origMap.names.map(function(n) n), origMap.description, origMap.action, - { flags: origMap.flags, rhs:origMap.rhs, noremap:origMap.noremap }); + { flags:origMap.flags, rhs:origMap.rhs, noremap:origMap.noremap }); origMaps.push(clone); } } @@ -131,21 +126,20 @@ function replaceUserMap(origKey, feedKey){ for (var i=0; i "<" - var matches = keys.substr(i + 1).match(/([CSMAcsma]-)*([^>]+)/); - if (matches && matches[2]) { + var matches = keys.substr(i + 1).match(/^((?:[ACMSacms]-)*)([^>]+)/); + if (matches) { if (matches[1]) { ctrl = /[cC]-/.test(matches[1]); alt = /[aA]-/.test(matches[1]); @@ -208,7 +202,7 @@ function feedKeyIntoContent(keys){ if (!ctrl && !alt && !shift && !meta) return false; charCode = matches[2].charCodeAt(0); } else if (matches[2].toLowerCase() == "space") { - charCode = 32; + charCode = KeyEvent.DOM_VK_SPACE; } else if (keyCode = getKeyCode(matches[2])) { charCode = 0; } else { @@ -233,9 +227,7 @@ function feedKeyIntoContent(keys){ commands.addUserCommand(['feedmap','fmap'],'Feed Map a key sequence', function(args){ if(!args){ - echo(feedMaps.map(function(map){ - return map.description.replace(//g,'>'); - }),true); + echo(feedMaps.map(function(map) map.description.replace(//g,'>')),true); } var [ ,lhs,rhs] = args.match(/(\S+)(?:\s+(.+))?/); if (!rhs){ @@ -245,24 +237,12 @@ commands.addUserCommand(['feedmap','fmap'],'Feed Map a key sequence', } } ); -commands.addUserCommand(['feedmapclear','fmapc'],'Clear Feed Maps', - function(){ - destroy(); - } -); +commands.addUserCommand(['feedmapclear','fmapc'],'Clear Feed Maps',destroy); var converter = { - setup: function(keys){ - init(keys); - }, - get origMap(){ - return origMaps; - }, - get feedMap(){ - return feedMaps; - }, - reset: function(){ - destroy(); - } + get origMap() origMaps, + get feedMap() feedMaps, + setup: init, + reset: destroy }; return converter; })(); -- cgit v1.2.3