aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_loader.js
blob: 035033251e0316c59c7855fc7848d99e6dd6eb4e (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
// ==VimperatorPlugin==
// @name           Plugin Loader
// @description    to load plugins from specified directory at starting up Vimperator.
// @description-ja 指定(ディレクトリ|プラグイン)を起動時にロードする
// @license        Creative Commons 2.1 (Attribution + Share Alike)
// @version        2.3
// @minVersion     1.2
// @maxVersion     2.0Pre
// @author         anekos
// ==/VimperatorPlugin==
//
// Usage:
//    let g:plugin_loader_roots = "<PLUGIN_DIRECTORIES>"
//    let g:plugin_loader_plugins = "<PLUGIN_NAMES>"
//
// Example:
//    let g:plugin_loader_roots = "/home/anekos/coderepos/vimp-plugins/ /home/anekos/my-vimp-plugins/"
//    let g:plugin_loader_plugins = "lo,migemized_find,nico_related_videos"
//
// Link:
//    http://d.hatena.ne.jp/nokturnalmortum/20081008#1223397705

{
  function toArray (obj) {
    return obj instanceof Array ? obj
                                : obj.toString().split(/[,| \t\r\n]+/);
  }

  let roots = toArray(liberator.globalVariables.plugin_loader_roots);
  let plugins = toArray(liberator.globalVariables.plugin_loader_plugins);
  let filter = new RegExp('[\\\\/](?:' +
                          plugins.map(function (plugin) plugin.replace(/(?=[\\^$.+*?|(){}\[\]])/g, '\\'))
                                 .join('|') +
                          ')\\.(?:js|vimp)$');

  liberator.log('plugin_loader: loading');

  roots.forEach(function (root) {
    let files = io.readDirectory(io.getFile(root), true);
    files.forEach(function (file) {
      if (filter.test(file.path)) {
        liberator.log("Sourcing: " + file.path);
        io.source(file.path, false);
      }
    });
  });

  liberator.log('plugin_loader: loaded');
}