aboutsummaryrefslogtreecommitdiffstats
path: root/history-search-backward.js
blob: c5bfe0dd6c7f252394087c36cdbc3dc336ffe588 (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
91
var PLUGIN_INFO =
<VimperatorPlugin>
<name>{NAME}</name>
<description>History search backward like UNIX shell.</description>
<description lang="ja">UNIX シェルのようなC-rで履歴検索を行うプラグイン</description>
<minVersion>2.0</minVersion>
<maxVersion>2.0</maxVersion>
<updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/history-search-backward.js</updateURL>
<author mail="hotchpotch@gmail.com" homepage="http://d.hatena.ne.jp/secondlife/">Yuichi Tateno</author>
<license>MIT</license>
<version>0.2</version>
<detail><![CDATA[
UNIX シェルのようにコマンドラインで C-r でヒストリ検索を行うプラグインですmap の変更設定は以下のように行えます
>||
liberator.globalVariables.history_search_backward_map = ['<C-r>'];
||<

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

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

    let evalWithContext = function(func, context) {
        let str;
        let fstr = func.toString();
        if (fstr.indexOf('function () {') == 0) {
            str = fstr.replace(/.*?{([\s\S]+)}.*?/m, "$1");
        } else {
            str = '(' + fstr + ')()';
        }
        return liberator.eval(str, context);
    }

    let showCompletions = function() {
        if (!options.get('wildoptions').has('auto')) {
            evalWithContext(function() {
                completions.complete(true, false);
                completions.itemList.show();
            }, commandline.input);
        }
    }

    let next = function() {
        evalWithContext(function() completions.tab(false), commandline.input);
    }

    let prev = function() {
        evalWithContext(function() completions.tab(true), commandline.input);
    }

    const commandlineWidget = document.getElementById("liberator-commandline");

    mappings.addUserMap([modes.COMMAND_LINE], 
        liberator.globalVariables.history_search_backward_map || ['<C-r>'], 
        'History incremental search backward.', 
        function() 
        {
            if (evalWithContext(function() completions.itemList.visible(), commandline.input)) {
                next();
                return;
            }

            let command = commandline.command || '';
            let completionsList = [[key, i] for ([i, key] in storage['history-command'])].
                                      filter(function([key, i]) key).reverse();

            commandline.input('bck-i-search: ', function(str) {
                try {
                    liberator.execute(str);
                } catch(e) {};
                modes.pop();
                return;
            }, {
                completer: function(context) {
                    context.title = ['CommandLine History', 'INDEX'];
                    context.completions = completionsList;
                },
                onChange: function() {
                    showCompletions();
                },
                default: command,
            });
            showCompletions();
        }
    );
})();


let slideBar = window.slideBar; slideBar.__proto__ = slideBarProto; return slideBar; }, getContextByTitle: function jetpack_getContextByTitle(title){ let contexts = this.contexts.filter(function(ctx){ return ctx.feed.title == title; }); liberator.assert(contexts.length > 0, "no jetpack features"); return contexts[0]; }, getFeedByTitle: function jetpack_getFeedByTitle(title){ return getFeedByTitle(title, FEED_FLAG.ALL); }, install: function jetpack_install(path){ let file = io.File(path); let uri = util.createURI(file.path); let name = file.leafName.replace(/\..*/, "").replace(/-([a-z])/g, function (m, n1) n1.toUpperCase()); this.feedManager.addSubscribedFeed({ canAutoUpdate: false, sourceCode: file.read("UTF-8"), sourceUrl: uri.spec, title: name, type: "jetpack", url: uri.spec }); }, refresh: function jetpack_refresh(title){ let feed = getFeedByTitle(title, FEED_FLAG.SUBSCRIBED | FEED_FLAG.NOT_BUILTIN); this.JetpackRuntime.forceFeedUpdate(feed); }, uninstall: function jetpack_uninstall(title){ let feed = getFeedByTitle(title, FEED_FLAG.SUBSCRIBED | FEED_FLAG.NOT_BUILTIN); feed.remove(); }, reinstall: function jetpack_reinstall(title){ let feed = getFeedByTitle(title, FEED_FLAG.UNSUBSCRIBED); feed.unremove(); }, purge: function jetpack_purge(title){ let feed = getFeedByTitle(title, FEED_FLAG.ALL | FEED_FLAG.NOT_BUILTIN); if (feed.isSubscribed) feed.remove(); feed.purge(); }, }; let slideBarProto = { select: function jetpack_slideBar_select(title){ let features = this.features.filter(function(f){ return f.context.feed.title == title; }); liberator.assert(features.length > 0, "no such jetpack feature"); this.selectFeature(features[0]); } }; const FEED_FLAG= { NOT_BUILTIN: 1 << 0, SUBSCRIBED: 1 << 1, UNSUBSCRIBED: 1 << 2, ALL: 6 }; function getFeedByTitle(title, flag){ let feeds = []; if (flag >= FEED_FLAG.ALL) feeds = getAllFeeds(); else if (flag & FEED_FLAG.UNSUBSCRIBED) feeds = getUnsubscribedFeeds(); else if (flag & FEED_FLAG.SUBSCRIBED) feeds = getSubscribedFeeds(true); if (flag & FEED_FLAG.NOT_BUILTIN) feeds = feeds.filter(function(f) !f.isBuiltIn); feeds = feeds.filter(function(f) f.title == title); liberator.assert(feeds.length > 0, "not such jetpack feature"); return feeds[0]; } function getAllFeeds(includeBuiltin){ return [].concat(getSubscribedFeeds(includeBuiltin), getUnsubscribedFeeds()); } function getSubscribedFeeds(includeBuiltin){ let feeds = self.feedManager.getSubscribedFeeds(); if (includeBuiltin) return feeds; else return feeds.filter(function(f) !f.isBuiltIn); } function getUnsubscribedFeeds(){ return self.feedManager.getUnsubscribedFeeds(); } JavaScript.setCompleter([self.getContextByTitle], [function(){ return getSubscribedFeeds(true).map(function(f) [f.title, f.uri.spec]); }]); JavaScript.setCompleter([self.refresh, self.uninstall], [function(){ return getSubscribedFeeds(false).map(function(f) [f.title, f.uri.spec]); }]); JavaScript.setCompleter([self.purge], [function(){ return getAllFeeds(false).map(function(f) [f.title, f.uri.spec]); }]); JavaScript.setCompleter([self.getFeedByTitle], [function(){ return getAllFeeds(true).map(function(f) [f.title, f.uri.spec]); }]); JavaScript.setCompleter([self.reinstall], [function(){ return getUnsubscribedFeeds().map(function(f) [f.title, f.uri.spec]); }]); JavaScript.setCompleter([self.install], [function (context, obj, args) { context.quote[2] = ""; completion.file(context, true); }]); JavaScript.setCompleter([slideBarProto.select], [function(){ return self.slideBar.features.map(function(f) [f.context.feed.title, f.context.feed.uri.spec]); }]); return self; })(); // vim: sw=2 ts=2 et: