aboutsummaryrefslogtreecommitdiffstats
path: root/options-migrate-user-pref.js
blob: 3c919f291c91b3bd3791be188bf5c42485dd08ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var PLUGIN_INFO =
<VimperatorPlugin>
<name>{NAME}</name>
<description>options migrate user_pref</description>
<description lang="ja">:set foobarbaz で簡単に user_pref をセットできるプラグイン</description>
<minVersion>2.0pre</minVersion>
<maxVersion>2.0</maxVersion>
<updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/options-migrate-user-pref.js</updateURL>
<author mail="hotchpotch@gmail.com" homepage="http://d.hatena.ne.jp/secondlife/">Yuichi Tateno</author>
<license>MIT</license>
<version>0.1</version>
<detail><![CDATA[
>||
:set! javascript.enabled=true
||<

>||
:set javascript
:set nojavascript
||<
のようにマッピングするためのプラグインです
boolean/number を簡単にセットできるようになるためよく user_pref を変更する場合などに便利です


>||
js <<EOF
liberator.globalVariables.options_migrate_user_pref =
[
    {
        pref: 'javascript.enabled',
        description: 'Using JavaScript',
        command: ['javascript', 'js'],
    },
    {
        pref: 'font.size.fixed.ja',
        description: 'JA fixed font-size',
        command: ['jaffont'],
    }
];
EOF
||<

]]></detail>
</VimperatorPlugin>;

(function() {
    let p = function(msg) {
        Application.console.log(msg);
    }

    liberator.plugins.migrateUserPref = function(config) {
        config.forEach(function(conf) {
            let pref = conf.pref;
            let type;
            try
            {
                switch (services.get('pref').getPrefType(conf.pref))
                {
                case Ci.nsIPrefBranch.PREF_STRING:
                    // XXX: string のとき、うまく user_pref に設定できていない?
                    type = 'string';
                    break;
                case Ci.nsIPrefBranch.PREF_INT:
                    type = 'number';
                    break;
                case Ci.nsIPrefBranch.PREF_BOOL:
                    type = 'boolean';
                    break;
                default:
                    return liberator.echoerr('migrate-user-pref: error pref: ' + pref);
                }
            }
            catch (e)
            {
                return liberator.echoerr('migrate-user-pref: error pref: ' + pref + ' ' + e);
            }

            options.add(conf.command, conf.description, type,
                (typeof conf.defaultValue == 'undefined' ? options.getPref(pref) : conf.defaultValue),
                {
                    setter: function(value) options.setPref(pref, value),
                    getter: function() options.getPref(pref),
                }
            );
        });
    }

    liberator.plugins.migrateUserPref(liberator.globalVariables['options_migrate_user_pref'] || []);
})();

: - "s" setting simple n-base decimal hint labeling (n = hintchars.length) - "a" setting adjust no overlap labeling Default setting is "s". e.g.) let g:hintlabeling="a" == TODO == ]]></detail> <detail lang="ja"><![CDATA[ == Usage == デフォルトの設定では 小文字は候補を絞るためのテキスト入力に 大文字は文字ラベルの選択に使います == SETTING == let g:hintchars: set character used by char-hint. e.g.) let g:hintchars="hjkl" let g:hintsio: - "i" setting char-hint input lowercase. - "I" setting char-hint input uppercase. - "o" setting char-hint show lowercase. - "O" setting char-hint show uppercase. Default setting is "IO". e.g.) let g:hintsio="i" let g:hintlabeling: - "s" setting simple n-base decimal hint labeling (n = hintchars.length) - "a" setting adjust no overlap labeling Default setting is "s". e.g.) let g:hintlabeling="a" == TODO == ]]></detail> </VimperatorPlugin>; //}}} (function () { const DEFAULT_HINTCHARS = "HJKLASDFGYUIOPQWERTNMZXCVB"; const hintContext = modules.hints; let hintchars = DEFAULT_HINTCHARS; let inputCase = function(str) str.toUpperCase(); let inputRegex = /[A-Z]/; let showCase = function(str) str.toUpperCase(); let getStartCount = function() 0; let timer = null; function chars2num(chars) //{{{ { let num = 0; hintchars = inputCase(hintchars); let base = hintchars.length; for(let i=0,l=chars.length;i<l;++i) { num = base*num + hintchars.indexOf(chars[i]); } return num; } //}}} function num2chars(num) //{{{ { let chars = ""; hintchars = inputCase(hintchars); let base = hintchars.length; do { chars = hintchars[((num % base))] + chars; num = Math.floor(num/base); } while(num>0); return chars; } //}}} function getAdjustStartCount(base, count) //{{{ { if(count < base) { return 0; } else if(count >= Math.pow(base, 2)) { return base; } var start = Math.floor(count / base); var adjust = count + start; var next_start; while(start != (next_start = Math.floor(adjust / base))) { adjust += start; start = next_start; } return start; } //}}} function getCharHints(win) //{{{ { let hints = []; (function (win) { let elems = [elem for(elem in util.evaluateXPath('//*[@liberator:highlight="Hint" and @number]', win.document))]; hints = hints.concat(elems); Array.forEach(win.frames, arguments.callee); })(win); return hints; } //}}} function showCharHints(hints) //{{{ { let start = getStartCount(hintchars.length, hints.length); for(let i=0,len=hints.length;i<len;++i) { let hint = hints[i]; let num = hint.getAttribute("number"); let hintchar = num2chars(parseInt(num, 10)+start); hint.setAttribute("hintchar", showCase(hintchar)); } } //}}} function isValidHint(hintInput, hint) //{{{ { if(hintInput.length == 0) return false; let hintchar = hint.getAttribute("hintchar"); return inputCase(hintchar).indexOf(hintInput) == 0; } //}}} function setIOType(type) //{{{ { switch (type) { case "I": inputCase = function(str) str.toUpperCase(); inputRegex = /[A-Z]/; break; case "i": inputCase = function(str) str.toLowerCase(); inputRegex = /[a-z]/; break; case "O": showCase = function(str) str.toUpperCase(); break; case "o": showCase = function(str) str.toLowerCase(); break; } } //}}} function clearOriginalTimeout() //{{{ { liberator.eval('if(_activeTimeout) clearTimeout(_activeTimeout);_activeTimeout = null;', hintContext); } //}}} function processHintInput(hintInput, hints) //{{{ { if (timer) { clearTimeout(timer); timer = null; } let start = getStartCount(hintchars.length, hints.length); let num = chars2num(hintInput)-start; if(num < 0) return; let numstr = String(num); for(let i=0,l=numstr.length;i<l;++i) { let num = numstr[i]; // events.toString(e) return e.liberatorString // if e.liberatorString. // so alt handled as press number event by vimperator. let alt = new Object; alt.liberatorString = num; charhints.original.onEvent.apply(hintContext,[alt]); } clearOriginalTimeout(); statusline.updateInputBuffer(hintInput); let validHints = hints.filter(function(hint) isValidHint(hintInput, hint)); if(validHints.length == 1) { charhints.original.processHints.apply(hintContext,[true]); return true; } let timeout = options["hinttimeout"]; if (timeout > 0) { timer = setTimeout(function () { charhints.original.processHints.apply(hintContext,[true]); }, timeout); } } //}}} var hintInput = ""; var charhints = plugins.charhints = { show: function (minor, filter, win) //{{{ { charhints.original.show.apply(hintContext, arguments); hintInput = ""; let hints = getCharHints(window.content); showCharHints(hints); }, //}}} onInput: function (event) //{{{ { let eventkey = events.toString(event); if(/^\d$/.test(eventkey)) { commandline.command += eventkey; } let hintString = commandline.command; commandline.command = hintString.replace(inputRegex, ""); charhints.original.onInput.apply(hintContext, arguments); for(let i=0,l=hintString.length;i<l;++i) { if(inputRegex.test(hintString[i])) { hintInput += hintString[i]; } } let hints = getCharHints(window.content); showCharHints(hints); if(hintInput.length>0) processHintInput(hintInput, hints); }, //}}} onEvent: function (event) //{{{ { if(/^\d$/.test(events.toString(event))) { charhints.onInput(event); } else { charhints.original.onEvent.apply(hintContext,arguments); clearOriginalTimeout(); statusline.updateInputBuffer(hintInput); } }, //}}} processHints: function (followFirst) //{{{ { // don't followFirst if processHints call from // charhints.original.onEvent(alt) in processHintInput let caller = arguments.callee.caller; if(caller == charhints.original.onEvent && caller.caller == processHintInput) return charhints.original.processHints.apply(hintContext,[false]); return charhints.original.processHints.apply(hintContext,arguments); }, //}}} }; if(!charhints.original) { charhints.original = { show: Hints.prototype.show, onInput: Hints.prototype._onInput, onEvent: Hints.prototype.onEvent, processHints: Hints.prototype._processHints, }; charhints.install = function () //{{{ { hints.show = charhints.show; hints.onEvent = charhints.onEvent; liberator.eval("_onInput = liberator.plugins.charhints.onInput", hintContext); liberator.eval("_processHints = liberator.plugins.charhints.processHints", hintContext); liberator.execute(":hi Hint::after content: attr(hintchar)", true, true); if(liberator.globalVariables.hintsio) { let hintsio = liberator.globalVariables.hintsio; Array.forEach(hintsio, setIOType); } if(liberator.globalVariables.hintchars) { hintchars = liberator.globalVariables.hintchars; } if(liberator.globalVariables.hintlabeling) { switch(liberator.globalVariables.hintlabeling) { default: case "s": getStartCount = function() 0; break; case "a": getStartCount = getAdjustStartCount; break; } } }; //}}} charhints.uninstall = function () //{{{ { hints.show = charhints.original.show; hints.onEvent = charhints.original.onEvent; liberator.eval("_onInput = liberator.plugins.charhints.original.onInput", hintContext); liberator.eval("_processHints = liberator.plugins.charhints.original.processHints", hintContext); liberator.execute(":hi Hint::after content: attr(number)", true, true); }; //}}} } charhints.install(); })(); // vim: set fdm=marker sw=4 ts=4 et fenc=utf-8: