aboutsummaryrefslogtreecommitdiffstats
path: root/cpan-search.js
blob: 49c1238aca6377d72978ff5c50c5cbf3040a61a2 (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
var PLUGIN_INFO =
<VimperatorPlugin>
<name>{NAME}</name>
<description>CPAN search</description>
<description lang="ja">CPAN モジュールを検索し補完します</description>
<minVersion>2.0</minVersion>
<maxVersion>2.0pre</maxVersion>
<updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/cpan-search.js</updateURL>
<author mail="hotchpotch@gmail.com" homepage="http://tako3.net/http://d.hatena.ne.jp/secondlife/">Yuichi Tateno</author>
<license>MPL 1.1/GPL 2.0/LGPL 2.1</license>
<version>0.1</version>
<detail><![CDATA[
:cpan[!] Moo::Me[tab]

CPAN モジュールリストは http://cpan.ma.la/list から vimp 起動時に初回ロードされます。(thx: mala!)
検索は1単語なら indexOf 探索ですが: を含む言葉(Foo::Bar など)なら RegExp 検索になるので重いかもしれません
WebService::Hatena をマッチさせたいなら Web::Ha[tab] などで補完できると思います
:cpan!  bang をつけると別のタブで開きます
]]></detail>
</VimperatorPlugin>;

(function() {
var p = function(arg) {
    Application.console.log(arg);
    // liberator.log(arg);
}

// preload cpan list
var cpanListURL = liberator.globalVariables.cpanSearchListURL || 'http://cpan.ma.la/list';
if (!liberator.globalVariables.cpanListCache) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                liberator.globalVariables.cpanListCache = 
                    xhr.responseText.split(/\r?\n/).map(function(i) [i, '', i.toUpperCase()]);
            } else {
                liberator.echoerr('CPAN Search: XHR Error: ' + xhr.statusText);
                // throw new Error(xhr.statusText);
            }
        }
    }
    xhr.open('GET', cpanListURL, true);
    xhr.send(null);
}

commands.addUserCommand(
    ['cpan'],
    'CPAN Search', 
    function(args) {
        var name = (args.string || '').replace(/\s/g, '').replace(/^\^/,'');
        var url = 'http://search.cpan.org/perldoc?' + name;
        liberator.open(url, args.bang ? liberator.NEW_TAB : null);
    }, {
        completer: function(context) {
            context.title = ['MODULE NAME', ''];
            var word = context.filter.toUpperCase();
            if (word.indexOf(':') >= 0) {
                var regex = word.split(/:+/).map(function(i) i + '[^:]*').join('::');
                regex = new RegExp('^' + regex.replace(/\[\^:\]\*$/, ''));
                context.filters = [function (item) regex.test(item.item[2])];
            } else {
                context.filters = [function (item) item.item[2].indexOf(word) != -1];
            }
            context.completions = liberator.globalVariables.cpanListCache || [];
        },
        argCount: '1',
        bang: true,
    },
    true
);

})();